Solved

Can't see php code problem

Posted on 2016-10-04
12
37 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 52

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 52

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

932 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

10 Experts available now in Live!

Get 1:1 Help Now