Jump to content

mysql_fetch_row($result) in array


angerbeaver

Recommended Posts

Hi,

 

I'm generating 20 rows of textboxes in PHP. Some textboxes are pre-filled with information from mysql table.

 

$sSQL = "SELECT prodnum, description FROM products WHERE discontinued=0 ORDER BY prodnum ";
$result = mysql_query($sSQL) or die();

for ($r=0; $r<20; $r++)
{
   /* regular textboxes work so I know connection is good and getting right information */
   
      print '<select name="text' . $r . "3" . '">';

  while($row=mysql_fetch_row($result))
  	print  '<option value=' . $row[0] . '>' . $row[0] . "      " . $row[1] . '</option>';


  print '</select>';
/* etc etc
}

 

I need that select box to be filled with all the products for each row created. It only fills in once and the rest of the selects are blank. I'm assuming it is because $row is not empty or something? Anyone have some ideas?

 

Thanks,

Link to comment
https://forums.phpfreaks.com/topic/78098-mysql_fetch_rowresult-in-array/
Share on other sites

Try this

 

<?php
$sSQL = "SELECT prodnum, description FROM products WHERE discontinued=0 ORDER BY prodnum ";
$result = mysql_query($sSQL) or die();

//Put rows into an array
$list = array();
while($row=mysql_fetch_row($result))
$list[] = $row;

for ($r=0; $r<20; $r++)
{
   /* regular textboxes work so I know connection is good and getting right information */
   
      print '<select name="text' . $r . "3" . '">';
            //Reuse the array
  foreach($list as $row)
  {
  	print  '<option value=' . $row[0] . '>' . $row[0] . "      " . $row[1] . '</option>';
  }

  print '</select>';
// etc etc
}
?>

 

*untested

 

**Comments added

Hm, that worked jst great. I'm surprised though because I thought the way above would be faster than the original code:

 

$sSQL = "SELECT prodnum FROM products WHERE discontinued=0 ORDER BY prodnum ";
$result = mysql_query($sSQL) or die();
$sSQL = "SELECT description FROM products WHERE discontinued=0 ORDER BY prodnum ";
$result2 = mysql_query($sSQL) or die();


for ($r=0; $r<20; $r++)
{
  print '<tr>';
  for ($c=0; $c<7; $c++)
  {
   if ($c == 0){
      print '<td align="center">';
      print '<input type="text" name="text' . $r . $c . '" size="36">';
      print '</td>';
   }
   if ($c == 1){
      print '<td align="center">';
      print '<input type="text" name="text' . $r . $c . '" size="20">';
      print '</td>';
   }
   if ($c == 2){
      print '<td align="center">';
      print '<input type="text" name="text' . $r . $c . '" size="3">';
      print '</td>';
   }
   if ($c == 3){
   	  print '<td align="center">';
      print '<select name="text' . $r . $c . '">';
  for ($i=0; $i < mysql_num_rows($result); $i++)
  {
  	print  '<option value=' . mysql_result($result, $i) . '>' . mysql_result($result, $i) . "      " . mysql_result($result2, $i) . '</option>';
  }
  print '</select>';
      print '</td>';
   }
   if ($c == 4){
      print '<td align="center">';
      print '<input type="text" name="text' . $r . $c . '" size="7" disabled>';
      print '</td>';
   }
   if ($c == 5){
      print '<td align="center">';
      print '<input type="text" name="text' . $r . $c . '" size="7" disabled>';
      print '</td>';
   }
  }
  print '</tr>';
}

 

but it really seems to be about the same to me. Ah well, it was worth a try. Thanks again :)

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.