skolind
skolind

Reputation: 1754

Calendar script PHP

Im trying to create a calendar application.

I've got a problem with the way to echo dates WITH an event.

I've tried this:

$nuvarende_maned=date("m");
$sql=("SELECT * FROM kalender WHERE begivenhed_maned='$nuvarende_maned'");
$result=mysql_query($sql);
$count=mysql_num_rows($result);
$row=mysql_fetch_array($result);

echo "<div id='kalender'>";

for($a=1;$a<=date("t");$a++){

$dag_m_begivenhed = $row['begivenhed_dag'];

if($count>=1 && $dag_m_begivenhed == $a){
    echo "<div class='kalender_dag'><a title='$row[begivenhed_overskrift]' href=''>".$a."</a></div>";
} else {
    echo "<div class='kalender_dag'>".$a."</div>";
}
}

echo "</div>";

It's printing one date with an event. I need some way if creating a while() loop. But I can't figure out where to place it. I've tried a while() inside of the for() but it doesn't work. I know I have the $row=mysql_fetch_array($result); in the beginning and that's why I would always only have the first match from the database. I just did this to see if something came out of the DB.

Upvotes: 0

Views: 213

Answers (1)

Marc B
Marc B

Reputation: 360572

You'll have to fetch all of the calendar events BEFORE going into your calendar generation loop. Right now, you only fetch the first row of the query results, and throw away the rest (mysql_fetch_array returns a single ROW as an array, not the whole result set).

$events = array();

$sql = "SELECT * FROM kalender WHERE begivenhed_maned='$nuvarende_maned'";
$result = mysql_query($sql) or die(mysql_error());

while($row = mysql_fetch_assoc($result)) {
   $events[$row['begivenhed_dag']] = $row;
}

for ($a = ...) {
   if (isset($events[$a])) {
       ... output whatever parts of $events[$a] you want
   }
}

Note that this doesn't handle multiple events in a single day. If you need that capability, then you'll have to modify the database fetch loop a bit, so it stores those multiple events in a sub-array.

Upvotes: 1

Related Questions