Solved

Can't see php code problem

Posted on 2016-10-04
12
45 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 110

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 56

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 56

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
 
LVL 56

Expert Comment

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

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 110

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 110

Expert Comment

by:Ray Paseur
ID: 41829050
:-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

726 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