Jump to content

PHP XML Loop - please help


janniesekind

Recommended Posts

I am about to cry.

 

I have this

 

<title>title of post</title>

<link>http-linktopost</link>

<pubDate>Mon, 14 Mar 2011 18:34:49 +0000</pubDate>

<creator>Cheng2011</creator>

 

<postmeta>

    <meta_key>uniqueID1</meta_key>

    <meta_value>value for meta key</meta_value>

</postmeta>

<postmeta>

    <meta_key>uniqueID2</meta_key>

    <meta_value>value for meta key</meta_value>

</postmeta>

<postmeta>

    <meta_key>uniqueID3</meta_key>

    <meta_value>value for meta key</meta_value>

</postmeta>

 

So, here's the PHP

 

<?php

// displays all the file nodes

if(!$xml=simplexml_load_file('xml.xml')){

    trigger_error('Error reading XML file',E_USER_ERROR);

}

echo 'Displaying contents of XML file...<br />';

foreach($xml as $item){

    echo 'Title: '.$item->title.'<br>';

echo 'Link: '.$item->link.'<br>';

echo 'pubDate: '.$item->pubDate.'<br>';

echo 'creator: '.$item->postmeta->meta_key->??????????????.'<br>';

echo '<br><br>';

}

?>

 

So I get what I want, I get the title, link, pub date. But how can I get the value for : uniqueID3

 

In MySQL I would say something like, WHERE meta_key=uniqueID3

 

 

My problem is, I have multiple instances

 

<title>title of post</title>

<link>http-linktopost</link>

<pubDate>Mon, 14 Mar 2011 18:34:49 +0000</pubDate>

<creator>Cheng2011</creator>

 

<postmeta>

    <meta_key>uniqueID1</meta_key>

    <meta_value>value for meta key</meta_value>

</postmeta>

<postmeta>

    <meta_key>uniqueID2</meta_key>

    <meta_value>value for meta key</meta_value>

</postmeta>

<postmeta>

    <meta_key>uniqueID3</meta_key>

    <meta_value>value for meta key</meta_value>

</postmeta>

 

 

 

<title>title of post</title>

<link>http-linktopost</link>

<pubDate>Mon, 14 Mar 2011 18:34:49 +0000</pubDate>

<creator>Cheng2011</creator>

 

<postmeta>

    <meta_key>uniqueID1</meta_key>

    <meta_value>value for meta key</meta_value>

</postmeta>

<postmeta>

    <meta_key>uniqueID2</meta_key>

    <meta_value>value for meta key</meta_value>

</postmeta>

<postmeta>

    <meta_key>uniqueID3</meta_key>

    <meta_value>value for meta key</meta_value>

</postmeta>

 

 

 

<title>title of post</title>

<link>http-linktopost</link>

<pubDate>Mon, 14 Mar 2011 18:34:49 +0000</pubDate>

<creator>Cheng2011</creator>

 

<postmeta>

    <meta_key>uniqueID1</meta_key>

    <meta_value>value for meta key</meta_value>

</postmeta>

<postmeta>

    <meta_key>uniqueID2</meta_key>

    <meta_value>value for meta key</meta_value>

</postmeta>

<postmeta>

    <meta_key>uniqueID3</meta_key>

    <meta_value>value for meta key</meta_value>

</postmeta>

 

But when I use the foreach function to get the postmeta data - I get the postmeta data at each instance, for ALL the posts.

 

Please help

Link to comment
https://forums.phpfreaks.com/topic/259683-php-xml-loop-please-help/
Share on other sites

Let me rephrase

 

I'm trying for the last 2 days, with 2 hours sleep, and viewing hundreds of forums, to achieve this :

 

XMl data

 

<postid>1</postid>

<posttitle>post title 1</posttitle>

<date>2012-01-01</date>

<postmeta>

<meta_key>Banner1URLforpost1</meta_key>

<meta_value>http://banner1forpost1.com</meta_value>

</postmeta>

<postmeta>

<meta_key>Banner2URLforpost1</meta_key>

<meta_value>http://banner2forpost1.com</meta_value>

</postmeta>

<postmeta>

<meta_key>Banner3URLforpost1</meta_key>

<meta_value>http://banner3forpost1.com</meta_value>

</postmeta>

 

 

<postid>1</postid>

<posttitle>post title 2</posttitle>

<date>2012-01-01</date>

<postmeta>

<meta_key>Banner1URLforpost22/meta_key>

<meta_value>http://banner1forpost2.com</meta_value>

</postmeta>

<postmeta>

<meta_key>Banner2URLforpost2</meta_key>

<meta_value>http://banner2forpost2.com</meta_value>

</postmeta>

<postmeta>

<meta_key>Banner3URLforpost2</meta_key>

<meta_value>http://banner3forpost2.com</meta_value>

</postmeta>

 

 

<postid>1</postid>

<posttitle>post title 3</posttitle>

<date>2012-01-01</date>

<postmeta>

<meta_key>Banner1URLforpost3</meta_key>

<meta_value>http://banner1forpost3.com</meta_value>

</postmeta>

<postmeta>

<meta_key>Banner2URLforpost3</meta_key>

<meta_value>http://banner1forpost3.com</meta_value>

</postmeta>

<postmeta>

<meta_key>Banner3URLforpost3</meta_key>

<meta_value>http://banner1forpost3.com</meta_value>

</postmeta>

 

 

Now, my PHP looks something like this :

<?php

// displays all the file nodes

if(!$xml=simplexml_load_file('xml.xml')){

    trigger_error('Error reading XML file',E_USER_ERROR);

}

echo 'Displaying contents of XML file...<br />';

foreach($xml as $item){

    echo 'postid: '.$item->postid.'<br>';

echo 'posttitle: '.$item->posttitle.'<br>';

echo 'date: '.$item->date.'<br>';

echo '<br><br>';

}

 

?>

 

 

So, the output is :

 

postid: 1

posttitle: post title 1

date:2012-01-01

 

postid: 2

posttitle: post title 2

date:2012-01-01

 

postid: 3

posttitle: post title 1

date:2012-01-01

 

Once I add a foreach loop within the foreachloop, it shows me the metakey and meta_value of all the items. So It looks like this :

 

postid: 1

posttitle: post title 1

date:2012-01-01

metakey: Banner1URLforpost1

metavalue:http://banner1forpost1.com

metakey: Banner2URLforpost1

metavalue:http://banner2forpost1.com

metakey: Banner3URLforpost1

metavalue:http://banner3forpost1.com

metakey: Banner1URLforpost2

metavalue:http://banner1forpost2.com

metakey: Banner2URLforpost2

metavalue:http://banner2forpost2.com

metakey: Banner3URLforpost2

metavalue:http://banner3forpost2.com

metakey: Banner1URLforpost3

metavalue:http://banner1forpost3.com

metakey: Banner2URLforpost3

metavalue:http://banner2forpost3.com

metakey: Banner3URLforpost3

metavalue:http://banner3forpost3.com

 

postid: 2

posttitle: post title 2

date:2012-01-01

metakey: Banner1URLforpost1

metavalue:http://banner1forpost1.com

metakey: Banner2URLforpost1

metavalue:http://banner2forpost1.com

metakey: Banner3URLforpost1

metavalue:http://banner3forpost1.com

metakey: Banner1URLforpost2

metavalue:http://banner1forpost2.com

metakey: Banner2URLforpost2

metavalue:http://banner2forpost2.com

metakey: Banner3URLforpost2

metavalue:http://banner3forpost2.com

metakey: Banner1URLforpost3

metavalue:http://banner1forpost3.com

metakey: Banner2URLforpost3

metavalue:http://banner2forpost3.com

metakey: Banner3URLforpost3

metavalue:http://banner3forpost3.com

 

postid: 3

posttitle: post title 1

date:2012-01-01

metakey: Banner1URLforpost1

metavalue:http://banner1forpost1.com

metakey: Banner2URLforpost1

metavalue:http://banner2forpost1.com

metakey: Banner3URLforpost1

metavalue:http://banner3forpost1.com

metakey: Banner1URLforpost2

metavalue:http://banner1forpost2.com

metakey: Banner2URLforpost2

metavalue:http://banner2forpost2.com

metakey: Banner3URLforpost2

metavalue:http://banner3forpost2.com

metakey: Banner1URLforpost3

metavalue:http://banner1forpost3.com

metakey: Banner2URLforpost3

metavalue:http://banner2forpost3.com

metakey: Banner3URLforpost3

metavalue:http://banner3forpost3.com

 

So, it shows me the postmeta for ALL the item instances.

 

But what I need is :

 

postid: 1

posttitle: post title 1

date:2012-01-01

metakey: Banner1URLforpost1

metavalue:http://banner1forpost1.com

metakey: Banner2URLforpost1

metavalue:http://banner2forpost1.com

metakey: Banner3URLforpost1

metavalue:http://banner3forpost1.com

 

postid: 2

posttitle: post title 2

date:2012-01-01

metakey: Banner1URLforpost2

metavalue:http://banner1forpost2.com

metakey: Banner2URLforpost2

metavalue:http://banner2forpost2.com

metakey: Banner3URLforpost2

metavalue:http://banner3forpost2.com

 

postid: 3

posttitle: post title 1

date:2012-01-01

metakey: Banner1URLforpost3

metavalue:http://banner1forpost3.com

metakey: Banner2URLforpost3

metavalue:http://banner2forpost3.com

metakey: Banner3URLforpost3

metavalue:http://banner3forpost3.com

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.