-1
空給我,從我的Joomla數據庫中檢索字段,然後用一個foreach功能呼應他們以.php:按日期排序的foreach給予警告:usort()預計參數1是陣列,在
<?php
$jquery1 = "SELECT `fulltext`,alias,metakey,publish_up,metadesc FROM xxx_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; ?>
不過,我想按日期排序在foreach功能,我已經嘗試做這件事的兩種不同方式(基於其他SE的問題),但每次我得到這樣的警告:
Warning: usort() expects parameter 1 to be array, null given in...
這是我第一次嘗試:
$jfeed1= $jdb1->loadObjectList();
function date_compare($a, $b)
{
$t1 = strtotime($a['publish_up']);
$t2 = strtotime($a['publish_up']);
return $t1 - $t2;
}
usort($jitem1, 'date_compare');
foreach ($jfeed1 as $jitem1):
這裏是我的第二次嘗試:
$jfeed1= $jdb1->loadObjectList();
usort($jfeed1->publish_up, function($a, $b) {
$date1 = DateTime::createFromFormat('Y-m-d H:i:s', $a);
$date2 = DateTime::createFromFormat('Y-m-d H:i:s', $b);
return $date1 > $date2;
});
foreach ($jfeed1 as $jitem1):
我「教訓」 PHP只是看着代碼和政黨成員,所以我錯過了很多fondamental觀念就是爲什麼我不明白是什麼我應該這樣做來解決這個警告。謝謝你的幫助!
編輯1: 功能
<?php
var_dump($jfeed1)
?>
給我的第一個項目:
array(22) { [0]=> object(stdClass)#318 (5) { ["fulltext"]=> string(212) "Yesterday ..." ["alias"]=> string(3) "237" ["metakey"]=> string(7) "Romania" ["publish_up"]=> string(19) "2011-11-15 09:06:39" }
EDIT 2
$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):
輸入在此函數是不是一個數組,但NULL(=無)。你的變量沒有價值。也許一個函數/方法的錯字不會返回任何東西。 – Martijn 2014-08-28 09:08:28
在loadObjectList()調用之後'$ jfeed1'的內容是什麼? – Sugar 2014-08-28 09:08:35
@ClémentMalet。那麼我不知道。我在我的.php文件中搜索了$ jfeed1,並且它們都在上面的代碼中。我已經在這裏上傳了我的全部.php文件http://www.docspal.com/viewer?id=twgcbiyc-6316723。非常感謝你的幫助! – Enora 2014-08-28 09:19:29