here i m having a loop which retrieves values from db. my issue is i want to skip duplicate DATE value, this code works perfectly, but DATE value is showing to all articles i want only DATE to show only once.
for example:
DATE: 5-2-2015
article 1
DATE: 5-2-2015
article 2
DATE: 4-2-2015
article 3
this is how my code shows
but i want like this
DATE: 5-2-2015
article 1
article 2
DATE: 4-2-2015
article 3
<?php
$postResults = posts();
foreach ($postResults as $postResult)
{
$date = strtotime($postResult['post_added_time']);
echo $date = date( 'j M Y', $date);
?>
<?php
if ($postResult['post_university'] == 'JNTU HYDERABAD')
{
?>
<div class="posts"><img class="arrow" src="<?php echo $site['config']['themeUrl'];?>/assets/images/arrow.GIF"/><a target="_blank" class="green" href="<?php echo $postResult['post_slug']; ?>"><?php echo $postResult['post_university'] . " : " . $postResult['post_title'] ;?></a>
<?php
if (strtotime($postResult['post_added_time']) > strtotime('-3 days'))
{
?>
<span class="new"></span>
<?php
}
?>
</div>
<?php
}
}
?>
I usually do this way around
//initialise date first
date = '';
for loop{
//Check for condition as date from loop is not equal to date in loop
if(date != date from loop){
date = date from loop
echo date;
}
rest of the code of displaying article details will be as below
...
}
Hope this helps you.
You can try following query .
SELECT distinct date,* FROM " . POSTS . " WHERE status = ? ORDER BY pid DESC
$new = array ();
$postResults = array (
array (
'post_added_time' => '2015-01-01',
'post_title' => 'title 1'
),
array (
'post_added_time' => '2015-01-02',
'post_title' => 'title 2'
),
array (
'post_added_time' => '2015-01-03',
'post_title' => 'title 3'
),
array(
'post_added_time' => '2015-01-03',
'post_title' => 'title 4'
),
);
foreach ( $postResults as $postResult ) {
$date = $postResult ['post_added_time'];
$new [$date] [] = $postResult;
}
echo "<pre>";
print_r($new);
die;
It will print:
Array
(
[2015-01-01] => Array
(
[0] => Array
(
[post_added_time] => 2015-01-01
[post_title] => title 1
)
)
[2015-01-02] => Array
(
[0] => Array
(
[post_added_time] => 2015-01-02
[post_title] => title 2
)
)
[2015-01-03] => Array
(
[0] => Array
(
[post_added_time] => 2015-01-03
[post_title] => title 3
)
[1] => Array
(
[post_added_time] => 2015-01-03
[post_title] => title 4
)
)
)
You can this $new array to print values as per your requirement.