Help with query

Thread Solved

Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Help with query

 
0
  #1
May 20th, 2009
Hey Guys,

I need a little help. I have a calendar script that I want to write a query for and pull out the day of the week, then have it send an email showing all the entries for that day. Unfortunately i'm querying a day, month, year as seperate fields and they are int not date fields. I have accomplished querying the database and used a date/mktime function to convert the int fields into a date field. Where I'm running into trouble is, I can't figure out how to pull this date field and compair it to todays date (date()), and have it display only the records from todays. Here is the code I have so far, I know it is very simple so If you have a better way of doing this, please let me know.
  1. <?php
  2. require("config.php");
  3.  
  4. mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
  5. mysql_select_db(DB_NAME) or die(mysql_error());
  6.  
  7. $query = "SELECT * FROM pec_mssgs ORDER BY m, d";
  8. $result = mysql_query($query) or die('Nope, query didn\'t work');
  9. while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  10. {
  11. $d = $row['d'];
  12. $m = $row['m'];
  13. $y = $row['y'];
  14. $title = $row['title'];
  15. $text = $row['text'];
  16.  
  17. $newdate = date("Y-m-d", mktime(0, 0, 0, $m, $d, $y));
  18.  
  19. echo "$newdate ($text, $title)<br>\n";
  20. }
  21.  
  22. if ($newdate == date("Y-m-d"))
  23. {
  24. echo "good if";
  25. } else
  26. echo "bad if";
  27.  
  28. ?>

This displays:
2009-01-07 (test, test)
2009-05-19 (test, test)
2009-05-20 (this is a test, today is 20 may)
2009-05-25 (Have a Great Day!!, Memorial Day)
2009-05-25 (test, testing day)
bad if

So far it is doing what I want but I want only those listed above that match today's date. Then I can take those items and send them out in an e-mail.

Any help is much appreciated. Thx.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,461
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 511
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Help with query

 
0
  #2
May 20th, 2009
Why not set the parameters of your query to only pull records where m=$todaysMonth and d=$todaysDay and y=$todaysYear?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: Help with query

 
0
  #3
May 20th, 2009
I tried this:
  1. $todaysMonth = date('m');
  2. $todaysDay = date('d');
  3. $todaysYear = date('y');
  4.  
  5. mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
  6. mysql_select_db(DB_NAME) or die(mysql_error());
  7.  
  8. $query = "SELECT * FROM pec_mssgs WHERE m=$todaysMonth, d=$todaysDay, y=$todaysYear";
  9. $result = mysql_query($query) or die('Nope, query didn\'t work');

My result was "Nope, query didn't work". Did I put it wrong in my select statement?
Thanks.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,461
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 511
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Help with query

 
0
  #4
May 20th, 2009
You would want the clause to be m=$todaysMonth AND d=$todaysDay AND y=$todaysYear
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: Help with query

 
1
  #5
May 20th, 2009
It should be
  1. $query = "SELECT * FROM pec_mssgs WHERE m=$todaysMonth AND d=$todaysDay AND y=$todaysYear";

Edit: Ezzaral beat me to it
Last edited by Will Gresham; May 20th, 2009 at 4:11 pm.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: Help with query

 
0
  #6
May 20th, 2009
Ok, i see my error, my select statement needed "And" instead of ",". It seems to be working just fine they way you said. I will try to progress with that. Thank you so much for the help.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: Help with query

 
0
  #7
May 20th, 2009
Ok, all is working well, I put the email functions in and it is working fine. My only problem now is that if there are more than one item on the calendar for that day, the email lists only the last item, not all of them. I need it to either list all of them, or one email for each, either one is acceptable. Here is the code I have so far:
  1. <?php
  2. require("config.php");
  3.  
  4. $todaysMonth = date('m');
  5. $todaysDay = date('d');
  6. $todaysYear = date('Y');
  7. echo "the year is: $todaysYear<br>";
  8. echo "the month is: $todaysMonth<br>";
  9. echo "the day is: $todaysDay<br>";
  10. //echo $today;
  11.  
  12. mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
  13. mysql_select_db(DB_NAME) or die(mysql_error());
  14.  
  15. $query = "SELECT * FROM pec_mssgs WHERE m=$todaysMonth AND d=$todaysDay AND y=$todaysYear";
  16. $result = mysql_query($query) or die('Nope, query didn\'t work');
  17. while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  18. {
  19. $d = $row['d'];
  20. $m = $row['m'];
  21. $y = $row['y'];
  22. $title = $row['title'];
  23. $text = $row['text'];
  24.  
  25. $newdate = date("Y-m-d", mktime(0, 0, 0, $m, $d, $y));
  26.  
  27. $to = "myemail$here.com";
  28. $subject = "Today's Calendar Reminder";
  29. $message = "Today's Calendar Reminder: $title";
  30. $from = "Web Calendar Reminder";
  31. $headers = "From: $from";
  32.  
  33. echo "$newdate ($text, $title)<br>\n";
  34. }
  35.  
  36. if ($newdate == date("Y-m-d"))
  37. {
  38. mail($to,$subject,$message,$headers);
  39. } else
  40.  
  41. echo "bad if";
  42.  
  43. ?>

I'm thinking I need some sort of a loop maybe?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: Help with query

 
0
  #8
May 20th, 2009
When you assign a value to $message, use .= rather than just = , this will append the text to the end of the current value. You may also want to put an \r\n onto the end of the string to add a new line at the end of each event.

EDIT: Also, take some of those statements that will not change out of the WHILE statement (i.e. the $from and $subject) otherwise they are having the same values written to them multiple times.

While will loop through all results.
Last edited by Will Gresham; May 20th, 2009 at 5:02 pm.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: Help with query

 
0
  #9
May 20th, 2009
Thank you Xan, I entered this:
  1. $message .= "Today's Calendar Events Reminder: $title\r\n";
it displays multiple events, but does not put a return break after each one. Also, is there a way to put the part before $title only once, otherwise it prints "Today's Calendar Events Reminder" for each event. If it does, its ok for now, but looks funny

thanks again.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro

Re: Help with query

 
0
  #10
May 20th, 2009
Rather than
  1. $message .= "Today's Calendar Events Reminder: $title\r\n";
Just put
  1. $message .= "$title\r\n";

Then before use mail() put:
  1. $message = "Today's Calendar Events Reminder: " . $message;
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC