energy2080 Posted April 18, 2010 Share Posted April 18, 2010 hello all, I am trying to display images from my database by date. I need to first display a header which is the date, then I need to display the images from each record in four columns. I have gotten part of it done but I am having trouble making the images display in columns. Here is what I have so far: $query = "SELECT * FROM images WHERE published='1' order by addeddate"; $lastdate =''; foreach ($rows as $row) { $datetitle = $row->addeddate; $addeddate= mosFormatDate( $row->addeddate, "%B %d, %Y" ); $thisdate=$addeddate; if ($thisdate<>$lastdate){ echo '<hr>'; echo '<div >'.$addeddate. '</div><br/>'; $lastdate =$thisdate; } echo '<div ><img src="'.$row->picturelink.'" width="200" height="150" /></a></div><br/>'; echo '<div >'.$row->title. '</div><br/>'; } This works but I can only display one column per row. How would I go about displaying four columns per row. I want it to look like this April 15 2010 Image1.jpgImage2.jpgImage3.jpgImage4.jpg Image5.jpgImage6.jpgImage7.jpgImage8.jpg April 14 2010 Image1.jpgImage2.jpgImage3.jpgImage4.jpg Image5.jpgImage6.jpgImage7.jpgImage8.jpg But it currently looks like this: April 15 2010 Image1 Image2 Image3 Image4 April 14 2010 Image1 Image2 Image3 Image4 All help would be greatly appreciated. Thank you Link to comment https://forums.phpfreaks.com/topic/198886-display-images-by-date-added/ Share on other sites More sharing options...
trq Posted April 18, 2010 Share Posted April 18, 2010 See this thread in the Code Snippet Repository board. Link to comment https://forums.phpfreaks.com/topic/198886-display-images-by-date-added/#findComment-1043976 Share on other sites More sharing options...
energy2080 Posted April 18, 2010 Author Share Posted April 18, 2010 thanks for the quick response, I have tried this but it does not work with my query because i am also trying to display the date as a header. Can you show me a php snippet that will work with my query? Link to comment https://forums.phpfreaks.com/topic/198886-display-images-by-date-added/#findComment-1043978 Share on other sites More sharing options...
litebearer Posted April 18, 2010 Share Posted April 18, 2010 Some PSUEDO code (not tested, its late, I'm old BUT you should be able to grasp the concept)... $unique_dates = an array of the unique dates (earliest first) $total_tables = count the elements in that array this will be the number of times your outer loop runs Start the outer loop $i=0; while ($i<$total_tables) { ?> <table><tr><tD colspan="4"><?PHP echo $unique_dates[$i]; ?><td></tr> <? $image = an array of images where date of image = $unique_dates[$i] $total_images = count($image); $cell = 1; $i2 = 0; while($i2<$total_images) { if($cell==1) { echo "<tr>" } echo "<td>" . $image[$i2] . "</td>; if(($cell==4) OR ($i2 == ($total_images - 1)) { echo "</tr> $i2 ++; $cell = 1; } } <? </table><br> <?PHP $i++ } end outer loop Make sense? Link to comment https://forums.phpfreaks.com/topic/198886-display-images-by-date-added/#findComment-1044026 Share on other sites More sharing options...
energy2080 Posted April 18, 2010 Author Share Posted April 18, 2010 does make sense. I am new to this can you show me how to get a unique array after the database query? then count them? Link to comment https://forums.phpfreaks.com/topic/198886-display-images-by-date-added/#findComment-1044032 Share on other sites More sharing options...
litebearer Posted April 18, 2010 Share Posted April 18, 2010 untested - am SURE others may have a better, more efficient way, but I believe this should do the trick... $query01 = "SELECT distinct addeddate FROM images WHERE published='1' ORDER BY addeddate"; $result01 = mysql_query($query01); $total_tables = mysql_num_rows($result01); while ($row = mysql_fetch_array($result01, MYSQL_BOTH)) { $unique_dates[] = $row['addeddate']); } $i=0; while ($i<$total_tables) { ?> <table><tr><ts colspan="4"><?PHP echo $unique_dates[$i]; ?><td></tr> <? $query02 = "SELECT * FROM images WHERE published='1' AND addeddate = '$unique_dates[$i]"; $result02 = mysql_query($query02); $total_images = mysql_num_rows($result02); $images[] = $row['addeddate']); while ($row = mysql_fetch_array($result02, MYSQL_BOTH)) { $image[] = $row['piclink']; } $cell = 1; $i2 = 0; while($i2<$total_images) { if($cell==1) { echo "<tr>"; } echo "<td>" . $image[$i2] . "</td>; if(($cell==4) OR ($i2 == ($total_images - 1))) { echo "</tr>; $i2 ++; $cell = 1; } } } <? </table><br> <?PHP Link to comment https://forums.phpfreaks.com/topic/198886-display-images-by-date-added/#findComment-1044079 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.