Reputation: 43
I have a bad time thinking... IDK what to do it's almost a day still can't figure it out... this is my display and I want to be like this...
https://i.sstatic.net/h2E36.jpg
My Implementation
<?php
$userId = (int) (!isset($_GET['id'])) ? 1 : $_GET['id'];
$imagesResult = $db->getWhere("SELECT * FROM photo WHERE userID = {$userId} ORDER BY `when` DESC");
echo '<pre>';
print_r($imagesResult);
echo '</pre>';
foreach ($imagesResult as $key => $value) {
if(strtotime(date('j M Y', $value['when'])) == strtotime(date('j M Y'))) {
echo "<div class='item'><h3>Today</h3></div>";
} else {
echo "<div class='item'><h3>".date('j M Y', $value['when'])."</h3></div>";
}
echo "
<div class='item'>
<a href='photo.php?photoid={$value['id']}'><img src='img/picture/{$value['userId']}/{$value['imageLocation']}' height='180' /></a>
</div>";
}
?>
i don't know where to begin... that will return like that... it is hard to do when the date is outside the loop. i don't know where to put... please help me with this... just give me some little hints to do this...
Sample Return of $imageResult
Array
(
[0] => Array
(
[id] => 35
[userId] => 1
[albumId] =>
[imageLocation] => Background.png
[description] => some description
[tags] =>
[when] => 1394965560
)
[1] => Array
(
[id] => 36
[userId] => 1
[albumId] =>
[imageLocation] => Background1.png
[description] => some description
[tags] =>
[when] => 1394965560
)
)
Upvotes: 1
Views: 47
Reputation: 6726
Try using a flag variable:
$userId = (int) (!isset($_GET['id'])) ? 1 : $_GET['id'];
$imagesResult = $db->getWhere("SELECT * FROM photo WHERE userID = {$userId} ORDER BY `when` DESC");
echo '<pre>';
print_r($imagesResult);
echo '</pre>';
$lastPrintedDateFlag = ""; // Flag variable
foreach ($imagesResult as $key => $value) {
if ($lastPrintedDateFlag != $value['when']) {
$lastPrintedDateFlag = $value['when'];
if(strtotime(date('j M Y', $value['when'])) == strtotime(date('j M Y'))) {
echo "<div class='item'><h3>Today</h3></div>";
} else {
echo "<div class='item'><h3>".date('j M Y', $value['when'])."</h3></div>";
}
}
echo "
<div class='item'>
<a href='photo.php?photoid={$value['id']}'><img src='img/picture/{$value['userId']}/{$value['imageLocation']}' height='180' /></a>
</div>";
}
But this is just a tricky way to archive what you want. I recommend to prepare a 2 dimensions array with images grouped by date, then you will print the result more clearly:
Example of the array:
Array
(
[16-03-14] => Array
(
[1] => Array
(
[id] => 35
[userId] => 1
[albumId] =>
[imageLocation] => Background.png
[description] => some description
[tags] =>
[when] => 1394965560
),
[2] => Array
(
[id] => 35
[userId] => 1
[albumId] =>
[imageLocation] => Background.png
[description] => some description
[tags] =>
[when] => 1394965560
)
)
[17-03-14] => Array
(
[1] => Array
(
[id] => 35
[userId] => 1
[albumId] =>
[imageLocation] => Background.png
[description] => fuck this Shit
[tags] =>
[when] => 1394965560
)
)
)
Upvotes: 1
Reputation: 354
First of all I suggest instead of echoing results directly, you should build a final array GROUPED BY dates (including today).
Like this:
$actualImageResults = array();
foreach ($imagesResult as $Image) {
$subarr = (date('j M Y', $Image['when']) == date('d M Y')) ? "today" : date("j-m-Y");
if(!is_array($actualImageResults[$subarr])) $actualImageResults[$subarr] = array();
array_push($actualImageResults[$subarr], $Image);
}
And then var_dump($actualImageResults)
to see if data is properly grouped...
After this you can go ahead an echo results:
foreach($actualImageResults as $stamp => $images) {
echo "<strong>{$stamp}</strong>";
foreach($images as $image) {
echo $image["imageLocation"];
/.../
}
}
this is just a rough idea, you will need to make necessary adjustments according to your needs... but this is what I understand from your question and image link you posted. Its something like this -> making a final array from echoing having dates as its keys and all images grouped among them.
Issues:
I guess you are working in an environment with full error_reporting / debugging / etc... Turn off notices in error reporting on start of file. Something like:
<?php
error_reporting(E_ALL ^ E_NOTICE);
/.../
Upvotes: 2