bradkenyon Posted August 27, 2008 Share Posted August 27, 2008 i have a script that pulls events from a table and sends the events in an email. i want to only send an email when there are events for that day. if there are no events, whats the point of sending them. this is what I have now, with no checking in place I have a check for if($result_upcoming), and if($result) - i don't think those if's are doing any good, since they both contain values of resource id's, so when i went to test w/ something along these lines.... if there is nothing in result_upcoming or result, then don't send the email result_upcoming, is upcoming events, to be featured at the bottom of the email result, is the events for that day ya ya, i should of came up w/ a better naming convention. what should i do to check to see if there are no events and no upcoming events for that day, to not send the email? Thanks in advance. <?php require_once("../customemailer/htmlMimeMail.php"); include('../cgi-bin/newsinclude/db.php'); function eventEmail($type, $subject, $emailto) { $today = date("Y-m-d"); //query for today's event $query = "select * from calendar_items WHERE expiredate LIKE '$today %' and event_type_id = '$type'"; $result=mysql_query($query); //query for future upcoming event to appear today $upcoming = "select * from calendar_items WHERE upcoming_event_featured_date LIKE '$today %' and event_type_id = '$type'"; $result_upcoming=mysql_query($upcoming); print '<h1>'.$type.'</h1>'; //if either has an event, then run and send something in the email, otherwise, don't do either if($result || $result_upcoming) { //checks to see if there is an event for today if($result) { print '<h1>Today</h1>'; $message.="<h1>Today's Events</h1>"; ?> <ul> <?php while($row = mysql_fetch_array($result)) { $i = 0; while($i <=0) { print '<p><strong>'.date("l F j, Y",strtotime($row['expiredate'])).'</strong> - <a href="/events/?id='.$row['id'].'">'.$row['subj'].'</a> - <small>'.$row['location'].'</small></p>'; $message.= "<h3>".$row['subj']."</h3> <p><strong>When:</strong> ".date("g:ma",strtotime($row['expiredate']))."<br /> <strong>Where:</strong> ".$row['location']."</p> <p>".nl2br($row['body'])."</p>"; $i++; } } print '</ul>'; } //checks to see if there is a future event that is displayed in today's email if($result_upcoming) { print '<h1>Events down the road...</h1>'; $message.="<h1>Events down the road...</h1>"; while($row_upcoming = mysql_fetch_array($result_upcoming)) { $u = 0; while($u <=0) { print '<p>'.$row_upcoming['subj'].$row_upcoming['id'].'</p>'; $message.= "<h3>".$row_upcoming['subj']."</h3> <p><strong>When:</strong> ".date("l F j, Y g:ma",strtotime($row_upcoming['expiredate']))."<br /> <strong>Where:</strong> ".$row_upcoming['location']."</p> <p>".nl2br($row_upcoming['body'])."</p>"; $u++; } } } //if there something in result and/or result_upcoming, it will run this $email = 'no-reply@domain.com'; $mail = new htmlMimeMail(); $mail->setHTML($message); $mail->setSubject($subject); //$mail->setFrom($email); //$result = $mail->send(array($emailto)); //echo $result ? 'Mail sent!' : 'Failed to send mail'; } else { print 'Nothing to send in the email, so it was not sent.'; } } ?> Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted August 27, 2008 Share Posted August 27, 2008 you can run an additional query to get the count of items. if the count is zero, dont run the next query to return all of the results Quote Link to comment Share on other sites More sharing options...
bradkenyon Posted August 27, 2008 Author Share Posted August 27, 2008 what would that query look like? i want to test both queries for if the count if zero. because both result, and upcoming_result need to be zero to not send out an email. because the email can still be sent to display the upcoming events even on days it has no events. the way upcoming events works, you can set an event to display in the daily event emailer a week, month, year ahead, whenever you set the day for it to display, it gives you the option to feature the event a week or month a head of time, instead of just the day of event, gives them more notice. so i want to make sure, if both is 0, then don't send it, but if either is over 0, then send it. Quote Link to comment Share on other sites More sharing options...
obsidian Posted August 27, 2008 Share Posted August 27, 2008 If you want to see if your arrays have values assigned to them, you can just do a count on them: <?php if (isset($result) && count($result) > 0) { // process result } ?> Quote Link to comment Share on other sites More sharing options...
bradkenyon Posted August 27, 2008 Author Share Posted August 27, 2008 I print out count($result) and count($result_upcoming) and both display the value 1. even with both having more than 1 event for both result and result upcoming. or is it referring to 1 as being true and 0 being not true, not true meaning, no events, true, meaning there are events. but when i have no events for either, it still sets the value to of result and upcoming_result to 1. Quote Link to comment Share on other sites More sharing options...
obsidian Posted August 27, 2008 Share Posted August 27, 2008 OK, I was saying that if it was an array, you could check the number of records that way. After looking at your code, the variables you are checking are database resources, so you need to check the number of rows like this: <?php if (mysql_num_rows($result) > 0) { // records returned, so process } ?> Quote Link to comment Share on other sites More sharing options...
bradkenyon Posted August 27, 2008 Author Share Posted August 27, 2008 perfect, thank you! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.