sayedsohail Posted May 4, 2007 Share Posted May 4, 2007 Hi, I wish to print five rows even if the fetched records are less than 5. here is my code: ?><table><tr> <? while(list($id, $name) = mysql_fetch_array($sql)) { print "<td><input type ='radio' value= '$id'></td>"; print"<td>$name</td></tr>"; } echo "</table>"; Link to comment https://forums.phpfreaks.com/topic/50050-solved-print-five-rows-even-if-records-are-less-than-5/ Share on other sites More sharing options...
chronister Posted May 5, 2007 Share Posted May 5, 2007 Do you mean that you want to repeat the list if need be? e.g. Query returns: id | name 1 bob 2 steve 3 jill You want the table to print out as <table> <tr> <td><input type ='radio' value= '1'></td> <td>bob</td> </tr> <tr> <td><input type ='radio' value= '2'></td> <td>steve</td> </tr> <tr> <td><input type ='radio' value= '3'></td> <td>jill</td> </tr> <tr> <td><input type ='radio' value= '1'></td> <td>bob</td> </tr> <tr> <td><input type ='radio' value= '2'></td> <td>steve</td> </tr> </table> Is this what your after? Nate Link to comment https://forums.phpfreaks.com/topic/50050-solved-print-five-rows-even-if-records-are-less-than-5/#findComment-245958 Share on other sites More sharing options...
sayedsohail Posted May 5, 2007 Author Share Posted May 5, 2007 i am able to repeat the list from sql statement, I wish to print blank tr td even if the records are less than 5 from my sql statement. someone has suggested code below, but its giving an error <?php for($i=0;$i<5;++$i) { if while(list($id, $name) = mysql_fetch_array($sql)) { echo "<tr><td><input type ='radio' value= '$id'></td>"; echo "<td>$name</td></tr>"; } else { echo '<tr><td></td><td></td></tr>'; } } ?> Link to comment https://forums.phpfreaks.com/topic/50050-solved-print-five-rows-even-if-records-are-less-than-5/#findComment-246088 Share on other sites More sharing options...
Barand Posted May 5, 2007 Share Posted May 5, 2007 it was close <?php for($i=0;$i<5;++$i) { if (list($id, $name) = mysql_fetch_row($sql)) { echo "<tr><td><input type ='radio' value= '$id'></td>"; echo "<td>$name</td></tr>"; } else { echo '<tr><td> </td><td> </td></tr>'; } } ?> Link to comment https://forums.phpfreaks.com/topic/50050-solved-print-five-rows-even-if-records-are-less-than-5/#findComment-246106 Share on other sites More sharing options...
sayedsohail Posted May 5, 2007 Author Share Posted May 5, 2007 You have taken off the while (list), i need the while statement with (list), please suggest. something. Link to comment https://forums.phpfreaks.com/topic/50050-solved-print-five-rows-even-if-records-are-less-than-5/#findComment-246117 Share on other sites More sharing options...
nita Posted May 5, 2007 Share Posted May 5, 2007 tak a look at the structure of this script, im sure you will find an answer: define ("NUMCOLS",4); $res = mysql_query("SELECT name, cover FROM movies ORDER BY id DESC LIMIT 4"); $count = 0; echo "<TABLE>"; while (list($name, $cover) = mysql_fetch_row($res)) { if ($count % NUMCOLS == 0) echo "<TR>\n"; # new row echo "<TD>$name<br>$cover</TD>\n"; $count++; if ($count % NUMCOLS == 0) echo "</TR>\n"; # end row } [color=red]# end row if not already ended if ($count % NUMCOLS != 0) { while ($count++ % NUMCOLS) echo "<td> </td>"; echo "</TR>\n"; } echo "</TABLE>";[/color] in red is what you after! nita Link to comment https://forums.phpfreaks.com/topic/50050-solved-print-five-rows-even-if-records-are-less-than-5/#findComment-246141 Share on other sites More sharing options...
Barand Posted May 5, 2007 Share Posted May 5, 2007 You have taken off the while (list), i need the while statement with (list), please suggest. something. Have you even tried out the code I posted yet? Link to comment https://forums.phpfreaks.com/topic/50050-solved-print-five-rows-even-if-records-are-less-than-5/#findComment-246206 Share on other sites More sharing options...
sayedsohail Posted May 5, 2007 Author Share Posted May 5, 2007 Thank you barand me bad, it worked perfectly with little changes for($p=0;$p<3;++$p) { if(list($id, $name, $address_1, $p_code, $city, $l_line, $mobile) = mysql_fetch_array($sql)) { echo "<tr><td><input type ='radio' value= '$id'></td>"; echo "<td>$name</td>"; echo "<td>$address_1</td>"; echo "<td>$p_code</td>"; echo "<td>$city</td>"; echo "<td>$l_line</td>"; echo "<td>$mobile</td></tr>"; } else { echo "<tr><td colspan='7' align='center'> </td></tr>"; } } Link to comment https://forums.phpfreaks.com/topic/50050-solved-print-five-rows-even-if-records-are-less-than-5/#findComment-246293 Share on other sites More sharing options...
Barand Posted May 5, 2007 Share Posted May 5, 2007 I thought you wanted 5 rows for($p=0;$p<3;++$p) only gives 3 Link to comment https://forums.phpfreaks.com/topic/50050-solved-print-five-rows-even-if-records-are-less-than-5/#findComment-246296 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.