jch02140 Posted September 23, 2011 Share Posted September 23, 2011 I am trying to do something like this but the code I have doesn't seem to do what I want... <?php while($item_rows = mysql_fetch_array($item_result)) { $output = " <tr bgcolor=\"#000000\"> <td align=\"center\"><p><img src=\"../images/".$item_rows['item_id'].".gif\" width=\"45\" height=\"45\" /></p> <p>".$item_rows['name']."<br /> Attack: +".$item_rows['attack']."<br /> <img src=\"../images/credit.gif\" width=\"17\" height=\"17\" />$".$item_rows['sell_price']."</p> <p>[Equipt|Destory]<br /> </p> </td>"; $remainder = $i % 5; echo ($remainder == 0 ? ($i == 0 ? "" : "\n</tr>")."\n$output " : $output); } $colspan = (4 - $remainder); $last_row = $colspan ? " <td ".($colspan? "colspan=$colspan" : "")."></td>" : ""; echo $last_row. " </tr> </table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/247716-setting-of-items-per-row-from-database/ Share on other sites More sharing options...
Buddski Posted September 23, 2011 Share Posted September 23, 2011 You appear to be overwriting the $output variable with each iteration of your loop.. Change the $output = " to $output .= " and see what you get. Quote Link to comment https://forums.phpfreaks.com/topic/247716-setting-of-items-per-row-from-database/#findComment-1272045 Share on other sites More sharing options...
jch02140 Posted September 23, 2011 Author Share Posted September 23, 2011 The records are displaying all vertically. Quote Link to comment https://forums.phpfreaks.com/topic/247716-setting-of-items-per-row-from-database/#findComment-1272065 Share on other sites More sharing options...
Buddski Posted September 23, 2011 Share Posted September 23, 2011 You will need to put your first 'tr' tag outside of the loop.. Once you have closed a tr you will have to check if another one needs opening.. Quote Link to comment https://forums.phpfreaks.com/topic/247716-setting-of-items-per-row-from-database/#findComment-1272069 Share on other sites More sharing options...
WebStyles Posted September 23, 2011 Share Posted September 23, 2011 also, if you're basing it on the values of $i, where does $i start, and where is it incremented? Quote Link to comment https://forums.phpfreaks.com/topic/247716-setting-of-items-per-row-from-database/#findComment-1272071 Share on other sites More sharing options...
Buddski Posted September 23, 2011 Share Posted September 23, 2011 Thats a good point Just for fun.. you can do this without messing with tables and such.. Good old floated divs <?php echo '<div id="wrapper">'; $i = 0; while($item_rows = mysql_fetch_array($item_result)){ echo "<div style=\"float:left;text-align:center\"> <p><img src=\"../images/".$item_rows['item_id'].".gif\" width=\"45\" height=\"45\" /></p> <p>".$item_rows['name']."<br /> Attack: +".$item_rows['attack']."<br /> <img src=\"../images/credit.gif\" width=\"17\" height=\"17\" />$".$item_rows['sell_price']."</p> <p>[Equipt|Destory]<br /> </p> </div>"; } echo '<div style="clear:both"> </div>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/247716-setting-of-items-per-row-from-database/#findComment-1272076 Share on other sites More sharing options...
jch02140 Posted September 23, 2011 Author Share Posted September 23, 2011 I just noticed a few errors of my code earlier.. I have updated the code and now able to display the correct number of records... However, all records display are the last record in the database... <?php for ($i=0; $i<$count_result; $i++) { $output = " <td align=\"center\"><p><img src=\"../images/".$item_rows['item_id'].".gif\" width=\"45\" height=\"45\" /></p> <p>".$item_rows['name']."<br /> Attack: +".$item_rows['attack']."(".$item_rows['damage'].")<br /> <img src=\"../images/credit.gif\" width=\"17\" height=\"17\" />$".$item_rows['sell_price']."</p> <p>[Equipt|Destory]<br /> </p> </td>"; $remainder = $i % 5; echo ($remainder == 0 ? ($i == 0 ? "" : "\n</tr>")."\n$output " : $output); } $colspan = (4 - $remainder); $last_row = $colspan ? " <td ".($colspan? "colspan=$colspan" : "")."></td>" : ""; echo $last_row. " </tr> </table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/247716-setting-of-items-per-row-from-database/#findComment-1272099 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.