我有一个新闻小部件,它使用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为数组,在
中给出null答案 0 :(得分:0)
考虑将所有提取的标记值传递到多维数组$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>";
}