squiblo Posted February 3, 2010 Share Posted February 3, 2010 I want the data from this query... $get_friend_id = mysql_query("SELECT * FROM page_zebra WHERE user_id='$user_id' AND friend=1 LIMIT 0,9"); while($row = mysql_fetch_assoc($get_friend_id)){ $friend_id = $row['page_zebra_id']; to be displayed in a 3x3 table there will not always be exactly 9 pieces of data sometimes less, sometimes more. How is this possible? Quote Link to comment https://forums.phpfreaks.com/topic/190852-db-data-into-a-table/ Share on other sites More sharing options...
schilly Posted February 3, 2010 Share Posted February 3, 2010 well there won't be more with "limit 0,9" in there. when i've done tables like this I just keep a count of whatever row im on. mod your count every loop through by the number of columns you want. if it equals zero close your row tag and add a new row tag. when the result set is done. check your count again and fill in any blanks until count mod num_columns equals zero then close the row off. there is also the first case you need to check for when you only a tr tag. Quote Link to comment https://forums.phpfreaks.com/topic/190852-db-data-into-a-table/#findComment-1006460 Share on other sites More sharing options...
squiblo Posted February 3, 2010 Author Share Posted February 3, 2010 sorry im finding it hard to understand that Quote Link to comment https://forums.phpfreaks.com/topic/190852-db-data-into-a-table/#findComment-1006464 Share on other sites More sharing options...
squiblo Posted February 4, 2010 Author Share Posted February 4, 2010 another idea, how could i put the 9 pieces of data into 9 different variables or an array like $friend_id[0] = ???; $friend_id[1] = ???; $friend_id[2] = ???; $friend_id[3] = ???; $friend_id[4] = ???; $friend_id[5] = ???; $friend_id[6] = ???; $friend_id[7] = ???; $friend_id[8] = ???; Quote Link to comment https://forums.phpfreaks.com/topic/190852-db-data-into-a-table/#findComment-1006469 Share on other sites More sharing options...
schilly Posted February 4, 2010 Share Posted February 4, 2010 something like: <?php $count=0; $num_cols = 3; echo "<table>"; while($row = mysql_fetch_array($results)){ if($count == 0 && $count % $num_cols == 0) echo "<tr>"; if($count != 0 && $count % $num_cols == 0) echo "</tr><tr>"; //echo row data in <td><td> $count++; } //finish table. fill in blank cells. while($count % $num_cols != 0){ echo "<td> </td>"; } echo "</tr></table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/190852-db-data-into-a-table/#findComment-1006474 Share on other sites More sharing options...
squiblo Posted February 4, 2010 Author Share Posted February 4, 2010 i am getting a never ending table with 1 row and never ending columns $get_friend_id = mysql_query("SELECT * FROM page_zebra WHERE user_id='$user_id' AND friend=1 LIMIT 0,9"); $count=0; $num_cols = 3; echo "<table border='1'>"; while($row = mysql_fetch_array($get_friend_id)){ if($count == 0 && $count % $num_cols == 0) echo "<tr>"; if($count != 0 && $count % $num_cols == 0) echo "</tr><tr>"; //echo row data in <td><td> $count++; } //finish table. fill in blank cells. while($count % $num_cols != 0){ echo "<td> </td>"; } echo "</tr></table>"; Quote Link to comment https://forums.phpfreaks.com/topic/190852-db-data-into-a-table/#findComment-1006476 Share on other sites More sharing options...
schilly Posted February 4, 2010 Share Posted February 4, 2010 oops. need to increment count in final loop. sorry. wrote that up in about 2mins. <?php //finish table. fill in blank cells. while($count % $num_cols != 0){ echo "<td> </td>"; $count++; } echo "</tr></table>"; Quote Link to comment https://forums.phpfreaks.com/topic/190852-db-data-into-a-table/#findComment-1006883 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.