我有一个新闻小部件,它使用DOM Xml拉入多个Xml文件来显示。我正在设法弄清楚如何让文章按日期顺序显示。下面是小部件的代码:
$dh = opendir('./xml/');
$fileCount = 0;
while ($file = readdir($dh) and $fileCount < 3) {
if (preg_match("/^..?$/", $file)) {
continue;
}
$open = "./xml/".$file;
$xml = domxml_open_file($open);
//we need to pull out all the things from this file that we will need to
//build our links
$root = $xml->root();
$stat_array = $root->get_elements_by_tagname("status");
$status = extractText($stat_array);
$date_array = $root->get_elements_by_tagname("date");
$date = extractText($date_array);
$ab_array = $root->get_elements_by_tagname("abstract");
$abstract = extractText($ab_array);
$h_array = $root->get_elements_by_tagname("headline");
$headline = extractText($h_array);
$img_array = $root->get_elements_by_tagname("image");
$image = extractText($img_array);
$lead_array = $root->get_elements_by_tagname("para-intro");
$para["intro"] = extractText($lead_array);
if ($status != "live") {
continue;
}
echo "<div class='"col-md-12 newsbox'"><img style='"margin-bottom: 10px;'" width='"100%'" src='"images/news/".$image."'"><div class='"newsboxtext'"><a href='"showArticle.php?file=".$file. "'"><h2 class='"mainheadline2'"> ".$headline . "</h2></a><a href='"showArticle.php?file=".$file . "'"><button style='"margin-top:5px;'" type='"button'" class='"btn btn-sm btn-default'">+ Read More</button></a></div><hr class='"linedivider'">
</div>";
$fileCount++;
}
这是我用来尝试排序结果的:
function date_compare($a, $b)
{
$date1 = strtotime($a['date']);
$date2 = strtotime($b['date']);
return $date1 - $date2;
}
usort($file, 'date_compare');
但是我收到了这个错误。警告:usort()期望参数1为array,
考虑将所有提取的标签值传递到多维数组$xmlData
中,然后对该数组进行排序,然后从排序后的数组中返回web html数据:
$r = 0;
$xml = new DOMDocument('1.0', 'UTF-8');
function extractText($tag) {
foreach ($tag as $item) {
$value = $item->nodeValue;
}
return $value;
}
while ($file = readdir($dh) and $fileCount < 3) {
if (preg_match("/^..?$/", $file)) {
continue;
}
$open = "./xml/".$file;
$xml->load($open);
//we need to pull out all the things from this file that we will need to
//build our links
$xmldata = [];
$xmldata[$r]['file'] = $file;
$stat_array = $xml->getElementsByTagName("status");
$xmldata[$r]['status'] = extractText($stat_array);
$date_array = $xml->getElementsByTagName("date");
$xmldata[$r]['date']= extractText($date_array);
$ab_array = $xml->getElementsByTagName("abstract");
$xmldata[$r]['abstract'] = extractText($ab_array);
$h_array = $xml->getElementsByTagName("headline");
$xmldata[$r]['headline'] = extractText($h_array);
$img_array = $xml->getElementsByTagName("image");
$xmldata[$r]['image'] = extractText($img_array);
$lead_array = $xml->getElementsByTagName("para-intro");
$xmldata[$r]['paraintro'] = extractText($lead_array);
if ($status != "live") {
continue;
}
$fileCount++;
$r++;
}
// YYYY-MM-DD STRINGS STILL SORTS IN DAILY ORDER (I.E., '2015-01-01' < '2015-12-31')
usort($xmldata, function($a, $b) {
return strcmp($a['date'], $b['date']);
});
$xmlcount = sizeof($xmldata[0]);
// ITERATE THROUGH LOOP EVERY NEEDED ITEM IN SORT ORDER OF ARRAY
for ($i = 0; $i <= ($xmlcount - 1); $i++) {
echo "<div class='"col-md-12 newsbox'">
<img style='"margin-bottom: 10px;'" width='"100%'" src='"images/news/".$xmldata[$i]['image']."'">
<div class='"newsboxtext'">
<a href='"showArticle.php?file=".$xmldata[$i]['file']. "'">
<h2 class='"mainheadline2'"> ".$xmldata[$i]['headline']. "</h2>
</a>
<a href='"showArticle.php?file=".$xmldata[$i]['file']. "'">
<button style='"margin-top:5px;'" type='"button'" class='"btn btn-sm btn-default'">+ Read More</button>
</a>
</div><hr class='"linedivider'">
</div>";
}