bugzy Posted July 26, 2012 Share Posted July 26, 2012 Hello guys.. I need some idea on how will I able to do this I have this code: <?php <table width="700"> <tr> <td width="140"></td> <td width="140"></td> <td width="140"></td> <td width="140"></td> <td width="140"></td> </tr> while(list($item_id,$item_code,$item_price,$item_stock,$item_sale,$item_reg,) = mysql_fetch_row($result)) { echo "<td align=\"center\">"; echo "<img src=".$png." id=\"resizeMe\" />"; echo "<br><span class=\"admin_list\"><u>". $item_code ."</span></u>"; echo "<br>P <b>". (int)$item_price ."</b>"; echo "<br>Stocks: <b>". $item_stock ."</b>"; echo "<br>Sale? "; if($item_sale == 0){ echo "<b>NO</b>"; }else{ echo "<b>YES</b>"; } echo "<br><span class=\"admin_list_date\">Date Added: <b>". $time_reg ."</span></b>"; echo "<br><a href=\"edit_item.php?id=". urlencode($item_id) ."\">[edit]</a> "; echo "<a href=\"delete_item.php?id=". urlencode($item_id) . "\"onclick=\"return confirm('Are you sure you delete item ". $item_id ." ?');\"> [delete]</a> "; echo "<a target=\"_blank\" href=\"edit_item.php?id=". urlencode($item_id) ."\">[view]</a>"; echo "</td>"; } </table> ?> After counting 5 rows I want to move to the next row of the table like this: <tr>item1 item2 item3 item4 item5</tr> <tr>item6 item7 item8 item9 item10</tr> <tr>item11 item12 </tr> Quote Link to comment https://forums.phpfreaks.com/topic/266307-move-to-next-line-after-counting-5-rows/ Share on other sites More sharing options...
Psycho Posted July 26, 2012 Share Posted July 26, 2012 Give this a try $max_columns = 5; $col_width = 140; $table_width = $col_width * $max_columns; //Open table echo "<table width='{$table_width}'>\n"; //Create header row echo "<th>\n"; for($col=0; $col<$max_columns; $col++) { echo "<td width='{$col_width}'></td>\n"; } echo "<th>\n"; //Process query results $recCnt = 0; while($row = mysql_fetch_assoc($result)) { $recCnt++; //Open new row if needed if($max_columns%$recCnt == 1) { echo "<tr>\n"; } $price = (int) $row['item_price']; $sale = ($row['item_sale']) ? 'YES' : 'NO'; $item_id_url = urlencode($row['item_id']); echo "<td align='center'>\n"; echo "<img src='{$png}' id='resizeMe' /><br>\n"; echo "<span class='admin_list'><u>{$row['item_code']}</u></span><br>\n"; echo "P <b>{$price}</b><br>\n"; echo "Stocks: <b>{$row['item_stock']}</b><br>\n"; echo "Sale? <b>{$sale}</b><br>\n"; echo "<span class='admin_list_date'>Date Added: <b>{$row['time_reg']}</b></span><br>\n"; echo "<a href='edit_item.php?id={$item_id_url}'>[edit]</a> \n"; echo "<a href='delete_item.php?id={$item_id_url}' onclick='return confirm('Are you sure you delete item {$row['item_id']}?');'> [delete]</a> \n"; echo "<a target='_blank' href='edit_item.php?id={$item_id_url}'>[view]</a>\n"; echo "</td>\n"; //Close row if needed if($max_columns%$recCnt == 0) { echo "</tr>\n"; } } //Close last row if needed if($max_columns%$recCnt == 0) { echo "</tr>\n"; } //Close table echo "</table>\n" Quote Link to comment https://forums.phpfreaks.com/topic/266307-move-to-next-line-after-counting-5-rows/#findComment-1364748 Share on other sites More sharing options...
jazzman1 Posted July 26, 2012 Share Posted July 26, 2012 Or try my: <?php echo '<table>'; echo '<tr>'; while(list($item_id,$item_code,$item_price,$item_stock,$item_sale,$item_reg,) = mysql_fetch_row($result)) { echo "<td align=\"center\">"; echo "<img src=".$png." id=\"resizeMe\" />"; echo "<br><span class=\"admin_list\"><u>". $item_code ."</span></u>"; echo "<br>P <b>". (int)$item_price ."</b>"; echo "<br>Stocks: <b>". $item_stock ."</b>"; echo "<br>Sale? "; if($item_sale == 0){ echo "<b>NO</b>"; }else{ echo "<b>YES</b>"; } echo "<br><span class=\"admin_list_date\">Date Added: <b>". $time_reg ."</span></b>"; echo "<br><a href=\"edit_item.php?id=". urlencode($item_id) ."\">[edit]</a> "; echo "<a href=\"delete_item.php?id=". urlencode($item_id) . "\"onclick=\"return confirm('Are you sure you delete item ". $item_id ." ?');\"> [delete]</a> "; echo "<a target=\"_blank\" href=\"edit_item.php?id=". urlencode($item_id) ."\">[view]</a>"; echo "</td>"; if($item_id%5 == 0) {echo '</tr><tr>';} } echo '</tr>'; echo '</table>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/266307-move-to-next-line-after-counting-5-rows/#findComment-1364753 Share on other sites More sharing options...
Mahngiel Posted July 27, 2012 Share Posted July 27, 2012 Or try my: Your code is unreadable, illogically structured, deprecated, and incorrect. Quote Link to comment https://forums.phpfreaks.com/topic/266307-move-to-next-line-after-counting-5-rows/#findComment-1364754 Share on other sites More sharing options...
jazzman1 Posted July 27, 2012 Share Posted July 27, 2012 Or try my: Your code is unreadable, illogically structured, deprecated, and incorrect. It's not my code. I just break down a table row by its id Quote Link to comment https://forums.phpfreaks.com/topic/266307-move-to-next-line-after-counting-5-rows/#findComment-1364755 Share on other sites More sharing options...
jazzman1 Posted July 27, 2012 Share Posted July 27, 2012 Sorry, I wanted to say, my solution related to him code Quote Link to comment https://forums.phpfreaks.com/topic/266307-move-to-next-line-after-counting-5-rows/#findComment-1364757 Share on other sites More sharing options...
bugzy Posted July 27, 2012 Author Share Posted July 27, 2012 Give this a try $max_columns = 5; $col_width = 140; $table_width = $col_width * $max_columns; //Open table echo "<table width='{$table_width}'>\n"; //Create header row echo "<th>\n"; for($col=0; $col<$max_columns; $col++) { echo "<td width='{$col_width}'></td>\n"; } echo "<th>\n"; //Process query results $recCnt = 0; while($row = mysql_fetch_assoc($result)) { $recCnt++; //Open new row if needed if($max_columns%$recCnt == 1) { echo "<tr>\n"; } $price = (int) $row['item_price']; $sale = ($row['item_sale']) ? 'YES' : 'NO'; $item_id_url = urlencode($row['item_id']); echo "<td align='center'>\n"; echo "<img src='{$png}' id='resizeMe' /><br>\n"; echo "<span class='admin_list'><u>{$row['item_code']}</u></span><br>\n"; echo "P <b>{$price}</b><br>\n"; echo "Stocks: <b>{$row['item_stock']}</b><br>\n"; echo "Sale? <b>{$sale}</b><br>\n"; echo "<span class='admin_list_date'>Date Added: <b>{$row['time_reg']}</b></span><br>\n"; echo "<a href='edit_item.php?id={$item_id_url}'>[edit]</a> \n"; echo "<a href='delete_item.php?id={$item_id_url}' onclick='return confirm('Are you sure you delete item {$row['item_id']}?');'> [delete]</a> \n"; echo "<a target='_blank' href='edit_item.php?id={$item_id_url}'>[view]</a>\n"; echo "</td>\n"; //Close row if needed if($max_columns%$recCnt == 0) { echo "</tr>\n"; } } //Close last row if needed if($max_columns%$recCnt == 0) { echo "</tr>\n"; } //Close table echo "</table>\n" Psycho thanks. On that code, will it work if I use while(list($item_id,$item_code,$item_price,$item_stock,$item_sale,$item_reg,) = mysql_fetch_row($result)) instead of while($row = mysql_fetch_assoc($result)) I will try this later once I got home. Quote Link to comment https://forums.phpfreaks.com/topic/266307-move-to-next-line-after-counting-5-rows/#findComment-1364783 Share on other sites More sharing options...
carugala Posted July 27, 2012 Share Posted July 27, 2012 Your code is unreadable, illogically structured, deprecated, and incorrect. WHOA!!!!!! pissing contest? personally, I would only say something like that, if I could PROVE: if unreadable, well, nothing else you post is relevant. how is it incorrect, if no read? illogically st... ok, spock, define structure(d). we can go from there. deprecated? nobody who knows that word would use it as you have. plus, even if deprecated, still works. Quote Link to comment https://forums.phpfreaks.com/topic/266307-move-to-next-line-after-counting-5-rows/#findComment-1364786 Share on other sites More sharing options...
Psycho Posted July 27, 2012 Share Posted July 27, 2012 Psycho thanks. On that code, will it work if I use while(list($item_id,$item_code,$item_price,$item_stock,$item_sale,$item_reg,) = mysql_fetch_row($result)) instead of while($row = mysql_fetch_assoc($result)) Yes, but that structure is poor form. Using that type of logic could cause problems later on. Quote Link to comment https://forums.phpfreaks.com/topic/266307-move-to-next-line-after-counting-5-rows/#findComment-1364789 Share on other sites More sharing options...
bugzy Posted July 27, 2012 Author Share Posted July 27, 2012 Thanks man. I got it working now Quote Link to comment https://forums.phpfreaks.com/topic/266307-move-to-next-line-after-counting-5-rows/#findComment-1364920 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.