[SOLVED] Need Help With A Date Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nmphpwebdev
Forum Newbie
Posts: 13
Joined: Fri Oct 01, 2004 12:29 pm

[SOLVED] Need Help With A Date Script

Post by nmphpwebdev »

Code: Select all

<?
            //Display current monthly events
            echo "<table width="100%" border="1">\n";
                echo "<tr>\n";
                    echo "<td><b>Event</b></td>\n";
                    echo "<td><b>Start Date</b></td>\n";
                    echo "<td><b>End Date</b></td>\n";
                    echo "<td><b>Location</b></td>\n";
                    echo "<td><b>Contact Person</b></td>\n";
                    echo "<td><b>Comments</b></td>\n";
                echo "</tr>\n";

                if ($_GET['action'] == 'View') {
                    $month = str_repeat("0", (2 - strlen($_GET['months']))).$_GET['months'];

                    $result2 = mysql_query("select event,
                                                         DATE_FORMAT( startdate, '%M %d, %Y' ) as sd,
                                                         DATE_FORMAT( enddate, '%M %d, %Y' ) as ed,
                                                         location, contact, comments from events where
                                                         startdate = '".$_GET['years']."-$month-01'");
echo $_GET['years'];
echo '&nbsp;';
echo $month;
                    if (mysql_num_rows($result2)) {
                        while ($rows2 = mysql_fetch_array($result2)) {
                            echo "<tr>\n";
                                echo "<td>".$rows2['event']."</td>\n";
                                echo "<td>".$rows2['sd']."</td>\n";
                                echo "<td>".$rows2['ed']."</td>\n";
                                echo "<td>".$rows2['location']."</td>\n";
                                echo "<td>".$rows2['contact']."</td>\n";
                                echo "<td>".$rows2['comments']."</td>\n";
                            echo "</tr>\n";
                        }
                    } else {
                        echo "<tr>\n";
                            echo "<td>&nbsp;</td>\n";
                            echo "<td></td>\n";
                            echo "<td></td>\n";
                            echo "<td></td>\n";
                            echo "<td></td>\n";
                            echo "<td></td>\n";
                        echo "</tr>\n";
                                                

                    }

                } else {
                    //Display current month events only
                    if (mysql_num_rows($result1)) {
                        while($rows1 = mysql_fetch_array($result1)) {
                            echo "<tr>\n";
                                echo "<td>".$rows1['event']."</td>\n";
                                echo "<td>".$rows1['sd']."</td>\n";
                                echo "<td>".$rows1['ed']."</td>\n";
                                echo "<td>".$rows1['location']."</td>\n";
                                echo "<td>".$rows1['contact']."</td>\n";
                                echo "<td>".$rows1['comments']."</td>\n";
                            echo "</tr>\n";
                        }
                    } else {
                        echo "<tr>\n";
                            echo "<td>&nbsp;</td>\n";
                            echo "<td></td>\n";
                            echo "<td></td>\n";
                            echo "<td></td>\n";
                            echo "<td></td>\n";
                            echo "<td></td>\n";
                        echo "</tr>\n";
                                                
                    }
                }
                          echo "</table>";
?>

The script is only giving me the 1st date of the month.

Where did I go off track in getting all dates listed for the month?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Can you explain this all a bit more? Coming here and saying it's only showing the first date doesn't help us much to understand what you are doing and how you are doing it.

Right now, the "second" query you perform is pulling things that started at the beginning of the year and month supplied. Maybe the other dates don't start then?
nmphpwebdev
Forum Newbie
Posts: 13
Joined: Fri Oct 01, 2004 12:29 pm

Post by nmphpwebdev »

Let's have you take a peak at all of the code:

Code: Select all

<?php
include_once( "dbconnect.php" );
$event = strftime( "%m %Y" );

//Set and run query
$result1 = mysql_query("select event, DATE_FORMAT( startdate, '%M %d, %Y' ) as 
				 sd, DATE_FORMAT( enddate, '%M %d, %Y' ) as ed, location, contact, 
				 comments from events where DATE_FORMAT( startdate, '%m %Y' ) = "$event" ");

//Set unix epoch timestamp for query
$today = strftime( "%B, %Y" );
?>

<h2>What's happening in <?php echo $today ?></h2>
<table>
    <tr>
        <td>This is a developing calendar, and other events will be added. While this unified calendar is intended for the purpose of internal planning and development for CES, other departments, agencies, and organizations are encouraged to <a href="submit.php">submit events</a>.</td>
        <td>
            <?
            //Future and past events drop down menu
            $months = array(1=>'January', 'February', 'March', 'April', 'May', 'June',
            'July', 'August', 'September', 'October', 'November', 'December');

            //Month selection drop down
            echo "<form action="index.php" method="GET">\n";
                echo "<table border="1">\n";
                    echo "<th colspan="2">View Other Months</th>\n";
                    echo "<tr>\n";
                        echo "<td>\n";
                            echo "<select name="months">\n";
                                foreach ($months as $key => $val) {
                                    ($key == date('m', time())) ? ($sel = " selected") : ($sel = "");
                                    echo "<option value="".$key.""$sel>".$val."</option>\n";
                                }
                            echo "</select>\n";
                        echo "</td>\n";
                        echo "<td>\n";
                            echo "<select name="years">\n";
                                for ($i=(date('Y', time()) - 5); $i<(date('Y', time()) + 5); $i++) {
                                    ($i == date('Y', time())) ? ($sel = " selected") : ($sel = "");
                                    echo "<option value="".$i.""$sel>".$i."</option>\n";
                                }
                            echo "</select>\n";
                        echo "</td>\n";
                    echo "</tr>";
                    echo "<tr>\n";
                        echo "<td colspan="2" align="center"><input type="submit" name="action" value="View"></td>\n";
                    echo "</tr>\n";
                echo "</table>";
            echo "</form>";
            ?>
        </td>
    </tr>
    <tr>
        <td>CES Faculty - Please note: Policy reminder: All events that result in any form of in-service training, 
            either given or received, are required to be included in the CES events calendar and must have prior approval 
            before announcement. To secure approval for any event please submit your request to the 
            <a href="mailto:swestern@nmsu.edu">SW District Office</a>.</td>
    </tr>
    <tr>
        <td>
            <?php
            //Display current monthly events
            echo "<table width="100%" border="1">\n";
                echo "<tr>\n";
                    echo "<td><strong>Event</strong></td>\n";
                    echo "<td><b>Start Date</b></td>\n";
                    echo "<td><b>End Date</b></td>\n";
                    echo "<td><b>Location</b></td>\n";
                    echo "<td><b>Contact Person</b></td>\n";
                    echo "<td><b>Comments</b></td>\n";
                echo "</tr>\n";

                if ($_GET['action'] == 'View') {
                    $month = str_repeat("0", (2 - strlen($_GET['months']))).$_GET['months'];

                    $result2 = mysql_query("select event, DATE_FORMAT( startdate, '%M %d, %Y' ) as sd, 
															 DATE_FORMAT( enddate, '%M %d, %Y' ) as ed, location, contact, comments from events where 
															 startdate = '".$_GET['years']."-$month-01'");
echo $_GET['years'];
echo '&nbsp;';
echo $month;
                    if (mysql_num_rows($result2)) {
                        while ($rows2 = mysql_fetch_array($result2)) {
                            echo "<tr>\n";
                                echo "<td>".$rows2['event']."</td>\n";
                                echo "<td>".$rows2['sd']."</td>\n";
                                echo "<td>".$rows2['ed']."</td>\n";
                                echo "<td>".$rows2['location']."</td>\n";
                                echo "<td>".$rows2['contact']."</td>\n";
                                echo "<td>".$rows2['comments']."</td>\n";
                            echo "</tr>\n";
                        }
                    } else {
                        echo "<tr>\n";
                            echo "<td>&nbsp;</td>\n";
                            echo "<td></td>\n";
                            echo "<td></td>\n";
                            echo "<td></td>\n";
                            echo "<td></td>\n";
                            echo "<td></td>\n";
                        echo "</tr>\n";
												

                    }

                } else {
                    //Display current month events only
                    if (mysql_num_rows($result1)) {
                        while($rows1 = mysql_fetch_array($result1)) {
                            echo "<tr>\n";
                                echo "<td>".$rows1['event']."</td>\n";
                                echo "<td>".$rows1['sd']."</td>\n";
                                echo "<td>".$rows1['ed']."</td>\n";
                                echo "<td>".$rows1['location']."</td>\n";
                                echo "<td>".$rows1['contact']."</td>\n";
                                echo "<td>".$rows1['comments']."</td>\n";
                            echo "</tr>\n";
                        }
                    } else {
                        echo "<tr>\n";
                            echo "<td>&nbsp;</td>\n";
                            echo "<td></td>\n";
                            echo "<td></td>\n";
                            echo "<td></td>\n";
                            echo "<td></td>\n";
                            echo "<td></td>\n";
                        echo "</tr>\n";
												
                    }
                } 
						  echo "</table>";
            ?>
        </td>
    </tr>
</table>
nmphpwebdev
Forum Newbie
Posts: 13
Joined: Fri Oct 01, 2004 12:29 pm

Post by nmphpwebdev »

Secondly,

This page http://cahedev.nmsu.edu/eventscopy is a list of all upcoming events.

There is a date selection box that would enable a visitor to select another month to view the events.

As of now, when you select a date the only event is on the 1st day of the month.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Again, your second query, now starting on line 77, is selecting all events that start on the first of X month of Y year, nothing else. Not all the events start on that date. So it is functioning correctly. You need to modify the query so it finds all events that start in X month of Y year, regardless of the day of the month they start in. I would use the [mysql_man]date functions[/mysql_man] to tell it which year and month you want start dates to be in.
nmphpwebdev
Forum Newbie
Posts: 13
Joined: Fri Oct 01, 2004 12:29 pm

Post by nmphpwebdev »

$result2 = mysql_query("select event, DATE_FORMAT( startdate, '%M %d, %Y' ) as sd, DATE_FORMAT( enddate, '%M %d, %Y' ) as ed, location, contact, comments from events where -->

This is where I thought the select is going to start "<-- startdate = '".$_GET['years']."-$month-01'");" If I am reading my code correctly, I should be getting the years and months here, and not paying any attention to the days of the month.

Am I not reading my own code correctly?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

*sigh* for the third and final time now... Your query is asking for the events that begin on the first day of the month and year selected.
nmphpwebdev
Forum Newbie
Posts: 13
Joined: Fri Oct 01, 2004 12:29 pm

Post by nmphpwebdev »

Your help is greatly appreciated.

I see that the I am asking for $month-01 ( which is asking for the 1st day of the month) . When I take the 01 out of the variable, I loose all of my results.

I have to find where I set this up to add in the date( "m" ) to get the rest of the dates in the month.

I am printing the $_GET['years'] and $months variables and I thought I had it pegged because those variables are showing what the select box is sending to php.

BUT, when mysql gets involved, I loose the rest of the days.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you can use the [mysql_man]extract[/mysql_man]() mysql function. To match just the year and month.
nmphpwebdev
Forum Newbie
Posts: 13
Joined: Fri Oct 01, 2004 12:29 pm

Post by nmphpwebdev »

WOOOO....FRIGGIN HOOOOOOOOOOOOOO

thank you kind sir.

extract did the trick.

check it out

http://cahedev.nmsu.edu/eventscopy
Post Reply