Click to See Complete Forum and Search --> : PHP - Nesting Loops?


FunkyBlueStick
03-20-2001, 08:05 PM
I'm trying to write a script that uses info from a calendar database and displays the next 7 days events, day by day. I've managed to get it to display the seven days but I can't get it to display the SQL data? Can someone have alook at this script and tell me what you think is wrong?

// Open SQL Connection
$db="udubs";
$table="calendar";
mysql_pconnect("db.clanunwanted.f2s.com","*****","*****")
or die("Could not connect to the database server... Please try later!");

// Loop to display next 7 days
for ($counter=0; $counter<=7; $counter++){
echo ("<font size=-2>".gmdate("D d M Y", mktime(0,0,0, gmdate(m), gmdate(d)+$counter, gmdate(Y)))."</font>");
echo ("<br>");
$date = gmdate("Y-m-d", mktime(0,0,0, gmdate(m), gmdate(d)+$counter, gmdate(Y)));
echo ("<font size=-2>$date</font>");
echo ("<br>");

// Make SQL Query
$sql = "select * from $table where date = $date order by time";
$result = mysql_db_query($db,$sql);
$numrows = mysql_num_rows($result);
if (!$numrows == 0){
echo ("<font size=-2><i>Nothing Scheduled</i><br></font>");
}else{
while ($row = mysql_fetch_array($result)){
$worktime = $row[time];
$time = substr($worktime,0,5);
echo ("<font size=-2>$time - ".$row[comment]."<br></font>");
}
}
echo ("<br>");
}

Thanks in advance...if you want more info give me a shout :)

You can see the output of this at http://www.clanunwanted.f2s.com/new/dates.php

Funkster
ICQ: 62038286

[ 20 March 2001: Message edited by: FunkyBlueStick ]

Salmon
03-21-2001, 09:43 AM
This is just at first glance, but shouldn't this . . .
if (!$numrows == 0) <HR></BLOCKQUOTE>
. . . . be this . . .
// remove the '!'
if ($numrows == 0)

The way you currently have it it will display the empty schedule message if there are results.

[ 21 March 2001: Message edited by: Salmon ]

FunkyBlueStick
03-21-2001, 02:56 PM
Ok, you spotted the deliberate mistake :} Did you check out the out put that gave though. It just displayed the dates and nothing else, not even the 'Nothing Scheduled' message. I'm guessing that it's something todo with the nested while loop but I am just guessing! There are definatley values in the DB for those dates. Please can someone help, this is really bugging me and I must defeat it!!! :)

Cheers

Raskii
03-21-2001, 03:59 PM
I don't know SQL yet and I'm not at my server to check for parse errors, but in here:

else
{
while ($row = mysql_fetch_array($result))
{
$worktime = $row[time];
$time = substr($worktime,0,5);
echo ("<FONT SIZE=\"-2\">$time - " . $row[comment] . "</FONT><BR>");
}
}

shouldn't you be checking for equality instead of assigning a new value to $row?

FunkyBlueStick
03-21-2001, 04:44 PM
Not to worry chaps, I got it working :)
Here's what I did in case anyone's intereseted.....

<?php
// Open connection to MySQL Database
$db="udubs";
$table="calendar";
mysql_pconnect("db.clanunwanted.f2s.com","udubs","c0me0n")
or die("Could not connect to the database server... Please try later!");

// Loop to display next 7 days
for ($counter=0; $counter<=7; $counter++){
echo ("<table width=100 align=center bgcolor=#2b2b5c cellspacing=0 cellpadding=2 class=borderlight>");
echo ("<tr><td width=20% bgcolor=#2b2b5c align=right colspan=2><font size=-2><i>".gmdate("D d M Y", mktime(0,0,0, gmdate(m), gmdate(d)+$counter, gmdate(Y)))."</i></font></td></tr>");

// Format date so it works with MySQL
$origdate = gmdate("Y-m-d", mktime(0,0,0, gmdate(m), gmdate(d)+$counter, gmdate(Y)));
$olddate = $origdate; $stringArray = explode("-", $olddate);
$workdate = mktime(0,0,0,$stringArray[1],$stringArray[2],$stringArray[0]);
$year = $stringArray[0];
$month = $stringArray[1];
$day = $stringArray[2];

// Make SQL Statement
$sql = "select * from $table where date = '".$year."-".sprintf("%02d", $month)."-".sprintf("%02d", $day)."' order by time";
$result = mysql_db_query($db,$sql);
$numrows = mysql_num_rows($result);
if (!$numrows == 0){
while ($row = mysql_fetch_array($result)){
$worktime = $row[time];
$time = substr($worktime,0,5);
echo ("<tr><td width=20% bgcolor=#2b2b5c align=right><font size=-2>$time</font></td><td width=* bgcolor=#2b2b5c align=left><font size=-2>".$row[comment]."</font></td></tr>");
}
}else{
echo ("<tr><td width=20% bgcolor=#2b2b5c align=center colspan=2><font size=-2 color=#cc0000><i>Nothing Scheduled</i></font></td></tr>");
}
echo ("</table>");
}
?>

You can see the output Here (http://www.clanunwanted.f2s.com/new/dates.php)

Thanks to all those who replyed :) You're all wonderful people!

Funkster
ICQ: 62038286