I have a .php script that retrieve fields from my database table and then displays them in a foreach loop (here are the relevant parts of the code):
<?php
$jquery1 = "SELECT `fulltext`,alias,metakey,publish_up,metadesc
FROM kms_content
WHERE catid=22 AND state= '1'";
$jdb1->setQuery( $jquery1 );
$jfeed1= $jdb1->loadObjectList();
foreach ($jfeed1 as $jitem1):
?>
<li>
<div>
<?php
$oldLocale = setlocale(LC_TIME, 'en_US');
echo strftime("%a %d %b %Y", strtotime($jitem1->publish_up));
setlocale(LC_TIME, $oldLocale);
?>
</div>
<div><?php echo $jitem1->fulltext; ?></div>
</li>
<?php
endforeach;
?>
So everything works great except that my array entries are sorted by id
. I would like them to be sorted out by the date (by the publish_up
value).
I have tried this code, but it's displaying a blank page with only the title at the top:
$jfeed1 = $jdb1->loadObjectList();
function date_compare($a, $b)
{
$t1 = strtotime($a['publish_up']);
$t2 = strtotime($b['publish_up']);
return $t1 - $t2;
}
usort($jfeed1, 'date_compare');
foreach ($jfeed1 as $jitem1):
I've "learned" php just by looking at codes and reading tutorials, so I am missing a lot of fundamental knowledge which is essential when I have to put theory into practice.
I am confused about the difference between an array
and an object
and I can't figure out what I am supposed to do to solve this warning.
EDIT:
<?php
var_dump($jfeed1)
?>
gives me the first item:
array(22) {
[0]=> object(stdClass)#318 (5) {
["fulltext"]=> string(212) "Yesterday ..."
["alias"]=> string(3) "237"
["metakey"]=> string(7) "France"
["publish_up"]=> string(19) "2011-11-15 09:06:39"
}