foulmouthedleon Posted July 1, 2010 Share Posted July 1, 2010 Hi there all, first post so please be gentle! I'm trying to pull four stories out of the database and display them in a table fomat. I know doing it by CSS is preferred, but time is of the essence and this is a quick and dirty fix to my problem. For the life of me, I can't seem to figure out how to arrange the results in the format I desire. Below is how I want it to appear, was wondering if someone could crank out the PHP code to make this a reality? Many thanks in advance...! <table width="620" border="0"> <tr> <td>photo1</td> <td>description1</td> <td>photo3</td> <td>description3</td> </tr> <tr> <td>photo2</td> <td>description2</td> <td>photo4</td> <td>desciprtion 4 </td> </tr> </table> This essentially produces a 4 x 4 table, though each story will have a picture associated with it. Quote Link to comment https://forums.phpfreaks.com/topic/206407-dynamic-table-from-sql-statement/ Share on other sites More sharing options...
Pikachu2000 Posted July 1, 2010 Share Posted July 1, 2010 Without knowing your database structure, and other criteria, this is about the best I can do for you. Pseudo-code: $query = " SELECT `photo`, `text1`, `text2`, `text3 FROM `table` LIMIT 4"; $result = mysql_query($query); echo "<table>"; while( $array = mysql_fetch_assoc($result) { echo "<tr><td>{$array['photo']}</td><td>{$array['text1']}</td><td>{$array['text2']}</td><td>{$array['text3']}</td></tr>"; } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/206407-dynamic-table-from-sql-statement/#findComment-1079786 Share on other sites More sharing options...
Adam Posted July 1, 2010 Share Posted July 1, 2010 So you want to place them in a grid like..? 1 3 2 4 That's a little more difficult to do as you're not counting through them in a logical way. 1 2 3 4 ... would be easier as on the second 'story', you could just start a new table row within your loop. I've got to shoot for a couple of hours but if you don't have a solution to achieve that later I'll come back. Quote Link to comment https://forums.phpfreaks.com/topic/206407-dynamic-table-from-sql-statement/#findComment-1079789 Share on other sites More sharing options...
Pikachu2000 Posted July 1, 2010 Share Posted July 1, 2010 Disregard my first post; it looked as though you were pulling 4 fields for each of 4 records, but I now see that isn't the case. You want 2 fields for each of 4 records. I agree with MrAdam on the way you've described the output as 1.3.2.4 being the more difficult way to do this. If the order can be simply 1,2,3,4, then this will work regardless of whether the number of results returned is odd or even, so you can eliminate or change the LIMIT and add a WHERE clause if needed. $query = "SELECT `photo`, `text` FROM `test` LIMIT 4"; $result = mysql_query($query) or die(mysql_error()); echo "<table>\n"; $i = 1; while( $array = mysql_fetch_assoc($result) ) { if( $i % 2 != 0 ) { echo "<tr>\n"; } echo "<td>{$array['photo']}</td>\n<td>{$array['text']}</td>\n"; if ( $i % 2 == 0 ) { echo "</tr>\n"; } $i ++; } if( $i % 2 == 0 ) { echo "<td></td>\n</tr>\n"; } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/206407-dynamic-table-from-sql-statement/#findComment-1079824 Share on other sites More sharing options...
Adam Posted July 2, 2010 Share Posted July 2, 2010 I have 2 solutions in mind, but the one to use would depend on the limit of this query. Will it just always be 4 rows/stories returned, or should be [able to cope with] unlimited? Quote Link to comment https://forums.phpfreaks.com/topic/206407-dynamic-table-from-sql-statement/#findComment-1080157 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.