Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 60
  • Last Modified:

Can't see php code problem

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
Richard Korts
Asked:
Richard Korts
  • 5
  • 4
  • 3
2 Solutions
 
Ray PaseurCommented:
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
 
Richard KortsAuthor Commented:
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
 
Julian HansenCommented:
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!

 
Richard KortsAuthor Commented:
I figured out myself, but your inputs helped & I rely on EE and IN PARTICULAR you guys to bail me out
0
 
Julian HansenCommented:
No problem Richard, as a matter of interest - what was the problem?
0
 
Richard KortsAuthor Commented:
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
 
Julian HansenCommented:
Ok - that will do it.
0
 
Ray PaseurCommented:
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
 
Richard KortsAuthor Commented:
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
 
Ray PaseurCommented:
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
 
Richard KortsAuthor Commented:
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
 
Ray PaseurCommented:
:-)
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now