|
-
Apr 9th, 2007, 09:47 AM
#1
Thread Starter
Member
infinite loop!! == Whitsend!!
ok i am working on an event and calendar system for a site, very basic however ia am trying to create a function that will take all the events in the database and create an array with values for each day of the month. Basicaly have a 1 if there is an event that day and a 0 if not. however with the code that i have i seem to be getting an infinite loop with the section that fills the array with zero values for each day in the month
here is my code
Code:
function fSetEventOn($vResults,$vMonth,$vYear){
global $vEvents,$vDay;
$temp1 = getdate(mktime(0,0,0,$vMonth+1,0,$vYear));
$numdays=$temp2['mday'];
$vEvents = array();
$vDay = array();
$i=0;
while($row = mysql_fetch_array($vResults)){
if($row['event_year']==$vYear && $row['event_month']==$vMonth){
$vDay[$i]=$row['event_day'];
}
$i++;
}
//cause of the infinite loop, runs fine if this for loop is // out
for($i=1;$i<=$numdays['mday'];$i++){
$vEvents[$i]=0;
}
for($i=0;$i<=sizeof($vDay);$i++){
$vEvents[$Day[$i]] = 1;
}
return $vEvents;
}
-
Apr 9th, 2007, 12:59 PM
#2
Re: infinite loop!! == Whitsend!!
the array "$temp2" is never defined in your function -- therefor, $numdays['mday'] doesn't even exist. 1 or above will never be below or equal to zero.
Last edited by kows; Apr 9th, 2007 at 01:02 PM.
-
Apr 9th, 2007, 01:07 PM
#3
Re: infinite loop!! == Whitsend!!
Pop this at the top of your code.
PHP Code:
error_reporting(E_ALL ^ E_STRICT);
And fix any notices/warnings.
-
Apr 11th, 2007, 01:42 PM
#4
Thread Starter
Member
Re: infinite loop!! == Whitsend!!
wow thanks i new it was something simple
and thanks for the tip pengate! one quick question though what does this mean
Notice: Undefined offset: 1
does it not mean that the array has noting declared at that value. because i get one for each day when i try and pass the array to be printed?
-
Apr 11th, 2007, 04:21 PM
#5
Re: infinite loop!! == Whitsend!!
Correct. You've initialised $vEvents to an empty array, but you've not filled it with any values before trying to access it in the loop.
I suggest you have a rethink of your code structure so that it flows more nicely and makes sense. 
Also, get in the habit of indenting and spacing things more.
Bad:
PHP Code:
while($row = mysql_fetch_array($vResults)){
if($row['event_year']==$vYear && $row['event_month']==$vMonth){
$vDay[$i]=$row['event_day'];
}
$i++;
}
Good:
PHP Code:
while ($row = mysql_fetch_array($vResults)) {
if ($row['event_year'] == $vYear && $row['event_month'] == $vMonth) {
$vDay[$i] = $row['event_day'];
}
$i++;
}
That way, it is far easier to see at a glance which statement belongs at which level, and this will ease maintenance and debugging. In a professional environment unindented code is not accepted so you should get in to the habit now.
-
Apr 12th, 2007, 07:20 AM
#6
Thread Starter
Member
Re: infinite loop!! == Whitsend!!
ok i still do not understand i am filling the array with something yet it still gives me the same error when ever i try to pint from the array. all of this is done before i print so the array should have the data in it?
Code:
$vEvent = array();
$vD = array();
$vM = array();
$vY = array();
$vId = array();
$vTitle = array();
$i=0;
while($row = mysql_fetch_array($vResults)){
$vD[$i]=$row['event_day'];
$vM[$i]=$row['event_month'];
$vY[$i]=$row['event_year'];
$vId[$i]=$row['event_id'];
$vTitle[$i]=$row['event_title'];
$i++;
}
$y=1;
for($i=1;$i<=$days['mday'];$i++){
$vEvent[$i] = '<ul>';
for($x=0;$x<sizeof($vD);$x++){
if($vY[$x]==$vYear && $vM[$x]==$vMonth && $vD[$x]==$i){
$vEvent[$i].='<li>';
$vEvent[$i].='<a href="" onclick="event-'.$y.'.style.visibility=\'visible\';">';
$vEvent[$i].=$vTitle[$x];
$vEvent[$i].='</a>';
$vEvent[$i].='</li>';
$y++;
}
}
$vEvent[$i] .= '</ul>';
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|