Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    335
    Thanks
    2
    Thanked 49 Times in 48 Posts

    Simple PHP XML parsing

    I've already posted this to the XML forum...I've just not been answered yet as I need the answer quickly

    I'm trying to break down this XML file containing the below code:

    Code:
    <stockreport>
    <timestamp>08/09/2012 08:46:02</timestamp>
    <version>2.0</version>
    <products>
    <product type="main">
             <code>1</code>
             <name>Table</name>
             <stock>43</stock>
             <status>In Stock</status>
    </product>
    <product type="main">
             <code>2</code>
             <name>Chair</name>
             <stock>64</stock>
             <status>In Stock</status>
    </product>
    </stockreport>
    
    I want inorder to display it in a table. I'm using the below PHP code:
    PHP Code:
    <?php

    $url 
    file_get_contents('http://www.topshinellc.com/feed/status-xml.asp.xml'); //The path to the XML file
    $data_s = new SimpleXMLElement($url);
    echo 
    "<table border='1' >";
    echo 
    "<tr><th width='200'><h4>code</h4></th><th width='200'><h4>Name</h4></th><th width='200'><h4>Stock</h4></th><th width='200'><h4>Status</h4></td></th></tr>";
    foreach(
    $data_s as $data)
    {
    echo <<<EOF
    <tr>
    <td width='200'>$key
    {$data->product->code}</td>
    <td width='200'>
    {$data->product->name}</td>
    <td width='200'>
    {$data->product->stock}</td>
    <td width='200'>
    {$data->product->status}</td>
    </tr>
    EOF;
    }
    echo 
    "</table>";
    Instead of getting two rows, only the first row is being displayed.
    Like This:
    Code:
    Code       Name      Stock        Status
    1          Table      64         In Stock
    What is wrong or missing in the code?. Of course this code will need to parse hundreds of such XML tags containing the data.
    Last edited by Redcoder; 09-09-2012 at 12:55 PM.

  • #2
    Regular Coder
    Join Date
    May 2011
    Posts
    242
    Thanks
    1
    Thanked 57 Times in 56 Posts
    PHP Code:
    foreach($data_s->products->product as $data)
    {
            echo 
    "<tr>\n";
            echo 
    "<td width='200'>{$data->code}</td>\n";
            echo 
    "<td width='200'>{$data->name}</td>\n";
            echo 
    "<td width='200'>{$data->stock}</td>\n";
            echo 
    "<td width='200'>{$data->status}</td>\n";
            echo 
    "</tr>\n";


  • Users who have thanked gvre for this post:

    Redcoder (09-09-2012)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •