mkoenig Posted September 26, 2007 Share Posted September 26, 2007 Selecting php into tables with multiple columns? I know how to select the data into tables with one column, but how do you select into 4 columns and 4 rows? Ex. If i had 16 images i wanted displayed 1234 5678 9101112 13141516 I've seen it done before? Thanks PHP Freaks. Quote Link to comment https://forums.phpfreaks.com/topic/70706-solved-selecting-php-into-tables-with-multiple-columns/ Share on other sites More sharing options...
Psycho Posted September 26, 2007 Share Posted September 26, 2007 You dont state how you are getting the data: array, query, etc? Here is an example using an array <?php $columns = 4; $currentRow = array(); echo "<table>"; foreach ($imageArray as $image) { $currentRow[] = $image; if (count($currentRow)==$columns) { echo "<tr><td>".implode('</td><td>', $currentRow)."</td></tr>"; $currentRow = array(); } } if (count($currentRow)>0) { while (count($currentRow)<$columns) { $currentRow[] = " " } echo "<tr><td>".implode('</td><td>', $currentRow)."</td></tr>"; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/70706-solved-selecting-php-into-tables-with-multiple-columns/#findComment-355453 Share on other sites More sharing options...
mkoenig Posted September 26, 2007 Author Share Posted September 26, 2007 I would use query select * from tablename I will try this array code however. Thank for your time Quote Link to comment https://forums.phpfreaks.com/topic/70706-solved-selecting-php-into-tables-with-multiple-columns/#findComment-355603 Share on other sites More sharing options...
mkoenig Posted September 27, 2007 Author Share Posted September 27, 2007 Not working for me? Thanks What I want is to select from a normal $query = "Select * from tablename order by entryid desc limit 16"; $result = mysql_query($query); and take that output and put it into a 4x4 table with 16 total blocks. Does anyone have the code for this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/70706-solved-selecting-php-into-tables-with-multiple-columns/#findComment-356679 Share on other sites More sharing options...
Psycho Posted September 27, 2007 Share Posted September 27, 2007 You would just need to modify the code I gave for iterating through a result set instead of an array. Here is the same basic functionality utilizing a db query and also with some other enhancements - Uses a function to create the table row instead of hard coding it in two places - Uses array_pad() instead of a while loop I have tested this code, so i know it works. However, this will display as many records as you give it in your result set into the number of columns specified in $columns. If you want to limit it to 16 records only then you should do that in your query. But, it will handle record sets that are not divisible by the number of columns by adding more cells to complete the last row. <?php function showRow($rowData) { echo "<tr>\n<td>".implode("</td>\n<td>", $rowData)."</td>\n</tr>\n"; } $columns = 4; $query = "SELECT * FROM genres"; $result = mysql_query($query); if (!mysql_num_rows($result)) { echo "NO results returned"; } else { echo "<table border=\"1\">\n"; while ($record = mysql_fetch_assoc($result)) { $image = $record['name']; $currentRow[] = $image; if (count($currentRow)==$columns) { showRow($currentRow); $currentRow = array(); } } if (count($currentRow)>0) { array_pad($currentRow, 5, ' '); showRow($currentRow); } echo "</table>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/70706-solved-selecting-php-into-tables-with-multiple-columns/#findComment-356700 Share on other sites More sharing options...
sasa Posted September 27, 2007 Share Posted September 27, 2007 try echo '<table>'; while ($record = mysql_fetch_assoc($result)) { echo '<tr>; // echo data echo '<td><img src="', $record['field name'],'"></img></td>'; for( $i = 1; $i < 4; $i++) { if ($record = mysql_fetch_assoc($result)) echo '<td><img src="', $record['field name'],'"></img></td>'; else echo '<td> </td>'; } echo '</tr>'; } echo '</table>'; Quote Link to comment https://forums.phpfreaks.com/topic/70706-solved-selecting-php-into-tables-with-multiple-columns/#findComment-356728 Share on other sites More sharing options...
mkoenig Posted September 28, 2007 Author Share Posted September 28, 2007 Thank you both so much ill try these in the next hour, and then close the topic if it works. (im sure it will work if im smart enough to do it lol) Thanks for your time(s) Quote Link to comment https://forums.phpfreaks.com/topic/70706-solved-selecting-php-into-tables-with-multiple-columns/#findComment-356862 Share on other sites More sharing options...
mkoenig Posted September 28, 2007 Author Share Posted September 28, 2007 Thanks so much Although im sure they both worked i tried... echo '<table>'; while ($record = mysql_fetch_assoc($result)) { echo '<tr>'; // echo data echo '<td><img src="', $record['field name'],'"></img></td>'; for( $i = 1; $i < 4; $i++) { if ($record = mysql_fetch_assoc($result)) echo '<td><img src="', $record['field name'],'"></img></td>'; else echo '<td> </td>'; } echo '</tr>'; } echo '</table>'; first. Quote Link to comment https://forums.phpfreaks.com/topic/70706-solved-selecting-php-into-tables-with-multiple-columns/#findComment-356917 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.