Jump to content

for loop of variables


malikah

Recommended Posts

Is there no way you can make this an array and have

$varibl = array();
$varibl[01]="abc";
$varibl[02]="def";
$varibl[03]="ghi";
$varibl[04]="jkl";
$varibl[05]="mno";

 

then you can simply use:

 

foreach($varibl as $value) {
echo $value;
}

 

There is a way to use the eval(); function but thats not very trustworthy coding.

Instead why not just access them via the array index???

 

while ($row = mysql_fetch_row($xyzQuery))
{
    for ($i=0;$i<5;$i++) {
         echo $row[$i];
    }
}

 

Same principle, just easier and more efficient to do....

 

What is your logic behind assigning them to variables and not just using the array index?

Because I don't know what $row is - what happened to the list?

 

The list is not needed, that just broke out the array.

 

$row is an array that contains all the data that list broke out inside of it's self to be accessed as shown above.

 

I would suggest reading up on array's They are like a storage container for data to be easily access/iterated through.

If you want to refer to them by there last digits then you need to take the sub string from the actual variable name.  This, for example, will display all the variables with a 1 at the end of the variable.

 

*NOT TESTED*

 

while ($row = mysql_fetch_row($xyzQuery))
{
    for ($i=0;$i         if(substr($$row[$i], 1) == 1) {
             echo $row[$i];
         }
    }
}

 

or you can access by first letter...

 

$string = $$row[$i];
if($string[1] == 'a') {
    echo $row[$i];
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.