PHP: How do I scroll through each XML file in a directory?

I am creating a simple application. This is the user interface to the online ordering system.

In principle, the system will work as follows:

  • Other companies upload their orders to our FTP server. These orders are simple XML files (containing things like customer data, address information, ordered products and quantities ...)

  • I created a simple user interface in HTML5, jQuery and CSS - everything works in PHP.

  • PHP reads the contents of the order (using the built-in SimpleXML functions) and displays it on the web page.

So, this is a web application that should always work in a browser in the office. The PHP application will display the contents of all orders. Every fifteen minutes or so, the app checks for new orders.

How do I iterate over all XML files in a directory?

Now my application can read the contents of one XML file and display it on the page.

My current code is as follows:

// 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…

So this is my code. Pretty simple. Only one thing is needed: print the contents of all the order files on the screen so that my colleagues and I can see the order, confirm it and deliver it.

What is it.

But right now - as you can see - I am choosing one order at a time, located in the Order directory. But how can I go through the entire catalog of orders and read a and display the contents of each order (for example, above)?

I am stuck. I really don't know how you get all the (xml) files in the directory, and then do something with the files (for example, read them and the echo data as I want).

- > . PHP/, , , .

!

// ( )

+3
1

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
     }

}
+9

Source: https://habr.com/ru/post/1737863/


All Articles