【发布时间】:2013-02-20 01:20:42
【问题描述】:
我有一个 xml 文件,其中包含大约 60 本书,我需要使用 php 从借来的计数中按升序排列到目前为止,我的代码显示了所有书籍但不会排序?任何帮助都会非常感激
PHP
<?php
$xmlassignDoc = new DOMDocument();
$xmlassignDoc->load("books.xml");
$books = $xmlBookDoc->getElementsByTagName("item");
foreach($books as $list)
{
$course = $list->getElementsByTagName("course");
$course = $course->item(0)->nodeValue;
//HERE is where the GET function will be
if ($course == "CC150")
{
print_r($array);
$id = $list->getAttribute("id");
echo "<b>Book ID: </b> $id <br>";
$title = $list->getElementsByTagName("title");
$title = $title->item(0)->nodeValue;
echo "<b>Title: </b> $title <br>";
$isbn = $list->getElementsByTagName("isbn");
$isbn = $isbn->item(0)->nodeValue;
echo "<b>ISBN: </b> $isbn <br>";
$borrowed = $list->getElementsByTagName("borrowedcount");
$borrowed = $borrowed->item(0)->nodeValue;
echo "<b>Borrowed Count: </b> $borrowed <br>";
echo "<br>";
}
}
//print $xmlBookDoc->saveXML();
?>
xml 文件
<?xml version="1.0" encoding="utf-8"?>
<bookcollection>
<items>
<item id="51390">
<title>Management of systems development /</title>
<isbn>0091653215</isbn>
<url>http://library.hud.ac.uk/catlink/bib/51390</url>
<borrowedcount>45</borrowedcount>
<courses>
<course>CC140</course>
<course>CC210</course>
</courses>
</item>
<item id="483">
<title>Database systems management and design /</title>
<isbn>0877091153</isbn>
<url>http://library.hud.ac.uk/catlink/bib/483</url>
<borrowedcount>28</borrowedcount>
<courses>
<course>CC140</course>
</courses>
</item>
<item id="585842">
<title>E-learning skills /</title>
<isbn>0230573126</isbn>
<url>http://library.hud.ac.uk/catlink/bib/585842</url>
<borrowedcount>5</borrowedcount>
<courses>
<course>CC157</course>
</courses>
</item>
【问题讨论】:
-
如果您不隐式编写一些对其进行排序的代码,您就不能期望对 XML 文件进行排序。尝试更改您的循环,以便将所有值保存到一个数组中,然后使用自定义排序函数,您可以在其中选择要排序的列。
-
抱歉忘记写了,我确实尝试过 ksort 等,但它没有工作,所以删除了我目前所拥有的代码
-
这是干什么用的?
if ($course == "CC150");您是否还根据课程进行过滤? -
杰克我必须添加用户输入,这样他们就可以输入不同的课程,然后按借阅次数的升序显示该课程中的书籍