Jump to content

Display images by date added


energy2080

Recommended Posts

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

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?

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.