Take the 2-minute tour ×
Joomla Stack Exchange is a question and answer site for Joomla! administrators, users, developers and designers. It's 100% free, no registration required.

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):

<?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 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!

EDIT 1

<?php
    var_dump($jfeed1)
?>

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

2 Answers 2

up vote 4 down vote accepted

There are probably other differences (or maybe not: http://stackoverflow.com/questions/2193049/php-objects-vs-arrays) 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! http://docs.joomla.org/Selecting_data_using_JDatabase

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! –  Arone Aug 30 at 10:04
1  
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 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! –  Arone Aug 30 at 15:19

You can use the following query:

$jquery1 = $jdb1->getQuery(true)
                ->select('`fulltext`, alias, metakey, publish_up, metadesc')
                ->from('#__content')
                ->where('`catid` = 22')
                ->where('`state` = 1')
                ->order('publish_up desc'); // OR ->order('publish_up asc');
share|improve this answer
    
Thanks for this @Fari! –  Arone Aug 30 at 10:06
1  
Please don't forget about escaping columns and values ($db->quote() and $db->quoteName()) ;) –  Lodder Aug 30 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 at 9:10

Your Answer

 
discard

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.