i want to load XML data into my php file with address data.
Each address should also have coordinates - if not they should be added. So i am doing the following:
$xmlDatei = "AddressData.xml";
$xml = simplexml_load_file($xmlDatei);
for($i=0,$size=count($xml);$i<$size;$i++){
if($xml->RECORD[$i]->ADDRESS->LAT != NULL){
//get lat and lng stuff here...
$lat = .......
$lng = .......
echo "lat: " . $lat; // Test echo WORKING
echo "lng: " . $lng;
// Now i want to add the data to the xml
$xml->RECORD[$i]->ADDRESS->addAttribute('LAT', $lat);
$xml->RECORD[$i]->ADDRESS->addAttribute('LNG', $lng);
$xml->saveXML();
}
// Test echo NOT WORKING
echo $xml->RECORD[$i]->ADDRESS->LAT;
echo $xml->RECORD[$i]->ADDRESS->LNG;
}
So it seems like the addAttribute is not working properly here.
What am I doing wrong???
Your echo is looking for a child element called LAT:
echo $xml->RECORD[$i]->ADDRESS->LAT;
But you have added an attribute, so you need to use different syntax:
echo $xml->RECORD[$i]->ADDRESS['LAT'];
You are adding attributes to the ADDRESS tag, not nodes.
Try this:
echo $xml->RECORD[$i]->ADDRESS['LAT'];
echo $xml->RECORD[$i]->ADDRESS['LNG'];
Related
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>
So i am having trouble getting an array from one PHP file to another.
In my first file (file1.php) i got following code:
print_r($order->delivery);
It will get visible when using echo of course, and it outputs the right things. It gives me an array with order information. Now i got another PHP file I need to use this information in.
What i tried so far is including file1.php to file2.php and then echo the array... But the result is empty.
require_once('path/file1.php');
echo print_r($order->delivery);
And i tried echo my array directly in file1.php adding a div like this
echo "<div id='test'>" . print_r($order->delivery, true) . "</div>";
And then getting the inner HTMl of the div with DOM
$dom = new DOMDocument();
$dom->loadHTML("mypageurl");
$xpath = new DOMXPath($dom);
$divContent = $xpath->query('//div[id="test"]');
if($divContent->length > 0) {
$node = $divContent->item(0);
echo "{$node->nodeName} - {$node->nodeValue}";
}
else {
// empty result set
}
Well... none of it works. Any suggestions?
You have to set a variable or something, not echoing it.
file1.php:
$delivery = $order->delivery;
file2.php
include('path/file1.php');
echo "<div id='test'>" . (isset($delivery['firstname']) ? $delivery['firstname'] : '') . "</div>";
Or you use the $object directly if it is set in file1.php
file2.php
include('path/file1.php');
echo "<div id='test'>" . (isset($order->delivery['firstname']) ? $order->delivery['firstname'] : '') . "</div>";
You can do this by using $_SESSION or $_COOKIE, See here for more detail; PHP Pass variable to next page
Be careful at the variable scope. See this link: http://php.net/manual/en/language.variables.scope.php
And try this code please:
require_once('path/file1.php');
global $order;
print_r($order->delivery);
Defining $order as global should fix your issue.
You could return an array in a file and use it in another like this:
<?php
/* File1.php */
return array(
0,1,2,3
);
-
<?php
/* File2.php */
var_dump(require_once('File1.php'));
I checked php.net and read a few examples of how urlencode( ) works but somehow I just can't get it right. Can someone give me a hand?
it'll be a lot to example so hopefully my brief example would make sense.
I have a page called 2.php and it was called to show some contents of a .txt file choosen in 1.php.
I am told to make a link for 3.php and the link should look something like /3?filename=a.txt
with filename as GET parameter name and Ensure GET parameter value is urlencoded using the urlencode( ) function.
but I'm confused how and where I should put urlencode() to make it work.
I'll paste my 2.php code here...I simplified the codes a bit...
<?php
$fileContents = file("./aaa/" . $_GET["course"] . ".txt");
echo "<table border=\"1\">";
foreach($fileContents as $row)
{
echo "<tr>";
$contents = preg_split("/,/", $row);
foreach($contents as $eachline)
{
echo "<td>";
if(!(preg_match("/#/", $eachline)))
{
echo trim(ucfirst($eachline));
}
else
{
echo trim(strtolower($eachline));
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
echo "<a href='./1.php'>Choose another txt file</a><br/>";
echo "or<br/>";
echo "<a href='.3.php?'>Work with this txt file</a>";
?>
BUT…the 3.php option must have a query string appended to it: the name of the text file that was selected in 1, so instead of ./3.php, the url should be something such as ./3?filename=asdf.txt
Use “filename” as the GET parameter name. Ensure the GET parameter value is urlencoded using the urlencode( ) function.
but I'm just not sure how to get it to work....
You can wrap the part that should be url encoded in the function within the string:
$url = 'http://www.google.com?q=' . urlencode($search);
OR in html
http://www.google.com?q=<?php echo urlencode($search); ?>
Where . is the concatenation of 2 outputs.
Hy!
I am very new in Web-Dev. I have a mysql db and the controller is made with PHP.
I give the data formated in php back to the html.
My Quest:
How to parse the xml in js right for the HTML based view?
If there are better solutions for the data transfair between PHP and HMTL, please let me know.
I don't wont the have HTML code in the php
Example PHP:
<?php
$id = $_GET['id'];
if (is_numeric($id))
{
include 'db_connect.php';
$sql = "Select * from RECIPES WHERE recipes_id=".$id;
$result = mysql_query($sql,$db) or exit("QUERY FAILED!");
while($row = mysql_fetch_array($result))
{
echo "<RECIPE>";
echo "<ID>" . $row['recipes_id'] . "</ID><br />";
echo "<TITLE>" . $row['text'] . "</TITLE><br />";
echo "<TEXT>" . $row['text'] . "</TEXT><br />";
echo "<COUNT_PERSONS>" . $row['count_persons'] . "</COUNT_PERSONS><br />";
echo "<DURATION>" . $row['duration'] . "</DURATION><br />";
echo "<USER_ID>" . $row['user_id'] . "</USER_ID><br />";
echo "<DATE>" . $row['date'] . "</DATE><br />";
echo "</RECIPE>";
}
}
else
{
echo "<ERROR>ID ERROR</ERRORR>";
}
mysql_close($db);
?>
thx
If you just want to transfer data between the browser and the server then it's simpler to use the JSON format. For PHP there are two built in function to decode and encode JSON: json_decode and json_encode.
Modern browser support JSON by default: JSON.parse and JSON.stringify.
I agree with styrr, but if you must use XML then you can use jQuery.parseXML() or simply use jQuery.ajax() with correct type eg.:
$.ajax({
url: "script.php?parameters=abc",
dataType: "xml",
success: function(responseDocument){
var all_ids = responseDocument.getElementsByTagName('ID');
var all_ids_jquery_way = $('ID', responseDocument);
}
});
You can use any othe AJAX framework or even write your own function. Browsers can parse XML and create a document which you can then change as the main document (with DOM functions).
Again with most data JSON is a bit simpler to use and is lighter.
Input is a URL like this http://ws.geonames.org/children?geonameId=6255148 and I would like to receive the XML file in a SimpleXMLElement for example?
Try this as your base point:
<?php
$xml = simplexml_load_file("http://ws.geonames.org/children?geonameId=6255148");
//print_r($xml);
foreach($xml->geoname as $geo)
{
echo $geo->toponymName . "<br />";
}
?>