Jump to content

Recommended Posts

How can I number a variable within a loop based on another variable?

 

Here is a sample PHP code:

 

foreach ($ar3id as $w) {

foreach ($ar4id as $w2) {

echo '&whatever';

}

}

 

Variable $w is an integer with some values, let's say: 2, 4, 5, 8.

 

Now how can I make a blue 4 a variable $w? What is the syntax?

There might be a way to do this using eval (which I'm myself not too familiar with). I would suggest, instead, using an array whose index would be contained in $w.

 

So for example...

 

$array1[] = 4;
$someArray[4][] = 'what';
$someArray[4][] = 'ever';
foreach ($array1 as $w) {
    foreach ($someArray[$w] as $w2) {
        echo $w2;
    }
}// Would echo: whatever

No, no. That's not what I meant.

 

I would like a number contained in the name of the variable to be dynamic based on the value of another variable.

 

In the loop this would go:

foreach ($ar4id as $w2)

foreach ($ar6id as $w2)

foreach ($ar9id as $w2)

etc.

 

So how do I type: foreach ($ar/$w/id as @w2) {} ????

 

With text I would merge it: 'ar'.$w.'id', but it's a variable within a variable.

I understand what you're asking, I'm merely remarking that (as far as I'm aware) PHP does not support this type of behavior without the use of a function. The eval function might be able to do this for you but I'm not very familiar with it. You can wait for someone else to reply possibly with how to use eval, or you can look up the function yourself.

 

Otherwise, it may be easier to use an array as I've described above.

Php would let you do this using variable-variables, but you already have the data in an array(s). There is nothing wrong with using array variables directly. Why waste the processing time and memory (you would be doubling the amount of memory required unless you unset() the original data), then you must keep track of the variable names you just created. Also, accessing a variable-variable takes three-time longer than accessing an array variable (an important consideration if you have a lot of data to process.)

Php would let you do this using variable-variables, but you already have the data in an array(s). There is nothing wrong with using array variables directly. Why waste the processing time and memory (you would be doubling the amount of memory required unless you unset() the original data), then you must keep track of the variable names you just created. Also, accessing a variable-variable takes three-time longer than accessing an array variable (an important consideration if you have a lot of data to process.)

 

I tried to PM you but couldn't. Is there a PHP manual entry on variable-variables? I've not seen this before in PHP and I'm quite curious.

 

Question answered, thanks.

OK, here is full code. I want to present categories and links within each category.

 

My problem is that somehow in every category the links contain all the previous categories and the new ones. I think there is something wrong with the inner loop, that's why I was thinking to impose a variable within a variable.

 

Perhaps there is an easier way?

 

<?php

$q3 = ("Select id, cat from cat order by id");

$r3 = mysql_query ($q3, $db);

while ($row3 = mysql_fetch_array($r3, MYSQL_ASSOC)) {

$ar3id[$row3['id']] = $row3['id'];

$ar3cat[$row3['id']] = $row3['cat'];

}

$num3 = mysql_num_rows($r3);

 

echo '<table style="BORDER-COLLAPSE: collapse" border=0 cellSpacing=0

borderColor=#000000 bgColor=#000000 cellPadding=3>';

// Loop for a Category

foreach ($ar3id as $w) {

echo '<tr valign="top"><td>';

echo '<basefont color="#FFFFFF">';

echo $ar3cat[$w];

echo '</basefont>';

echo '</td></tr>';

 

$q4 = ("Select * from kkk where cat = '".$ar3cat[$w]."' order by id");

$r4 = mysql_query ($q4, $db);

while ($row4 = mysql_fetch_array($r4, MYSQL_ASSOC)) {

$ar4id[$row4['id']] = $row4['id'];

$ar4d[$row4['id']] = $row4['d'];

$ar4l[$row4['id']] = $row4['l'];

$ar4cat[$row4['id']] = $row4['cat'];

$ar4com[$row4['id']] = $row4['com'];

$ar4sta[$row4['id']] = $row4['sta'];

}

$num4 = mysql_num_rows($r4);

 

// Loop for lines within a Category

foreach ($ar4id as $w2) {

echo '<tr valign="top"><td>';

echo '   <A href="'.$ar4l[$w2].'">'.$ar4d[$w2].'</A><br>';

echo $q4.' '.$ar4d[$w2].' '.$ar4id[$w2];

echo '</td>';

echo '</tr>';

}

}

echo '</table>';

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.