colap Posted January 9, 2011 Share Posted January 9, 2011 Image name will come from database. It can be displayed in divs or table cells. Which is better using divs or table cells? Can anyone post sample code? http://img28.imageshack.us/i/44233003.png/ [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/223861-how-can-i-display-images-in-a-series-of-div-or-in-table-cell/ Share on other sites More sharing options...
litebearer Posted January 9, 2011 Share Posted January 9, 2011 A rough idea of how to do it - this will display ALL of the images in a table. Ultimately you will most likely want to paginate the results. To test it, simply modify where necessary. <?php /* connect to db */ include('db.php'); /* create your query */ $query = "SELECT * FROM what_ever_table_name"; /* execute the query */ $result = mysql_query($query); /* set the max columns for the table */ $max_columns = 4; /* set max columns for table */ /* count total images to be displayed */ $total_cells = mysql_num_rows($result); /* initially set counter to use in starting and ending rows */ $i = 0; ?> <table border ="2"> <?PHP $total_rows = ceil($total_cells / $max_columns); /* calculate total rows needed */ $junk1 = $total_rows * $max_columns; /* calculate number of empty cells in last row */ $junk2 = $junk1 - $total_cells; if($junk2==0) { $last_row = "</tr>"; }else{ $j = 0; while($j<$junk2){ $last_row = $last_row . "<td></td>"; $j ++; } $last_row = $last_row . "</tr>"; } while($row = mysql_fetch_array($result))) { /* begin looping thru the results */ if($i == 0){ echo "<tr>"; $i ++; } ?> /* EDIT THIS LINE TO REFLECT THE ACTUAL NAME OF YOUR DB TABLE FIELD */ <td><IMG src="<?PHP echo $row['image_field_name_here'; ?>"></td> <?PHP $i ++; if($i > $max_columns) { /* check if need to close row */ echo "</tr>"; $i=0; } } echo $last_row . "</table>"; /* clean up last row */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/223861-how-can-i-display-images-in-a-series-of-div-or-in-table-cell/#findComment-1157090 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.