cel*_*ian 3 php xml directory loops simplexml
我正在构建一个简单的应用程序.它是在线订购系统的用户界面.
基本上,系统将这样工作:
其他公司将其采购订单上传到我们的FTP服务器.这些订单是简单的XML文件(包含客户数据,地址信息,订购产品和数量......)
我在HTML5,jQuery和CSS中构建了一个简单的用户界面 - 全部由PHP提供支持.
PHP读取订单的内容(使用SimpleXML的内置功能)并将其显示在网页上.
所以,它是一个Web应用程序,应该始终在办公室的浏览器中运行.PHP应用程序将显示所有订单的内容.每十五分钟左右,该应用程序将检查新订单.
现在,我的应用程序能够读取单个XML文件的内容,并在页面上以一种很好的方式显示它.
我当前的代码如下所示:
// pick a random order that I know exists in the Order directory:
$xml_file = file_get_contents("Order/6366246.xml",FILE_TEXT);
$xml = new SimpleXMLElement($xml_file);
// start echo basic order information, like order number:
echo $xml->OrderHead->ShopPO;
// more information about the order and the customer goes here…
echo "<ul>";
// loop through each order line, and echo all quantities and products:
foreach ($xml->OrderLines->OrderLine as $orderline) {
"<li>".$orderline->Quantity." st.</li>\n".
"<li>".$orderline->SKU."</li>\n";
}
echo "</ul>";
// more information about delivery options, address information etc. goes here…
Run Code Online (Sandbox Code Playgroud)
那么,这是我的代码.很简单.它只需要做一件事 - 在屏幕上打印出所有订单文件的内容 - 这样我和我的同事就可以看到订单,确认并交付订单.
而已.
但是现在 - 正如你所看到的 - 我一次只选择一个订单,位于订单目录中.但是我如何遍历整个Order目录,并读取a并显示每个订单的内容(如上所述)?
我被卡住了.我真的不知道如何在目录中获取所有(xml)文件,然后对文件执行某些操作(比如读取它们并像我想的那样回显数据).
- >我真的很感激一些帮助.我对PHP /服务器端编程不是很有经验,所以如果你能在这里帮助我,我将非常感激.
非常感谢提前!
//Björn(我在com com的名人)
use*_*291 10
欢迎来到SO.
尝试glob函数:
$files = glob("Order/*xml");
if (is_array($files)) {
foreach($files as $filename) {
$xml_file = file_get_contents($filename, FILE_TEXT);
// and proceed with your code
}
}
Run Code Online (Sandbox Code Playgroud)