Jump to content

Using a query twice


thecase

Recommended Posts

Hi,

 

This is my code

 

$query = mysql_query("SELECT id, username, lastname FROM users ORDER BY username ASC"); 


echo '<h1>Add Rota</h1>
<table>
<tr>
<td><b>Presenter 1:</b></td>
<td>

<select name="presenter1">
  <option value="0">Pick presenter</option>
  ';


      while ($row = mysql_fetch_array($query, MYSQL_NUM))
          {
          echo "<option value=\"{$row['0']}\">{$row['1']} {$row['2']} </option>\n";
          }
        
echo '
  
  </select>	</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td><b>Presenter 2:</b></td>
<td>

	<select name="presenter2">
  <option value="0">Pick presenter</option>
  ';

      while ($row = mysql_fetch_array($query, MYSQL_NUM))
          {
          echo "<option value=\"{$row['0']}\">{$row['1']} {$row['2']} </option>\n";
          }
echo '

</td>

 

I am trying to define the $query to use in a dropdown twice although I cannot re use it and this doesnt work for the second dropdown. I was trying to only write the query out once to save on resources. Any suggestions would be great

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/209935-using-a-query-twice/
Share on other sites

Form (concatenate) the entire <option> ... </option> list in a variable, then echo the variable any place you want it or if your whole <select> ... </select> menu is the same each time, form the whole <select> ... </select> menu in a variable and echo the variable any place you want it.

Link to comment
https://forums.phpfreaks.com/topic/209935-using-a-query-twice/#findComment-1095753
Share on other sites

Do you mean something like this

 

$dropdown = " while ($row = mysql_fetch_array($query, MYSQL_NUM))
          {
          <option value={$row['0']}>{$row['1']} {$row['2']} </option>\n
          }  </select>	
        ";

 

It comes back with Notice: Undefined variable: row

Link to comment
https://forums.phpfreaks.com/topic/209935-using-a-query-twice/#findComment-1095764
Share on other sites

No what they mean is something on the lines of this:

$query = mysql_query("SELECT id, username, lastname FROM users ORDER BY username ASC"); 
echo '<h1>Add Rota</h1>
<table>
<tr>
<td><b>Presenter 1:</b></td>
<td>
<select name="presenter1">
  <option value="0">Pick presenter</option>
  ';


      while ($row = mysql_fetch_array($query, MYSQL_NUM))
          {
          $options[] = "<option value=\"{$row['0']}\">{$row['1']} {$row['2']} </option>\n";
          }
	  
	  foreach($options as $v) {
		echo $v;
		}
        
echo '  
  </select>
</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td><b>Presenter 2:</b></td>
<td>
<select name="presenter2">
  <option value="0">Pick presenter</option>
  ';

     reset($options);
 foreach($options as $v) {
	echo $v;
}

echo '
</td>

Link to comment
https://forums.phpfreaks.com/topic/209935-using-a-query-twice/#findComment-1095766
Share on other sites

Please do not follow the last persons post. You will have more time processing it than you need to. He is essentially doing 3 loops and messing allocating memory for arrays. My solution just puts it into one variable and only does one loop. This is what the original poster actually meant:

 

<?php
$query = mysql_query("SELECT id, username, lastname FROM users ORDER BY username ASC"); 

//setup options to hold the options;
$options = '';

while ($row = mysql_fetch_array($query, MYSQL_NUM)){
$options .= "<option value=\"{$row['0']}\">{$row['1']} {$row['2']} </option>\n";
}

?>

<h1>Add Rota</h1>
<table>
<tr>
	<td><b>Presenter 1:</b></td>
	<td>
		<select name="presenter1">
  				<option value="0">Pick presenter</option>
			<?php echo $options; ?>  
			</select>
	</td>
	<td></td>
	<td></td>
	<td></td>
</tr>
<tr>
	<td><b>Presenter 2:</b></td>
	<td>
		<select name="presenter2">
  				<option value="0">Pick presenter</option>
			<?php echo $options; ?>
		</select>
	</td>
</tr>
</table>

Link to comment
https://forums.phpfreaks.com/topic/209935-using-a-query-twice/#findComment-1095771
Share on other sites

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.