Andrej_Petras Posted January 15, 2022 Share Posted January 15, 2022 (edited) Hi, I need help for printing table from MySQL in PHP. i need this: 1 2 3 4 5 6 7 8 9 no this: 1 2 3 4 If someone has code for that i would appreciate it. Edited January 15, 2022 by Andrej_Petras Quote Link to comment https://forums.phpfreaks.com/topic/314415-columns/ Share on other sites More sharing options...
ginerjm Posted January 16, 2022 Share Posted January 16, 2022 Run your query (I used pdo) and then do the following: $row_size = 3; $cnt = 0; echo "<table><tr>"; while($row = $pdo->fetch()) { if ($cnt == $row_size) { echo "</tr><tr>"; $cnt = 0; } echo "<td>{$row['item']}</td>"; $cnt++; } echo "</tr></table>"; Quote Link to comment https://forums.phpfreaks.com/topic/314415-columns/#findComment-1593350 Share on other sites More sharing options...
gizmola Posted January 16, 2022 Share Posted January 16, 2022 There are many ways to accomplish this. What approach have you attempted so far? HTML tables Flexbox Grid One fundamental technique for procedural programming, when looping through a set of data, is to utilize the modulus operator. By checking the remainder of division by the number of items you want per row, you can determine whether or not to start a new line, or whether a new line was just started. Consider this code that has a simulated result set of rows: <?php define('PER_ROW', 3); $resultset[] = array('id' => 3, 'name' => 'Bob'); $resultset[] = array('id' => 5, 'name' => 'Mark'); $resultset[] = array('id' => 9, 'name' => 'Sam'); $resultset[] = array('id' => 10, 'name' => 'Linda'); $resultset[] = array('id' => 11, 'name' => 'Amy'); $resultset[] = array('id' => 75, 'name' => 'John'); $resultset[] = array('id' => 20, 'name' => 'Aaron'); $resultset[] = array('id' => 19, 'name' => 'Ringo'); $resultset[] = array('id' => 2, 'name' => 'Paul'); $resultset[] = array('id' => 99, 'name' => 'George'); foreach ($resultset as $key => $row) { $key++; if ($key % PER_ROW === 1) { // First item in row echo "| "; } echo "{$row['id']}: {$row['name']} | "; if ($key % PER_ROW === 0) { // Last item in row echo PHP_EOL; } } echo PHP_EOL; You can play with this example here: https://3v4l.org/lD83M Changing the PER_ROW constant to another number like 4 for example, should help you see how this common technique works. Quote Link to comment https://forums.phpfreaks.com/topic/314415-columns/#findComment-1593352 Share on other sites More sharing options...
Psycho Posted January 18, 2022 Share Posted January 18, 2022 (edited) And, for good measure, I will provide another option: array_chunk() $columns = 3; //Put results into an array $results = $pdo->fetchAll(); echo "<table>"; foreach(array_chunk($results, $columns) as $rowData) { echo "<tr>"; foreach($rowData as $record) { echo "<td>{$record['name']}</td>"; echo "</tr>"; } echo "</table>"; EDIT: Fixed inner TD tags Edited January 18, 2022 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/314415-columns/#findComment-1593411 Share on other sites More sharing options...
ginerjm Posted January 18, 2022 Share Posted January 18, 2022 Maybe a td tag inside the inner foreach loop instead of a tr tag? 1 Quote Link to comment https://forums.phpfreaks.com/topic/314415-columns/#findComment-1593413 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.