Page 1 of 1

trouble with variable scope using for()?

Posted: Tue May 19, 2009 1:20 am
by bnagle
Im guessing at my subject line, I don't really know why this won't work and I've been banging my head against it for some time. Thanks ahead of time for any help.

I'm trying to loop through an array of fetched rows from a database. Each time through the loop's looking for a specific date if it finds it, it prints the event. If a event is not found for the date it's supposed to print another pattern (the &lattice), add a days worth of seconds to the search date and do it again. Right now the search date ($current) will not update.

Here's what I have:

Code: Select all

 
$events_res=array();
$current = time();
 
while($events_row = mysql_fetch_assoc($events)){
    $events_res[]=$events_row;
    }
    
for($i=0; $i<7; $i++){
        $lattice = TRUE;
        
        for($i=0; $i<count($events_res); $i++){
            $current_f = date("n d",$current);
            $compare = $events_res[$i][month]." ".$events_res[$i][day];
        
                if ($current_f == $compare) {
            $evt_date = mktime(0,0,0,$events_res[$i][month],$events_res[$i][day],0);
            echo "
            <div class=\"event\">
            <div id=\"day\">".strtoupper(date("M d",$evt_date))."</div>
            <a href=\"dj.html\">".$events_res[$i][name]."</a>
            <p>".$events_res[$i][desc]."</p>
            </div>";
            $lattice = FALSE;
            }
        
        if($lattice&&$i=count($events_res)-1){ 
            echo "<div class=\"no_event\">
            <div id=\"day\">day</div>
            </div>";
            }
        } 
        $current+=86400;
}
 

Re: trouble with variable scope using for()?

Posted: Tue May 19, 2009 2:36 am
by Christopher
Your inner and outer for() loop are both using $i.