Solved

Can't see php code problem

Posted on 2016-10-04
12
33 Views
Last Modified: 2016-10-04
See code.
$qry = "SELECT * from auth_users where email ='jcarowitz@gmail.com' or email='richardkorts@gmail.com' or email = 'jeremy.hanson@watermanusa.com'";
$users = $wpdb->get_results($qry, ARRAY_A);	
$i = 0;
foreach($users as $user) {
	$uem = $user['email'];
	$entity = $user['entity'];
	$ocode = $user['code'];
	switch($uem) {
		case "jcarowitz@gmail.com":
			$entity = "FRANKA.OLSEN";
			$ocode = 2974;
			break;
		case "jeremy.hanson@watermanusa.com":
			$entity = "NEWMANREGENCYGROUP";
			$ocode = 5236;
			break;
		case "richardkorts@gmail.com":
			$entity = "CSAMSCO";
			$ocode = 2008;
			break;	
	}		
	if ($entity == "") {
		$ocode = "All";
		$entity = "All";
	}	
	$mail            = new PHPMailer(); // defaults to using php "mail()"

	$mail->isSMTP(); // telling the class to use SMTP transport
	$mail->SetFrom('noreply@watermanusa.com');
	$body             = "Dear " . $user['name'] . "<br><br>";
	$body = $body . "Greetings!<br><br>";
	$body = $body . "Here is your weekly update of quotes in process with Waterman Industries estimating department.   Please take a minute to review the status of your projects.  Check in with our estimating team if you have questions or concerns.<br><br>";
	$body = $body . "Thank you for your efforts toward growing our mutual success in your region.<br><br>";
	$body = $body . "The Team at Waterman Industries";
	$mail->Subject = "Your Weekly Status Report from Waterman Industries";
	$mail->AddAddress($user['email']);
	$someAttach = false;
	$file = $rfqdir . $entity . "rfqs.xlsx";
	//echo "attach = " . $file . "<br>";
	//echo "email = " . $user['email'] . "<br>";
	if (file_exists ($file)) {
		$someAttach = true;
		$mail->AddAttachment($file);
	}
	$ofile = $orddir . $ocode . "orders.xlsx";
	if (file_exists ($ofile)) {
		$someAttach = true;
		$mail->AddAttachment($ofile);
	}
	if (! $someAttach) {
		$body = $body . "<br><br>There are currently no RFQ's or orders for " . $entity;
	}	
	$mail->MsgHTML($body);

	if(!$mail->Send()) {
		echo "Mailer Error: " . $mail->ErrorInfo;
	} else {
		echo "Mail sent to " . $user['email'] . "<br>";
	}

}	

Open in new window


This is not all but the essential parts of this program.

Note the switch statement based on email. The 3rd one works. The top two do not, for the top two, the default values are applied ($entity = "All", $ocode = "All")

Is the syntax / structure on the case statement incorrect?

Or any other way the that $entity gets set back to "All" before the email attachments?

Thanks
0
Comment
Question by:Richard Korts
  • 5
  • 4
  • 3
12 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 41828591
The $entity variable appears to be set ("assigned a value") on these lines: 6, 10, 14, 18, 24.  From the look of things, the if() statement on line 22 leads to the only place that $entity can be assigned the value "All."  Since this appears to be a data-dependent problem, what I would do is log or display the value of $users right after line 2.  You would expect to find an array of associative arrays ("sub-arrays" if you will).  Please let us have a look at that and we can probably find the issue once we see the data.
0
 

Author Comment

by:Richard Korts
ID: 41828787
Ray,

I did that. I echoed $users. It said Array() (no data)

But I thought why is it working for me & not them.

I limited the SQL to ONE of the others, it assigns entity of "".

So I put in other echos, I'm guessing there are unprintable characters (probably spaces) in the others, haven't been able to get rid of them yet, I know about trim, etc.
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 250 total points
ID: 41828789
Switch statements have a default option. I would change your code to this
switch($uem) {
		case "jcarowitz@gmail.com":
			$entity = "FRANKA.OLSEN";
			$ocode = 2974;
			break;
		case "jeremy.hanson@watermanusa.com":
			$entity = "NEWMANREGENCYGROUP";
			$ocode = 5236;
			break;
		case "richardkorts@gmail.com":
			$entity = "CSAMSCO";
			$ocode = 2008;
			break;	
		default:
			// If execution gets here it is because the 
			// data (uem) did match any of the conditions
echo "DEBUG: UEM: {$uem}<br/>";
			$ocode = "All";
			$entity = "All";
	}

Open in new window

Let's see what the debug statement yields. I am betting a spelling error or a case issue or both.
0
 

Author Closing Comment

by:Richard Korts
ID: 41828811
I figured out myself, but your inputs helped & I rely on EE and IN PARTICULAR you guys to bail me out
0
 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41828839
No problem Richard, as a matter of interest - what was the problem?
0
 

Author Comment

by:Richard Korts
ID: 41828861
There were leading or trailing spaces (or both) in the email addresses in the database. So comparing the database value to "xyz@abc.com" did not work because the database email was like " xyz@abc.com ".

So I just used trim().

As usual, very simple ONCE you see it.

Richard
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 51

Expert Comment

by:Julian Hansen
ID: 41828901
Ok - that will do it.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41828927
I echoed $users. It said Array()
That's one of the many problems with PHP.  You can't get a sensible value when you cast an array to a string!  But you can use var_dump() on any variable and you will be able to see what it's made of.  In a debugging context, echo is nearly useless because it only works on strings, and in the increasingly object-oriented environment strings are less and less common primary data structures.  Echo sometimes provides useless or misleading results (as you have just seen).
0
 

Author Comment

by:Richard Korts
ID: 41828940
Thanks for that observation, Ray.

FYI, it AMAZES me how far things have come.

I first worked on computers in the '60's, with vacuum tube mainframes, punched cards and NO mass storage, only reel to reel tapes.

I worked on an IBM 7090, top of the line when I graduated college, they upgraded that to transistor storage, called a 7094. 32K 36bit words, approx equal to 192K bytes now.

My iPhone is probably 100,000 times more powerful than that, what at the time cost millions of dollars.

Now we do all this & can develop a meaningful set of programs in a day.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41828949
Yeah, I take your point.  We flew to the moon and today your iPhone has more computing power than all of NASA's computers in the late 1960's!  It's kind of amazing that we got out astronauts back alive (or at all).
0
 

Author Comment

by:Richard Korts
ID: 41828970
Ray,

I try to explain this to younger people; they cannot comprehend it (as they scan their smartphone for Facebook or Instagram).

Two of the most significant inventions of all time. We didn't know we needed them but once they were there, we HAD to have them.

The iPhone & the light bulb.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41829050
:-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now