Jump to content

Recommended Posts

Hi,

 

In the below snippet of code, I'm trying to change sub1 to whatever the amount of results that are being returned. So if there are 5 results I will need to loop 5 times changing sub($i).. so sub1, sub2, sub3 etc.. It needs to be dynamic and not static.

 

Just a little stuck on how I can do this.

 

<?php

while(($row = mysql_fetch_row($result)) != false) 
{

echo "			
<div class='menutitle' onclick=\"SwitchMenu('sub1')\">".$row[1]."</div>
<span class='submenu' id='sub1'>";

populate_ordering_products();

        "</span>";


		 }

}

/>

Link to comment
https://forums.phpfreaks.com/topic/119638-solved-loop-question/
Share on other sites

Like this?

 

$i = 1; //start i at 1
while(($row = mysql_fetch_row($result)) != false) {

echo "<div class='menutitle' onclick=\"SwitchMenu('sub".$i."')\">".$row[1]."</div>
<span class='submenu' id='sub1'>";

populate_ordering_products();
"</span>";
$i += 1; //add one to i.
}

Link to comment
https://forums.phpfreaks.com/topic/119638-solved-loop-question/#findComment-616374
Share on other sites

$x = 1;
while(($row = mysql_fetch_row($result)) != false) 
{

echo "			
<div class='menutitle' onclick=\"SwitchMenu('sub".$x."')\">".$row[1]."</div>
<span class='submenu' id='sub".$x."'>";

populate_ordering_products();
$x++;
        "</span>";

}

Link to comment
https://forums.phpfreaks.com/topic/119638-solved-loop-question/#findComment-616375
Share on other sites

Thanks,

 

I found another solution too by using the primary id in the table.

 

<?php

<?php

while(($row = mysql_fetch_row($result)) != false) 
{

echo "			
<div class='menutitle' onclick=\"SwitchMenu('sub".$row[0]."')\">".$row[1]."</div>
<span class='submenu' id='sub".$row[0]."'>";

populate_ordering_products();

       "</span>";


		 }

}

/>

?>

Link to comment
https://forums.phpfreaks.com/topic/119638-solved-loop-question/#findComment-616376
Share on other sites

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.