PHP XML Foreach shows 1 review - php

Iam trying to loop a XML file with Foreach but doesn't work. There are like 30 reviews in the XML File but only shows one. It shows the first person in the list but then on the bottom.
Iam trying to get better at PHP so dont know allot about it for now.
This is the code that i use.
<?php
$url = 'https://mobiliteit.klantenvertellen.nl/xml/autorijschool-
wezemer%20' or die ('Niet verbonden');
$xml = simplexml_load_file($url);
foreach ($xml as $rijschool){
echo 'Voornaam: '.$rijschool->beoordeling->voornaam.'<br>';
echo 'Achternaam: '.$rijschool->beoordeling->achternaam.'<br>';
echo 'Woonplaats: '.$rijschool->beoordeling->woonplaats.'<br>';
echo 'Beschrijving: '.$rijschool->beoordeling->beschrijving.'<br>';
echo 'Aanbeveling: '.$rijschool->beoordeling->aanbeveling.'<br>';
echo 'Service: '.$rijschool->beoordeling->service.'<br>';
echo 'Deskundigheid: '.$rijschool->beoordeling->deskundigheid.'<br>';
echo 'Prijskwaliteit: '.$rijschool->beoordeling-
>prijskwaliteit.'<br>';
echo 'Gemiddelde: '.$rijschool->beoordeling->gemiddelde.'<br>'.'<br>';
}
?>
Edit: here is the XML file Link https://mobiliteit.klantenvertellen.nl/xml/autorijschool-wezemer%20
And here is what iam getting what current code shows

I think this is what you're trying to do:
<?php
$url = 'https://mobiliteit.klantenvertellen.nl/xml/autorijschool-wezemer%20';
$xml = simplexml_load_file($url);
foreach ($xml->beoordelingen->beoordeling as $rijschool){
echo 'Voornaam: '.$rijschool->voornaam.'<br>';
echo 'Achternaam: '.$rijschool->achternaam.'<br>';
echo 'Woonplaats: '.$rijschool->woonplaats.'<br>';
echo 'Beschrijving: '.$rijschool->beschrijving.'<br>';
echo 'Aanbeveling: '.$rijschool->aanbeveling.'<br>';
echo 'Service: '.$rijschool->service.'<br>';
echo 'Deskundigheid: '.$rijschool->deskundigheid.'<br>';
echo 'Prijskwaliteit: '.$rijschool->prijskwaliteit.'<br>';
echo 'Gemiddelde: '.$rijschool->gemiddelde.'<br>'.'<br>';
}
?>
The problem you are having is that your foreach is iterating over the topmost node, but you want to iterate over a node lower down in the tree.

Related

Echo XML Elements with file_get_contents

i'm successfully managing to pull in the information in this feed:
http://api.zoopla.co.uk/api/v1/zed_index?area=yo1&output_type=outcode&api_key=XXXXMYAPIKEYGOESHEREXXXXX
I'm doing this with the following code:
<p><?php $postcode = get_the_title(); $str = urlencode($postcode); $url = "http://api.zoopla.co.uk/api/v1/zed_index?area=$str&output_type=outcode&api_key=5dj2d5x8kd2z2vnk9g52gpap"; $string = file_get_contents($url); echo $string;?></p>
However, this just echos the following output:
DE45 http://www.zoopla.co.uk/home-values/de45 53.258037 53.138911 -1.580861 -1.79776 England Derbyshire 53.198474 -1.6893105 DE45 368929 375424 362103 372926 333441 329349 322644 368056
How could i adapt my existing code to successfully echo individual elements from the feed, for example just the following fields wrapped in tags:
zed_index
zed_index_1year
zed_index_2year
Thanks for your help!
You could use simplexml_load_file() to get an array which will contains every of your XML tags :
<p>
<?php
$XML_url = 'http://api.zoopla.co.uk/api/v1/zed_index?area=yo1&output_type=outcode&api_key=XXXXMYAPIKEYGOESHEREXXXXX';
$XML_parsed = simple_xml_load($XML_url);
// print for debug
echo '<pre>';
print_r( $XML_parsed );
echo '</pre>';
// Access one of the tag
$tagName = $XML_parsed['tagName'];
// Access a nested tag
$nestedTag = $XML_parsed['first_tag']['second_tag'];
?>
</p>

phpQuery: Replace all occurrences of text with another

I am trying to parse a website homepage to convert it into xml file to be used as an api in my app.
So far I have successfully done so. However, the parsed text contains the & (ampersand) character which causes the XML parser to fail.
I am looking for a solution that doesn't use the CDATA or doesn't output CDATA in the XML file.
I want to replace & with and at every occurrence. What phpQuery method should I use?
This causes error in browser because the text() method returns a text with
& character in it.
require('phpQuery/phpQuery.php');
$all=phpQuery::newDocumentFileHTML('BPUT.htm', $charset = 'utf-8');
$links = $all['a.myblue'];
echo '<notice>';
foreach ($links as $link) {
echo '<text>';
echo pq($link)->text();
echo '</text>';
echo '<url>';
echo pq($link)->attr('href');
echo '</url>';
}
echo '</notice>';
?>
I do not want to use CDATA, as the CDATA tag is visible in the generated XML :
<?php
header('Content-type: text/xml');
require('phpQuery/phpQuery.php');
$all=phpQuery::newDocumentFileHTML('BPUT.htm', $charset = 'utf-8');
$links = $all['a.myblue'];
echo '<notice>';
foreach ($links as $link) {
echo '<text>';
echo "<![CDATA[";
echo pq($link)->text();
echo "]]>";
echo '</text>';
echo '<url>';
echo pq($link)->attr('href');
echo '</url>';
}
echo '</notice>';
?>
bumping for answers.

XML PHP MYSQL data parsing

I am having a question to ask...
I am having a mysql database...
I want to make an XML file for my mysql database using PHP
I did it using newXMLELEMENT function..
Now next thing i want to do is parse my XML file data to HTML Tables & Forms..
Hoe can i add XML data to html textbox and other controls..?
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("userimages.xml");
$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
{
print $item->nodeName . " = " . $item->nodeValue . "<br>";
}
?>
this is my code..i wan tot add the fetched data to forms....but its getting print simply as i am using print function
this is my XML
i want to parse all this data in my forms ex. , etc to disabled textbox,
<ALLIMAGES>
<gal>
<imageid>6</imageid>
<imagetype>Vector</imagetype>
<image>..images/accept.png</image>
<companyname>w845</companyname>
<description>villain</description>
<status>sold</status>
<dou>2014-03-16</dou>
<like>22</like>
<price>212</price>
<color>BLUE</color>
</gal>
<gal>
<imageid>7</imageid>
<imagetype/>
<image/>
<companyname>amit</companyname>
<description>sdfj</description>
<status>Not Sold</status>
<dou>2014-01-01</dou>
<like>0</like>
<price>123</price>
<color/>
</gal>
This is just an illustration and it works as shown.. Spoonfeeding is not good, so you try to explore more with this code..
<?php
$xml=' <ALLIMAGES>
<gal>
<imageid>6</imageid>
<imagetype>Vector</imagetype>
<image>..images/accept.png</image>
<companyname>w845</companyname>
<description>villain</description>
<status>sold</status>
<dou>2014-03-16</dou>
<like>22</like>
<price>212</price>
<color>BLUE</color>
</gal>
<gal>
<imageid>7</imageid>
<imagetype/>
<image/>
<companyname>amit</companyname>
<description>sdfj</description>
<status>Not Sold</status>
<dou>2014-01-01</dou>
<like>0</like>
<price>123</price>
<color/>
</gal>
</ALLIMAGES>';
$xml = simplexml_load_string($xml);
echo "<pre>";
echo "<form method='post' action=''>";
foreach ($xml->gal as $tag)
{
echo "Image ID : <input type='text' value=$tag->imageid />";echo "<br>";
echo "Company Name: <input type='text' value=$tag->companyname />";echo "<br>";
}
echo "</form>";
OUTPUT :

PHP SimpleXML Breaking when trying to traverse nodes

I'm trying to read the xml information that tumblr provides to create a kind of news feed off the tumblr, but I'm very stuck.
<?php
$request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
$xml = simplexml_load_file($request_url);
if (!$xml)
{
exit('Failed to retrieve data.');
}
else
{
foreach ($xml->posts[0] AS $post)
{
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$small_post = substr($post,0,320);
echo .$title.;
echo '<p>'.$small_post.'</p>';
}
}
?>
Which always breaks as soon as it tries to go through the nodes. So basically "tumblr->posts;....ect" is displayed on my html page.
I've tried saving the information as a local xml file. I've tried using different ways to create the simplexml object, like loading it as a string (probably a silly idea). I double checked that my webhosting was running PHP5. So basically, I'm stuck on why this wouldn't be working.
EDIT: Ok I tried changing from where I started (back to the original way it was, starting from tumblr was just another (actually silly) way to try to fix it. It still breaks right after the first ->, so displays "posts[0] AS $post....ect" on screen.
This is the first thing I've ever done in PHP so there might be something obvious that I should have set up beforehand or something. I don't know and couldn't find anything like that though.
This should work :
<?php
$request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
$xml = simplexml_load_file($request_url);
if ( !$xml ){
exit('Failed to retrieve data.');
}else{
foreach ( $xml->posts[0] AS $post){
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$small_post = substr($post,0,320);
echo $title;
echo '<p>'.$small_post.'</p>';
echo '<hr>';
}
}
First thing in you code is that you used root element that should not be used.
<?php
$request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
$xml = simplexml_load_file($request_url);
if (!$xml)
{
exit('Failed to retrieve data.');
}
else
{
foreach ($xml->posts->post as $post)
{
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$small_post = substr($post,0,320);
echo .$title.;
echo '<p>'.$small_post.'</p>';
}
}
?>
$xml->posts returns you the posts nodes, so if you want to iterate the post nodes you should try $xml->posts->post, which gives you the ability to iterate through the post nodes inside the first posts node.
Also as Needhi pointed out you shouldn't pass through the root node (tumblr), because $xml represents itself the root node. (So I fixed my answer).

Target RSS value in PHP

I'm making RSS feed reader with PHP and i get all feed content, but i can't figure out hot to target RSS values so i can echo them, style them, etc!
this is the feed
my script so far:
foreach($feed_list as $from => $feeds) {
$xml = simplexml_load_file($feeds);
echo "<pre>";
print_r($xml);
echo "</pre>";
}
I get this out after code executes
Sorry for trouble, but i found out how
<?php
$rss = simplexml_load_file('http://www.riga.lv/rss/lv/PressRelease/');
?>
<h1><?php echo $rss->channel->title; ?></h1>
<ul>
<?php
foreach($rss->channel->item as $e) {
echo "<li><a href=\"".$e->link['href']."\">";
echo $e->title;
echo "</a></li>\n";
}
?>
Thanks to this :) - Thanks Stack Overflow :)
You need to traverse the $xml object returned and get the values for each single node... check http://www.php.net/manual/en/class.simplexmlelement.php for more info.

Categories