Solved

Can't see php code problem

Posted on 2016-10-04
12
42 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 109

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 54

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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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 54

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 54

Expert Comment

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

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 109

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 109

Expert Comment

by:Ray Paseur
ID: 41829050
:-)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 dynamically set the form action using jQuery.

776 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