Jump to content

Recommended Posts

Hi Guys,

 

Is there anyway I can loop through a number of drop down lists that read from the database using just one SQL statement?!

 

At the moment only the very first drop down menu is bringing any data back from the db and the others show nothing.

 

$query2 = "select * from employee";
$result2 = mysql_query($query2, $connection) or die ("Unable to perform query $query2");

 

<?php
while($row = mysql_fetch_array($result))
{
?>
  	<tr>
    	<td height="27"><?php echo $row['taskName']?></td>
    	<td><?php echo $row['taskDescription']?></td>
	<td><?php echo $row['weekday']?></td>
	<td>
	<form>
	<select name="empName">
	<option>Select an Employee</option>
	<?php while ($row2 = mysql_fetch_array($result2))
	{ ?><option><?php echo $row2['fName']?> <?php echo $row2['sName']?></option>
	<?php } ?> </select></form></td>
  	</tr>
<?php
}
?>

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/88203-solved-help-please/
Share on other sites

You're missing semi-colons at the end of your echo's in the second while loop.  Also it looks a tad messy, so you could try tidying it up:

 

<?php

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

echo "<option>".$row2['fName']." ".$row2['sName'];

}

?>

 

Ok maybe that still looks as messy xD Nevermind!

Link to comment
https://forums.phpfreaks.com/topic/88203-solved-help-please/#findComment-451383
Share on other sites

Hi,

Thanks for taking a look.

 

I've added semi-colons onto the end of my echo's but it still only displays the data in the first drop down menu and the rest are empty.

 

<?php
while($row = mysql_fetch_array($result))
{
?>
  	<tr>
    	<td height="27"><?php echo $row['taskName']?></td>
    	<td><?php echo $row['taskDescription']?></td>
	<td><?php echo $row['weekday']?></td>
	<td>
	<form action="taskAdminQuery.php">
	<select name="empName">
	<option>Select an Employee</option>
	<?php while ($row2 = mysql_fetch_array($result2))
	{ ?><option value="<?php echo $row['empID']?>"><?php echo $row2['fName'];?> <?php echo $row2['sName'];?></option>
	<?php } ?> </select></td>
	<td><input name="taskID" value="<?php echo $row['taskID']?>" type="hidden" /></td>
  	</tr>
<?php
}
?>

Link to comment
https://forums.phpfreaks.com/topic/88203-solved-help-please/#findComment-451389
Share on other sites

Mysql uses a cursor, that advances every time you use a mysql_fetch_* function. So, after this code is run:

<?php while ($row2 = mysql_fetch_array($result2))

{ ?><option><?php echo $row2['fName']?> <?php echo $row2['sName']?></option>

<?php } ?>

for the first item in the parent loop, the cursor is at the end, and therefore will keep returning false thereafter. Try building the select menu first like so:

 

<?php
//Build Select Menu
$employee_list = '<select name="empName"><option>Select an Employee</option>';
while ($row2 = mysql_fetch_array($result2))
  $employee_list .= '<option>'.$row2['fName'].' '.$row2['sName'].'</option>';
$employee_list .= '</select>';

while($row = mysql_fetch_array($result)) {
?>
  	<tr>
    	<td height="27"><?php echo $row['taskName']; ?></td>
    	<td><?php echo $row['taskDescription']; ?></td>
	<td><?php echo $row['weekday']; ?></td>
	<td>
	<form><?php echo $employee_list; ?></form></td>
  	</tr>
<?php
}
?>

Link to comment
https://forums.phpfreaks.com/topic/88203-solved-help-please/#findComment-451393
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.