Help - Search - Members - Calendar
Full Version: for in a foreach loop
Invision Power Services > Community Forums > Community Web Design and Coding
primetime
Hey coding gurus!

I need some help in figuring something out. In one of my scripts, I run 3-4 for loops each having a different condition set in them.

I've decided to save on some time that I'll store the conditions in an array and run it through a foreach loop that then runs each of the conditions through a for loop.

Unfortunately, I'm having problems in storing the conditions in the array because when I run it through the for loop it gives me a parse error. Here's a basic example of what I want to do...

CODE
<?php

// Sample code

     $condition['first'] = array("$i = 0; $i < 12; $i++", "echo $i;");
     $condition['second'] = array("$cellcount = 0; $cellcount % $somevariable != 12; $cellcount++", "echo "It should print out the cell count".$cellcount);
     $condition['etc'] = etc....
    
     foreach($condition as $key => $array)
     {
         list($for_value, $for_output) = $array;
         for($for_value)
         {
             $for_output;
             $somevariable++;
         }
     }
?>


Any suggestions on how to go about doing this? Like how I should store my conditions in the array or something? My brain's just fried and I need an outsider's opinion on how to go about this original.gif

Thanks
phatmonkey
The parameters for "for" can't be a string! It is a statement which can't be executed from a string without eval, which I wouldn't suggest you do.
primetime
I already tried passing it through eval() and it still gives me a parse error.. something about having ";" where it shouldn't be..
phatmonkey
You can fairly easily do things like that with while, and that will probably make your job easier.

Your code in general is flawed. You will need to evaluate the code at some point, which should be avoided if possible. $for_output needs to be evaluated, "$for_output;" is doing absolutely nothing, and I wouldn't be surprised if that's causing your parse error. A line number would be useful.

You also have a syntax error with your quotes on the second array entry definition. Try matching them and you'll see the problem!
princetontiger
His parse error is because he has a string in the for loop. I tried using an eval, I even echo'd it for debugging, but it still didn't work.
primetime
Hehe.. well I guess I should post my code here so that you can see original.gif

I just whipped up that code in my first post just to make it clear what I want to do.. just incase my explaination didn't make sense. I finally got back to the computer I was coding on so I'll be able to post up my original code..

CODE
// The array that stores what will happen in the for loop
             $table['weekdays'] = array("0", "$i < 7", "substr($this->weekdays[$i], 0, 3)");
             $table['previous'] = array("0", "$i < $this->get_month_day(1, $month, $year)", "$prev_blank");
             $table['main'] = array("1", "$i <= $this->get_num_days($month, $year)", "$i");
             $table['next'] = array("1", "$cell % 7 != 0", "$i");


And... the foreach loop that iterates through the values in the array

CODE
             foreach($table as $key => $conditions)
             {
                 list($initial, $condition, $out) = $conditions;
                 for($i = $initial; eval($condition); $i  )
                 {
                     $this->output .= "tt<td {%".$key."%}>".eval("$out = "$out";")."</td>n";
                     $cell;
                 }
             }


And whoops I forgot to put in the errors
CODE
Notice: Undefined property: get_month_day in this.php on line 90

Notice: Undefined property: get_num_days in this.06.php on line 91

Parse error: parse error in this.php(101) : eval()'d code on line 1

Parse error: parse error in this.php(101) : eval()'d code on line 1

Parse error: parse error in this.php(101) : eval()'d code on line 1

Parse error: parse error in this.php(101) : eval()'d code on line 1
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.