Forum Moderators: coopster

Message Too Old, No Replies

storing a while loop in a variable

         

supermanjnk

8:28 pm on May 17, 2005 (gmt 0)

10+ Year Member



Is it possible to store a while loop in a variable?

for instance

$while = while ($this = $that) {
echo "test";
}

StupidScript

8:34 pm on May 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not the loop itself, but you can use the loop to collect its "output":

while($this==$that) {

$result++;

$loop .= $result.",";

}

echo $loop;

dreamcatcher

10:20 pm on May 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can also assign the data to an array for later manipulation:

$loop = array();

while($this==$that) {
$result++;
$loop[] = $result.",";
}

echo $loop[0];

dc

webkami

4:20 pm on May 18, 2005 (gmt 0)

10+ Year Member



or maybe you can tell what do you expect the while loop to return and sombody will tell you another way of getting that result.....

StupidScript

6:36 pm on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



supermanjnk, are you trying to set up a page with a bunch of data-dependent while loops, and then grab the appropriate one for use in a more generic page? Like using a stored procedure in SQL?

ergophobe

1:17 am on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think SS gave you your answer in the first post, but if the answer to his last question is "yes", then in fact, you can store a while loop in a variable.


$while_loop = '
while ($this == $that)
{
echo "test" ;
} ' ;
eval($while_loop);

Note that you probably want to use the comparison op (==) and not the assignment op (=) in your while loop.

supermanjnk

3:26 am on May 19, 2005 (gmt 0)

10+ Year Member



Yea I will actually be using
$this = mysql_fetch_object($that)