Go Back   CodingForums.com > :: Client side development > XML

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-08-2012, 06:58 PM   PM User | #1
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
Simple PHP XML parsing

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 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.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk

Last edited by Redcoder; 09-10-2012 at 06:01 PM..
Redcoder is offline   Reply With Quote
Old 09-09-2012, 05:41 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
In your xml file you don't close the <products> tag.

Code:
<td width='200'>$key{$data->product->code}</td>
$key not defined. You get one row if $key is removed.
sunfighter is offline   Reply With Quote
Old 09-09-2012, 07:22 PM   PM User | #3
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
I didn't get an answer here so I figured that it was because of the PHP part therefore maybe most of the people who visited the XML forum used other languages. I reposted it http://www.codingforums.com/showthread.php?t=272632 and got an answer.

But thanks for pointing that out.

Off-topic: Isn't there a way of marking a thread as resolved?
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Old 09-10-2012, 04:17 AM   PM User | #4
Alex Vincent
Moderator


 
Join Date: May 2002
Location: Hayward, CA
Posts: 1,427
Thanks: 1
Thanked 19 Times in 17 Posts
Alex Vincent is on a distinguished road
FYI, we really don't like it when the same question is asked in more than one forum. Moderators and WA are happy to move threads from one thread to another, as needed. Shame on you!

I actually don't know how to mark a thread resolved.
__________________
"The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
June 30, 2001
author, Verbosio prototype XML Editor
author, JavaScript Developer's Dictionary
https://alexvincent.us/blog
Alex Vincent is offline   Reply With Quote
Old 09-10-2012, 04:56 PM   PM User | #5
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
Quote:
Originally Posted by Alex Vincent View Post
I actually don't know how to mark a thread resolved.
How do you not know? Your a MOD dammit.


The option is either there or it isn't there.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Old 09-10-2012, 05:58 PM   PM User | #6
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
Edit your original post. Next to the title is a dropdown. That will set things to resolved.
sunfighter is offline   Reply With Quote
Old 09-10-2012, 06:03 PM   PM User | #7
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
Quote:
Originally Posted by Alex Vincent View Post
FYI, we really don't like it when the same question is asked in more than one forum. Moderators and WA are happy to move threads from one thread to another, as needed. Shame on you!

I actually don't know how to mark a thread resolved.
Quote:
Originally Posted by sunfighter View Post
Edit your original post. Next to the title is a dropdown. That will set things to resolved.
Neat. Although I thought MOD's should know that hehe.

__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:06 AM.


Advertisement
Log in to turn off these ads.