QuackMasterDan Posted May 28, 2008 Share Posted May 28, 2008 Hello there, I am creating a website for a client who has specifically requested a layout of data from a MYSQL table. The table named states has the columns id, and statename (its a list of states) and I have created a script to organize the statenames into a table, which will later have links added to them. The table must only be three cells (<td>) wide, and rows (<tr>) should be able to expand forever, as a lot of content will be added. Here is the code: $query = "SELECT * FROM states"; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); if (mysql_num_rows($result) > 0) { echo "<table cellpadding=\"15px\" border=\"1\"><tr><td align=\"middle\" colspan=\"100%\" valign=\"middle\"><h1>States of Russia</h1></td></tr><tr valign=\"middle\">"; while ($row = mysql_fetch_row($result)) { echo "<td align=\"middle\" width=\"200px\" height=\"200px\" cellpadding=\"25px\"><h3>".$row[1]."</h3></td>"; } echo "</tr>"; echo "</table>"; } else { echo "No rows found!"; } mysql_free_Result($result); mysql_close($connection); ?> Pretty simple stuff, however the way it is setup it just writes every entry into a cell, all in one row. How can I create a new row after every third cell, and each new row continues to list states where the previous cell left off? Quote Link to comment https://forums.phpfreaks.com/topic/107544-create-an-organized-table-from-mysql/ Share on other sites More sharing options...
mga_ka_php Posted May 28, 2008 Share Posted May 28, 2008 can you show the output you want to achieve? Quote Link to comment https://forums.phpfreaks.com/topic/107544-create-an-organized-table-from-mysql/#findComment-551323 Share on other sites More sharing options...
QuackMasterDan Posted May 28, 2008 Author Share Posted May 28, 2008 For a pictographical representation: ______________________________________________ | States of Russia | ______________________________________________ | | | | | | | | | $row[1] #1 | $row[1] #2 | $row[1] [#3] | | | | | | | | | | | | | _____________________________________________ | NEW ROW | | | | | | | | | $row[1] #1 | $row[1] #2 | $row[1] [#3] | | | | | | | | | | | | | _____________________________________________ | NEW ROW I'm asking a friend who thinks it should be done with an array and a counter that counts: 0,1,2 and then resets, but I'm not sure how to implement it. Quote Link to comment https://forums.phpfreaks.com/topic/107544-create-an-organized-table-from-mysql/#findComment-551343 Share on other sites More sharing options...
mga_ka_php Posted May 28, 2008 Share Posted May 28, 2008 got a problem like that before, but mine was limited only to view 3x4 per page. so what i did was i used css left and top for positioning it into every column and row. if someone has a better solution please post.it can help me too. Quote Link to comment https://forums.phpfreaks.com/topic/107544-create-an-organized-table-from-mysql/#findComment-551379 Share on other sites More sharing options...
QuackMasterDan Posted May 28, 2008 Author Share Posted May 28, 2008 After about an hour and a half of coding, I solved it. I created an array from the data that comes from the MySQL table. I set a variable ($i) that starts off at 0. A while loop takes information from the table and each time it repeats adds 1 to $i ($i++). Then $i is checked on what number it is. Since I want it to be 3 columns long, the number is 3. So once $i=3, it is reset to 0. This continues to mysql_fetch_row is no longer true, and stops. Here is the code: /* CODE STARTS HERE */ $query = "SELECT * FROM states"; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); echo "<table cellpadding=\"15px\"><tr><td align=\"middle\" colspan=\"100%\" valign=\"middle\"><h1>States</h1></td></tr> <tr valign=\"center\" height=\"200px\">"; $data = array(); $i=0; while ($row = mysql_fetch_row($result)) { $i++; $data[$i] = $row[1]; if ($i == 1) {echo "<td width=\"200px\">$data[1]</td>";} if ($i == 2) {echo "<td width=\"200px\">$data[2]</td>";} if ($i == 3) {echo "<td width=\"200px\">$data[3]</td></tr><tr valign=\"center\" height=\"200px\">";} if ($i == 3) {$i = 0;} } /* CODE ENDS HERE */ Quote Link to comment https://forums.phpfreaks.com/topic/107544-create-an-organized-table-from-mysql/#findComment-551423 Share on other sites More sharing options...
Barand Posted May 28, 2008 Share Posted May 28, 2008 This a neat solution (by rhodesa) http://www.phpfreaks.com/forums/index.php/topic,198223.msg895525.html#msg895525 Quote Link to comment https://forums.phpfreaks.com/topic/107544-create-an-organized-table-from-mysql/#findComment-552203 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.