6 Replies - 592 Views - Last Post: 25 May 2012 - 07:19 AM Rate Topic: -----

#1 mrben2006  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 02-December 06

While in a while loop - Help

Posted 25 May 2012 - 06:15 AM

Hi guys,

This is probably realy simple, but for the life of me I can't understand why this isn't working.

I want this code to loop through 10 times, which it does, but it only displays one part of it once and the rest it does as expected (See screenshot).

The source code shows it does the loop, and creates the 10 table rows, it creates the <select> tags and adds the appropriate names to them, but it only shows the <options> in the first row.

Many thanks,

</ben>


<table>
	<?php
	
	include './includes/dbconnect.php';		//dbconnect.php (Database connection script)
	
	//SQL Query
        $result = mysql_query('SELECT `compID`,`compName` FROM `components` ORDER BY `compName`')  or die (mysql_error());
	
	$i=1;
	while($i<=10)
	{
		echo "
			<tr>
				<td>".$i."</td>
				<td>
				";
				
		echo "<select name=\"comp$i\">
		";
				
		while ($row = mysql_fetch_assoc($result)) 
		{ 
			echo ('<option value="'.$row['compID'].'">'.$row['compName'].'</option>
			'); 
		}
		
		echo "</select>
		        </td>
			</tr>";
		$i++;
	}
	?>
</table>


Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: While in a while loop - Help

#2 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 599
  • Joined: 12-October 09

Re: While in a while loop - Help

Posted 25 May 2012 - 06:22 AM

After the first iteration of your outer while() loop, it has reached the end of $row, so when it comes around to do the next 9 iterations rows, it finds that $row is already at the end of the array, so stops. If you unset the $row variable before you start the innter iteration, it should work, though I haenv't tested it.

unset($row);
while ($row = mysql_fetch_assoc($result)) 
{ 
    echo '<option value="'.$row['compID'].'">'.$row['compName'].'</option>'; 
}


This post has been edited by Duckington: 25 May 2012 - 06:24 AM

Was This Post Helpful? 0
  • +
  • -

#3 mrben2006  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 02-December 06

Re: While in a while loop - Help

Posted 25 May 2012 - 06:47 AM

Hi,

Thanks for the reply, but that doesn't fix it, unless I did it wrong.

<table>
	<?php
	
	include './includes/dbconnect.php';		//dbconnect.php (Database connection script)
	
	//SQL Query
        $result = mysql_query('SELECT `compID`,`compName` FROM `components` ORDER BY `compName`')  or die (mysql_error());
	
	$i=1;
	while($i<=10)
	{
		echo "
			<tr>
				<td>".$i."</td>
				<td>
				";
				
		echo "<select name=\"comp$i\">
		";
		
		unset($row);	
		
		while ($row = mysql_fetch_assoc($result)) 
		{ 
			echo ('<option value="'.$row['compID'].'">'.$row['compName'].'</option>
			'); 
			
		}
		
		echo "</select>
		        </td>
			</tr>";
		$i++;
		
	}
	?>
</table>


Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,599
  • Joined: 08-June 10

Re: While in a while loop - Help

Posted 25 May 2012 - 06:54 AM

once you have fetched data from a DB result, they are (practically) removed from the result set. so neither unsetting nor resetting/rewinding will bring up that fetched data again*. if the options are the same for every (outer) loop cycle, save them in a variable beforehand and insert that each time.




* - thatís why foreach() checks for the Traversable and not the Iterable interface.
Was This Post Helpful? 0
  • +
  • -

#5 mrben2006  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 02-December 06

Re: While in a while loop - Help

Posted 25 May 2012 - 07:11 AM

Hi,

Thanks for your reply. The selects are always the same.

I think I understand your suggestion, but I don't know how to impliment it as what I thought you meant hasn't seemed to work...

<table>
	<?php
	
	include './includes/dbconnect.php';		//dbconnect.php (Database connection script)
	
	//SQL Query
        $result = mysql_query('SELECT `compID`,`compName` FROM `components` ORDER BY `compName`')  or die (mysql_error());
	
	$i = 1;
	while($i <= 10)
	{
		echo "<tr>
				<td>".$i."</td>
				<td>
				";
				
		echo "<select name=\"comp".$i."\">";
		
		unset($row);
		
		$drop = ('<option value="'.$row['compID'].'">'.$row['compName'].'</option>');
		
		while ($row = mysql_fetch_assoc($result)) 
		{ 
			echo $drop;	
		}
		
		echo "</select>
		        </td>
			</tr>";
		$i++;	
	}
	?>
</table>



Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3397
  • View blog
  • Posts: 9,599
  • Joined: 08-June 10

Re: While in a while loop - Help

Posted 25 May 2012 - 07:14 AM

thatís because you have completely misunderstood things. what I meant:
// pseudo code (!!!)
while ($row = fetch($result))
{
    $data .= $row["data"];
}

while ($some_condition)
{
    echo $some_loop_context;

    echo $data;
}

Was This Post Helpful? 1
  • +
  • -

#7 mrben2006  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 02-December 06

Re: While in a while loop - Help

Posted 25 May 2012 - 07:19 AM

View PostDormilich, on 25 May 2012 - 03:14 PM, said:

thatís because you have completely misunderstood things. what I meant:
// pseudo code (!!!)
while ($row = fetch($result))
{
    $data .= $row["data"];
}

while ($some_condition)
{
    echo $some_loop_context;

    echo $data;
}


Awesome, thanks for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1