ivan8r Posted November 29, 2007 Share Posted November 29, 2007 I'm not great with PHP, so hopefully you pro's will find my problem easily. I've written a script that pulls a month number from a database table and is supposed to display it on the screen. For some reason, any month that is before the current month (November) is displayed as November. December is not having this problem, but January through October do not display correctly. Here's the code: <?php //get current date and split into month, day and year $cur_month = date ('n'); $cur_day = date ('j'); $cur_year = date ('Y'); //month name array $monthname[1]="January"; $monthname[2]="February"; $monthname[3]="March"; $monthname[4]="April"; $monthname[5]="May"; $monthname[6]="June"; $monthname[7]="July"; $monthname[8]="August"; $monthname[9]="September"; $monthname[10]="October"; $monthname[11]="November"; $monthname[12]="December"; //retrieve all records from the db $result = mysql_query('SELECT * FROM class_schedule ORDER BY class_year,class_month,class_day'); if (!$result) { die('Invalid query: ' . mysql_error()); } //generate html table to display class schedule print "<div align=center> <font size=5>Upcoming Classes</font><br><br> <table border=0 cellpadding=2 cellspacing=0 bordercolor=#cccccc width=100%> <tbody> <tr> <td align=center valign=bottom bgcolor=#cccccc><b><font color=black> Course Name </font></b></td> <td align=center valign=bottom bgcolor=#cccccc><b><font color=black> Date </font></b></td> <td align=center valign=bottom bgcolor=#cccccc><b><font color=black> Seats Available </font></b></td> <td align=center valign=bottom bgcolor=#cccccc><b><font color=black> Enroll? </font></b></td> </tr>"; //loop to generate class schedule $color1="#ffffff"; $color2="#eeeeee"; while ($row = mysql_fetch_assoc($result)) { //grab date variables for current row in the db $month=$row['class_month']; $day=$row['class_day']; $year=$row['class_year']; //if class date is in the past, do not display it if ($month>"$cur_month" OR $year>"$cur_year" OR $year=="$cur_year" AND $month=="$cur_month" AND $day>="$cur_day"){ print "<tr> <td align=center bgcolor='$bgcolor'><b> "; $course=$row['class_name']; $class=str_replace(" ", "_", $course); echo $course; print " </b></td> <td align=center bgcolor='$bgcolor'> "; $scheduled = $monthname[$month] ." ". $day .", ". $year; $course_date = $month ."/". $day ."/". $year; echo "$scheduled"; print " </td> <td align=center bgcolor='$bgcolor'> "; if ($row['num_students'] >= { print "Class Is Full </td> <td align=center bgcolor='$bgcolor'> Unavailable </td> </tr>"; } else { $seats = 8-$row['num_students']; echo $seats; print " </td> <td align=center bgcolor='$bgcolor'> <a href=index.php?option=com_chronocontact&chronoformname=enroll&course=$class&date=$course_date&month=$month&day=$day&year=$year>Enroll</a> </td> </tr>"; } } // this "toggles" the color $bgcolor=($bgcolor==$color1)?$color2:$color1; } //html to close table tags print "</tbody> </table> </div>"; //What's the date? echo "<br>Today's Date: " .$monthname[$cur_month]. " " .$cur_day. ", " .$cur_year. "<br><br>"; ?> Here is a picture of the database content: Here's a picture of the output (from a similar script that outputs date in number format): You can clearly see that anytime I try to display a month that is January through October, it is not displayed correctly. The website where this exists is http://testing.mtaonline.net. I can't seem to find the error. If you could help me out, I'd really appreciate it!! Ivan Vasquez http://www.akwebz.com Quote Link to comment Share on other sites More sharing options...
btherl Posted November 29, 2007 Share Posted November 29, 2007 That's output from a "similar" script? Can you show us the similar script? Or show us the output from this script. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted November 29, 2007 Share Posted November 29, 2007 there is an amazing date function library in php use it! Quote Link to comment Share on other sites More sharing options...
marcus Posted November 29, 2007 Share Posted November 29, 2007 You got some nice classes to enroll in there. Ha. Quote Link to comment Share on other sites More sharing options...
ivan8r Posted November 29, 2007 Author Share Posted November 29, 2007 That's output from a "similar" script? Can you show us the similar script? Or show us the output from this script. Here's the *similar* script. <?php //get current date and split into month, day and year $cur_month = date ('n'); $cur_day = date ('j'); $cur_year = date ('Y'); //retrieve all records from the db $result = mysql_query('SELECT * FROM class_schedule ORDER BY class_year,class_month,class_day'); if (!$result) { die('Invalid query: ' . mysql_error()); } //generate html table to display class schedule print "<div align=center> <table border=0 cellpadding=2 cellspacing=0 bordercolor=#cccccc width=100%> <tbody> <tr> <td align=center valign=bottom bgcolor=#cccccc><b><font color=black> Course Name </font></b></td> <td align=center valign=bottom bgcolor=#cccccc><b><font color=black> Date </font></b></td> </tr>"; //loop to generate class schedule $color1="#ffffff"; $color2="#eeeeee"; while ($row = mysql_fetch_assoc($result)) { //grab date variables for current row in the db $month=$row['class_month']; $day=$row['class_day']; $year=$row['class_year']; //if class date is in the past, do not display it if ($year >= "$cur_year" AND $month > "$cur_month" OR $year >= "$cur_year" AND $month="$cur_month" AND $day>="$cur_day" OR $year>"$cur_year"){ print "<tr> <td align=center bgcolor='$bgcolor'><b> "; echo $row['class_name']; print " </b></td> <td align=center bgcolor='$bgcolor'> "; $scheduled = $month ."/". $day ."/". $year; echo "$scheduled"; print " </td>"; } // this "toggles" the color $bgcolor=($bgcolor==$color1)?$color2:$color1; } //html to close table tags print "</tbody> </table> </div> <br><b> <a href=index.php?option=com_chronocontact&chronoformname=upcoming&Itemid=15>Click here to enroll in a class</a></b>"; ?> It's got the same basic code... that's why I didn't post it the first time. Quote Link to comment Share on other sites More sharing options...
ivan8r Posted November 29, 2007 Author Share Posted November 29, 2007 there is an amazing date function library in php use it! I'm still learning. What could I have done different to make it better? Quote Link to comment Share on other sites More sharing options...
ivan8r Posted November 29, 2007 Author Share Posted November 29, 2007 A coworker helped me figure out the problem. the line that said: if ($month>"$cur_month" OR $year>"$cur_year" OR $year=="$cur_year" AND $month=="$cur_month" AND $day>="$cur_day"){ was changed to: if ($month>"$cur_month" OR $year>"$cur_year" OR ($year=="$cur_year" AND $month=="$cur_month" AND $day>="$cur_day")){ and now the dates display as they should. 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.