954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

need little help displaying xml data from two website in php

hi
need little help displaying xml data from two website

need to display data like this
1-website a
2-website b
3-website a
4-website b

i am using this code

can any one tell me how i can display two xml data from different websites
i am using this
what i have to do that this can display date in same foreach loop

function getFeedAll() {

$content = file_get_contents("weblink");

$x = new SimpleXmlElement($content);

echo "<ul>";

foreach($x->channel->item as $entry) {
echo "
<li>
<a href='$entry->link' title='$entry->title' target='_new'> " . $entry->title . "</a>
</li>";
}

echo "</ul>";
}
hotice47
Newbie Poster
23 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

Here's an example:

------------
file: a.xml
------------

<xml>
	<channel>
		<item>
			<title>a title 1</title>
			<link>a_link_1</link>
		</item>
		<item>
			<title>a title 2</title>
			<link>a_link_2</link>
		</item>
		<item>
			<title>a title 3</title>
			<link>a_link_3</link>
		</item>
	</channel>
</xml>


------------
file: b.xml
------------

<xml>
	<channel>
		<item>
			<title>b title 1</title>
			<link>b_link_1</link>
		</item>
		<item>
			<title>b title 2</title>
			<link>b_link_2</link>
		</item>
		<item>
			<title>b title 3</title>
			<link>b_link_3</link>
		</item>
		<item>
			<title>b title 4</title>
			<link>b_link_4</link>
		</item>
	</channel>
</xml>


---------------
file: read.php
---------------

<?php
$a = file_get_contents('a.xml');
$b = file_get_contents('b.xml');
$xa = new SimpleXmlElement($a);
$xb = new SimpleXmlElement($b);	

$na = $xa->channel->item->count();
$nb = $xb->channel->item->count();
$c = ($na == $nb) ? $na : max($na,$nb);	# how many loops? in this example 4 because b.xml has 4 items

if($c != 0)
{
	echo '
	<ul>
	';
	for($i = 0; $i < $c; $i++)
	{
		if($xa->channel->item[$i] == true)
		{
			echo '
			<li><a href="'.$xa->channel->item[$i]->link.'">'.$xa->channel->item[$i]->title.'</a></li>
			';
		}
		
		if($xb->channel->item[$i] == true)
		{
			echo '
			<li><a href="'.$xb->channel->item[$i]->link.'">'.$xb->channel->item[$i]->title.'</a></li>
			';
		}
	}
	echo '
	</ul>
	';
}
?>

Just change $c to (for example) $c = 2; if you want to control how many links your application is going to display. Bye :)

cereal
Master Poster
709 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: