angerbeaver Posted November 20, 2007 Share Posted November 20, 2007 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, Quote Link to comment https://forums.phpfreaks.com/topic/78098-mysql_fetch_rowresult-in-array/ Share on other sites More sharing options...
MadTechie Posted November 20, 2007 Share Posted November 20, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/78098-mysql_fetch_rowresult-in-array/#findComment-395238 Share on other sites More sharing options...
angerbeaver Posted November 20, 2007 Author Share Posted November 20, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/78098-mysql_fetch_rowresult-in-array/#findComment-395253 Share on other sites More sharing options...
MadTechie Posted November 20, 2007 Share Posted November 20, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/78098-mysql_fetch_rowresult-in-array/#findComment-395264 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.