Joomla Stack Exchange is a question and answer site for Joomla! administrators, users, developers and designers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a .php that retrieve fields from my Joomla database and then echo them with a foreach function (here are just the relevant part of the code):

  $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):
  $oldLocale = setlocale(LC_TIME, 'en_US');
  echo strftime("%a %d %b %Y", strtotime($jitem1->publish_up)); 
  setlocale(LC_TIME, $oldLocale);
 <div><?php echo $jitem1->fulltext; ?></div>
<?php endforeach; ?>

So everything works great except that my array entries are sort 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 giving 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 tuto, so I am missing a lot of fondamental notions* essential when I have to put theory into practice. I am confused here about array and object and I can't figure out what I am supposed to do to solve this warning. Thanks for your help!



gives me for 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" }
share|improve this question
up vote 4 down vote accepted

There are probably other differences (or maybe not: between objects and arrays, but it is mostly a style difference (at least when using a stdClass object).

For objects, you use the syntax of $variable->property and for arrays you use $variable['property']. var_dump will usually show an array-style syntax, though that is not what you want to use if you see stdClass, meaning it is an object.

So your date_compare function should use $a->publish_up and $b->publish_up within the strtotime calls. That should get that to actually use the values and thus actually sort.

You can also skip all the sorting bit in PHP by just ordering in the SQL statement:

$jquery1 = "SELECT `fulltext`,alias,metakey,publish_up,metadesc FROM kms_content  WHERE catid=22 AND state= '1' ORDER BY publish_up DESC";
$jdb1->setQuery( $jquery1 );
$jfeed1= $jdb1->loadObjectList();

(Not sure exactly what order you want, so if it is backwards, change the DESC at the end to ASC (descending v. ascending...)

And finally, the obligatory "other Joomla best practices that you should use" section. (Obviously your code can work without these, but they leverage some of the nicer features of Joomla.)

The biggest one is to avoid putting the table prefix in the query. Always reference a table by starting with #__. If you ever share code between two sites, it is likely you'll change the prefix and that will be the only thing that needs to change in the code to make it work in the new site, so save yourself trouble and just always type #__.

Second, Joomla's query object is a great way to build queries and have it automatically work for other database types (even if you never use them now) and also just has it escape things for you. Definitely worth learning about!

share|improve this answer
Great! Thanks so much David for your answer and for all your advices. It's working great! I didn't know about the #__ trick: that's really cool! I will dig into the Joomla's query object to improve my codes! Thanks again David! – Enora Nedelec Aug 30 '14 at 10:04
Keep asking questions, and you'll probably keep getting cool tricks. I know a lot of the people that answer questions on here like to provide them! :) – David Fritsch Aug 30 '14 at 15:06
:)! There are so many things that are still a great mystery to me, that you can be sure I will stick around to ask questions and get more tricks! – Enora Nedelec Aug 30 '14 at 15:19

You can use the following query:

$jquery1 = $jdb1->getQuery(true)
                ->select('`fulltext`, alias, metakey, publish_up, metadesc')
                ->where('`catid` = 22')
                ->where('`state` = 1')
                ->order('publish_up desc'); // OR ->order('publish_up asc');
share|improve this answer
Thanks for this @Fari! – Enora Nedelec Aug 30 '14 at 10:06
Please don't forget about escaping columns and values ($db->quote() and $db->quoteName()) ;) – Lodder Aug 30 '14 at 22:04
@Lodder Sure, but when column names and values are not variable, it's not necessary to use $db->quoteName() and $db->quote() ;) – Farahmand Aug 31 '14 at 9:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.