chriscloyd Posted November 21, 2008 Share Posted November 21, 2008 i need help creating a new row for a table say i have 8 teams i want three coloums made then when it shows three make a new row this is all i can think of <?php if ($pic_num == 1) { echo '<tr>'; } if ($pic_num == 4) { echo '<tr>'; } if ($pic_num == 7) { echo '<tr>'; } if ($pic_num == 10) { echo '<tr>'; } if ($pic_num == 13) { echo '<tr>'; } ?> im just so lost on how to do this my meds are driving me crazy Link to comment https://forums.phpfreaks.com/topic/133611-solved-php-create-new-row/ Share on other sites More sharing options...
DarkWater Posted November 21, 2008 Share Posted November 21, 2008 The modulus operator, %, gives you the remainder from dividing two numbers. This can used to display something every x amount of iterations: <?php $data = array(1, 45, 12, 4, 58, 684, 51, 6, 8, 7, 21, 67); $cols = 3; $count = 0; echo "<table><tr>"; foreach ($data as $datum) { if ($count != 0 && $count % $cols == 0) { echo "</tr><tr>"; } echo "<td>$datum</td>"; $count++; } echo "</tr></table>"; ?> Link to comment https://forums.phpfreaks.com/topic/133611-solved-php-create-new-row/#findComment-695101 Share on other sites More sharing options...
chriscloyd Posted November 21, 2008 Author Share Posted November 21, 2008 lost on how it works Link to comment https://forums.phpfreaks.com/topic/133611-solved-php-create-new-row/#findComment-695105 Share on other sites More sharing options...
DarkWater Posted November 21, 2008 Share Posted November 21, 2008 Here it is, line by line: 1: Open PHP tag 2: Set up example data array 3: Specify number of columns per row we want 4: Initialize a count so we don't get nasty 'undefined variable' notices 5: Start table and first row 6: Begin foreach loop through data 7: Check to see if the remainder of $count / $cols is equal to 0. If it is, it means that $count is divisible by $cols, so it's gone through $cols number of rows. 8: End old row, start new one 9: END IF $count % $cols == 0 10: Echo data in table cell 11: Increase count 12: END FOREACH $data as $datum 13: Finish off last row and close table 14: Close PHP tag Link to comment https://forums.phpfreaks.com/topic/133611-solved-php-create-new-row/#findComment-695108 Share on other sites More sharing options...
chriscloyd Posted November 21, 2008 Author Share Posted November 21, 2008 this is what i get hmmm 1 45 12 4 58 684 51 6 8 7 21 67 doesnt really work the right way hmmm im doing it while getting info from database $get_all_players = mysql_query("SELECT * FROM users WHERE team1 OR team2 OR team3 = 1 "); then do a while statement so i can show each user Link to comment https://forums.phpfreaks.com/topic/133611-solved-php-create-new-row/#findComment-695110 Share on other sites More sharing options...
DarkWater Posted November 21, 2008 Share Posted November 21, 2008 The code I posted works just fine; you wanted 3 columns per row, correct? And just switch the foreach with while, and play around with it. It should work. Link to comment https://forums.phpfreaks.com/topic/133611-solved-php-create-new-row/#findComment-695112 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.