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>";

			}

Link to comment
Share on other sites

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++;
}

Link to comment
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.