trouble with variable scope using for()?

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
bnagle
Forum Newbie
Posts: 1
Joined: Tue May 19, 2009 1:12 am

trouble with variable scope using for()?

Post 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;
}
 
Last edited by Benjamin on Tue May 19, 2009 9:50 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: trouble with variable scope using for()?

Post by Christopher »

Your inner and outer for() loop are both using $i.
(#10850)
Post Reply