Jump to content

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 :)

Can you click solved (bottom left)

your need to test in milliseconds, storing in an array will take more memory and is a tad slower Unless you use the array over and over in which case its worth the extra memory as it becomes quicker

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.