0

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>";
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by cereal
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 :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.