Problems with date arrays

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
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Problems with date arrays

Post by lloydie-t »

I am builing a calendar based on a tutorial from web calendar. it seems to be going OK except where there is no records for a month it generates errors saying:
Warning: Wrong datatype for second argument in call to in_array in /var/www/webhosts/web.com/httpdocs/support/desk/calender.php on line 37.
I believe the problem is where there is no records returned for that month either nothing or 0 is being used to compare against the month array. How can I get round this? Do I just use an if/else statement to create a dummy date (ie. 0000-00-00) if a result is not returned? Please find following code.

Code: Select all

<?php 
     include "calender.php"; 
$query = "SELECT dayofmonth(cal_date) as cal_date
FROM cal_entry
WHERE year(cal_date)='$year' and month(cal_date)='$month'"; 

$result = mysql_query ($query); 

while ($row = mysql_fetch_array ($result)) { 

$dateArray[] = $row['cal_date']; 

} 
?>
Calendar function

Code: Select all

<?php
if (in_array($currentDay,$dateArray)) { 
$date = "$year-$month-$currentDay"; 

$calendar .= "<td class='linkedday'> 
<a href='webcal.php?date=$date' 
class='calendarlink'>$currentDay</a></td>"; 

} else { 
$monthday = "$year-$month-$currentDay";
$calendar .= "<td class='day'>
<a href='webcal.php?date=$monthday' 
class='calendarlink'>$currentDay</a></td>"; 

}
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

a $dateArray = array(); before the while-loop should do
laserlight
Forum Commoner
Posts: 28
Joined: Wed Jan 01, 2003 6:41 am

Post by laserlight »

hmm... yet the php manual notes "If $arr doesn't exist yet, it will be created. So this is also an alternative way to specify an array."
lloydie-t
Forum Commoner
Posts: 88
Joined: Thu Jun 27, 2002 3:41 am
Location: UK

Post by lloydie-t »

thanks volka. All sorted now
Post Reply