Jump to content

[SOLVED] A for loop inside a while loop - Is it possible?


tqla

Recommended Posts

Hello, I wrote this to pull data from a database. I need to change the "showid" number on each loop ($f).  What I am having trouble with is getting the value of $f to start at 1 and increment by 1 with every loop. As it is it shows 9999 for every loop. How can I get the for loop to work within the while loop? Thanks!

 

$sql = "SELECT * FROM $content WHERE section_group = '$grp' ORDER BY position";
  		$questions2 = mysql_query($sql) or die (mysql_error());

			while($row2 = mysql_fetch_array($questions2)) {

// This is the showid value
for ($i = 0; $i < 10000; $i++) {
$f = $i;
}


			echo "<div style=\"text-align:left;font-weight:bold;cursor:pointer\" class=\"home_cat_nav\" onclick=\"showid('";
			echo $f;
			echo "');\">";
			echo $row2['title'];
			echo "</div>";

			}

So you just want a counter inside the while loop?

 

set a variable before the loop starts and then incrememnt inside

$showid = 1
while($row2 = mysql_fetch_array($questions2)) 
{
      //loop code here

      //increment counter
      $showid++;
}

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.