PHP Calendar

Soldato
Joined
8 Oct 2005
Posts
4,184
Location
Midlands, UK
Hi,

Am looking through a few tutorials from the new web designer may and they have agood looking one of making a simple events calender. I've created the tables exactly, outtputted the sql to screen and ran the sql using phpmyadmin (this works btw) but can't get it working. I get an error saying:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in path

The line it highlights is:while($events[] = mysql_fetch_assoc($res))
Have tried a few things with quotes but still get nothing. Here is the original code from the mag (should display my cal with events for each day, but nothing shows even though ther are evnts in the events table):

If anyine is interested, the original files can be freely downloaded from their site. datbase.php just connects to and selects my database and header is just a bit of html code.


Thanks

PHP:
<?php

    include("database.php");
    include("header.php");
    
?>

    <h1>Your Calendar</h1>
    
    <?php
    
        $start = strtotime("last Monday");
        $finish = strtotime("next Sunday");
        
        if(isset($_GET["offset"])) {
            $offset = intval($_GET["offset"]);
            $start = $start + (604800 * $offset);
            $finish = $finish + (604800 * $offset);
        }
        
        $sql = "select * from events order by datetime asc";
        $res = mysql_query($sql);
        
        echo "NUM ROWS: " . mysql_num_rows($sql);
        
        while($events[] = mysql_fetch_assoc($res)) ?>





                <h2>Showing <?=date("d M Y", $start);?> - <?=date("d M Y", $finish);?></h2>
                
                <table class="calendar"><tr>
            
                <?php for($i = $start; $i <= $finish; $i = $i + 86400) {   ?>
                        <td><div class="heading"><?=date("D d M Y", $i);?></div>
                        <div class="events">
                <?php foreach($events as $key => $event) {
                            
                            if(($event["datetime"] > $i) && ($event["datetime"] < ($i + 86400))) {
                                ?><a href="edit.php?id=<?=$event["id"];?>"><?=$event["name"];?></a><br /><?php
                            }//End If
                            
                        }//End For ?>
                        </div></td>
                        <?php
                        if(date("D", $i) == "Sun") {
                            echo '</tr><tr>';
                        }
                        
                        
        }//End While
        
    ?>
    
    </tr></table>
    
    <br />
    
    <table class="links">
        <tr>
            <td align="center">
                <a href="view.php?offset=<?=$offset - 1;?>">
                    <img alt="Back A Week" src="images/left.png" />
                    <br />
                    Back A Week
                </a>
            </td>
            <td align="center">
                <a href="edit.php">
                    <img alt="New Event" src="images/new.png" />
                    <br />
                    New Event
                </a>
            </td>
            <td align="center">
                <a href="view.php">
                    <img alt="View Today" src="images/today.png" />
                    <br />
                    View Today
                </a>
            </td>
            <td align="center">
                <a href="view.php?offset=<?=$offset + 1;?>">
                    <img alt="Forward A Week" src="images/right.png" />
                    <br />
                    Forward A Week
                </a>
            </td>
        </tr>
    </table>
 
Actually I do need some help as I've extended the period that is displayed (E.g 4 weeks) :)

I'm trying to highlight weekend days (Saturday and Sunday's) a different color. Any idea how I could do this?

Thanks
 
Actually I do need some help as I've extended the period that is displayed (E.g 4 weeks) :)

I'm trying to highlight weekend days (Saturday and Sunday's) a different color. Any idea how I could do this?

Thanks

Haven't looked at your code in too much detail, but something like:

if(date('D', $i) == 'Sat' || date('D', $i) == 'Sun') {
echo '<div class="weekend">';
} else {
echo '<div class="weekday">';
}

Or something along those lines!
 
Haven't looked at your code in too much detail, but something like:

if(date('D', $i) == 'Sat' || date('D', $i) == 'Sun') {
echo '<div class="weekend">';
} else {
echo '<div class="weekday">';
}

Or something along those lines!


Cheers that worked!

Any idea how i could highlight the current date?
 
You could set an array value to 1 with the key of today's date:

PHP:
$year = date('Y');
$month = date('m');
$day = date('d');
$today["$year-$month-$day"];

Then when looping through you could check to see whether it is set or not:

PHP:
if(isset($today["$year-$month-$day"])) {
    echo '<div class="today">';
} else {
    echo '<div class="not_today">';
}

I am assuming your $year, $month and $day variables are updated (probably only $day) within the loop so that it checks for each date.

Probably a much easier way - but that was off the top of my head.
 
You could set an array value to 1 with the key of today's date:

PHP:
$year = date('Y');
$month = date('m');
$day = date('d');
$today["$year-$month-$day"];

Then when looping through you could check to see whether it is set or not:

PHP:
if(isset($today["$year-$month-$day"])) {
    echo '<div class="today">';
} else {
    echo '<div class="not_today">';
}

I am assuming your $year, $month and $day variables are updated (probably only $day) within the loop so that it checks for each date.

Probably a much easier way - but that was off the top of my head.


Cheers. As I was making the dates withing a loop a quick elseif worked:

PHP:
stuff
}
elseif (date("j", $i) == date("j")) {
    //formating for today's date
}
stuff
 
Back
Top Bottom