How to store data in xml? - php

I have one php file in which i am displaying data of xml file by this code
<?php
$xml = simplexml_load_file("note.xml") or die("Error: Cannot create object");
foreach($xml->xpath('//agent') as $item) {
$row = simplexml_load_string($item->asXML());
$v = $row->xpath('//id[. ="1"]');
if($v[0]){
print $item->id;
print $item->image;
print $item->name;
print $item->company;
print $item->street;
print $item->city;
print $item->phone;
}
else{
echo 'No records';
}
?>
Now i want to store this displayed data into other pages of my site and i am begginer of php so not expert in session.
and i want to store this detail in session for displaying this data into other pages of my site. for storing this data into session i have tried this code on same page
<?php
session_start();
$name = $_session_['$item->name'];
?>
But thats not help, So please guys can you suggest me where i am wrong.
any suggestion should be appreciable.

Start ur session in first doc.
<?php
session_start();
$xml = simplexml_load_file("note.xml") or die("Error: Cannot create object");
foreach($xml->xpath('//agent') as $item) {
$row = simplexml_load_string($item->asXML());
$v = $row->xpath('//id[. ="1"]');
if($v[0]){
print $item->id;
print $item->image;
print $item->name;
print $item->company;
print $item->street;
print $item->city;
print $item->phone;
}
else{
echo 'No records';
}
$name = $_session['name'][$item->name]; // dont quote ''
?>
http://www.w3schools.com/php/php_sessions.asp

your session code is not valid, you may try this one ;
$name = $_session['name'][$item->name];

Related

How do i change the format of an SQL output?

I am using Laravel 5.5, and when I am trying to export data from another DB, i use:
$fname = DB::connection('smallplanet')->table('pilots')->where('id', Auth::user()->id)->get(['fname']);
echo $fname;
and this is the output:
[{"fname":"Aidas1"}]
how do i change the output to just "Aidas1"?
Its JSON ,try this:
$output = json_decode($fname,true);
echo $output[0]["fname"];
//********* OR **********
foreach($output as $row)
echo $row["fname"];
Couldn't it be easier to change the source instead of the result ?
For one pilot
$pilot = DB::connection('smallplanet')->table('pilots')->find(Auth::user()->id);
if($pilot){
echo $pilot->fname;
}else{
echo "pilot not found";
}
For many pilots
$pilots = DB::connection('smallplanet')->table('pilots')->where('your_field', $value)->get();
foreach($pilots as $pilot){
echo $pilot->fname . PHP_EOL;
}

How to get json output in php from url

I see there is some more questions about this, but not useful for me.
Here i have the link and following code i am Link
<?php
$json=file_get_contents("http://2strok.com/radio.json");
$details=json_decode($json);
if($details->Response=='True')
?>
<?php echo $details->name[3];?><br>
$json=file_get_contents("http://2strok.com/radio.json");
$details=json_decode($json,true);
foreach($details as $output) {
echo $output['name'].'<br>';
}
Update - get specific name
$json=file_get_contents("http://2strok.com/radio.json");
$details=json_decode($json,true);
foreach($details as $output) {
if ($output['name']=='FM 101 / Islamabad'){
echo 'Specific name found: '.$output['name'];
}
// echo $output['name'].'<br>';
}
After json decode, it returns an array of objects, so try this:
$json=file_get_contents("http://2strok.com/radio.json");
$details=json_decode($json);
foreach ($details as $detail){
echo $detail->name;
echo "<br>";
}
?>

How to print jSON values with loop

I am using the following code to print the output of the jSON response but when I try to print
echo $obj->HotelListResponse->customerSessionId; // This is working.
echo $obj->HotelListResponse->HotelList->HotelSummary->name; // This is not working.
When the response contains only one node then its printing perfectly but when there are multiple nodes with same name then its not printing. I tried using foreach just like the below. I also tried using while loop but still I am unable to print the list of hotel names.
My jSON decoded output is like http://pastebin.com/Fr21DkEk
Total code:
$url = "https://api.eancdn.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=99&apiKey=cbrzfta369qwyrm9t5b8y8kf&locale=en_AU&currencyCode=AUD&xml=<HotelListRequest><city>Brisbane</city><stateProvinceCode>QLD</stateProvinceCode><countryCode>AU</countryCode><arrivalDate>10/16/2014</arrivalDate><departureDate>10/18/2014</departureDate><RoomGroup><Room><numberOfAdults>2</numberOfAdults></Room></RoomGroup><numberOfResults>25</numberOfResults></HotelListRequest>";
$json = file_get_contents($url);
$obj = json_decode($json);
foreach($obj as $val) {
echo $val->HotelListResponse->HotelList->HotelSummary->name;
}
Try this
foreach($obj->HotelListResponse->HotelList->HotelSummary as $val) {
echo $val->name . '<br/>';
}
HotelSummary is an array:
echo $val->HotelListResponse->HotelList->HotelSummary[0]->name;
If you want all of the hotel summaries:
foreach($obj as $val) {
foreach($val->HotelListResponse->HotelList->HotelSummary as $sum) {
echo $sum->name;
}
}
Yes you can directly access them inside the foreach. Like this:
foreach($obj->HotelListResponse->HotelList->HotelSummary as $val) {
// ^^
// since you're interested on just names, you can point it directly on that object, then each of that batch is in `$val`
echo $val->name . '<br/>';
}
// or start from the parent
foreach($obj as $values) {
$customerSessionId = $values->customerSessionId;
echo $customerSessionId . '<hr/>';
$hotelList = $values->HotelList;
foreach($hotelList->HotelSummary as $hotelsummary) {
echo $hotelsummary->name . '<br/>';
}
}

generate multiple qr code in the same webpage

I need to generate mulitple qr code in the same webpage.
Which echo in a table,one row one qr code, and it is generated by calling echo 'img src = "generateqrcode.php"'.
Also I need to do some checking before printing each row the data and qr code.
However I found that the generated qr code were overwritten by the last one. Why this happened?
generateqrcode.php
session_start();
$key = $_SESSION['key'];
$link = TARGET_LINK.$key;
QRcode::png($link,false,"L",10,0);
A.php
$row=1;
while($row <=1){
echo '<table>';
echo '<td>';
echo $row;
echo '</td>';
echo '<td>';
$sql = GETSECTIONSQL;
if(!($qids = get_records_sql($query))){
$qids = array();
}
foreach($qids as $qid){
$qrsec = $qid->section;
if($section == $qrsec){
$sql2 = GETLINKSEQUENCESQL;
if(!($viewids = get_records_sql($query))){
$viewids = array();
}
foreach($viewids as $viewid){
$vid = $viewid->sequence;
session_start();
$_SESSION['vid'] = $vid;
echo '<td>';
echo '<a class="fancybox" href="generateqrcode.php">';
echo '<img src="generateqrcode.php"/></a>';
echo '</td>';
}
}
}
echo '</table>';
$row++;
}
New
A.php
foreach($viewids as $viewid){
$vid = $viewid->sequence;
echo '<td>';
echo '<a class="fancybox" href="generateqrcode.php">';
echo '<img src="generateqrcode.php?id=$vid"/></a>';
echo '</td>';
}
generateqrcode.php
$id = $_GET['id'];
$link = TARGET_LINK.$id;
QRcode::png($link,false,"L",10,0);
Well, all your images are linking to the exact same "image" generateqrcode.php. Of course they're all going to look the same. Since your session can only store one value at a time, you cannot transport any ids unique per image in the session. You should make unique links to unique images:
<img src="generateqrcode.php?id=1234567">
Then use $_GET['id'] when generating the image instead of the session value.

creating xml with php and 'if changed' option

I am creating an address book xml feed from a MySQL database, everything is working fine, but I have a section tag which gets the first letter of the surname and pops it in that tag. I only want this to display if it has changed, but for some reason my brain isn't working this morning!
Current code:
<?php
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
echo "<data>";
do {
$char = $row_fetch["surname_add"];
$section = $char[0];
//if(changed???){
echo "<section><character>".$section."</character>";
//}
echo "<person>";
echo "<name>".$row_fetch["firstname_add"]." ".$row_fetch["surname_add"]."</name>";
echo "<title>".$row_fetch["title_add"]."</title>";
echo "</person>";
//if(){
echo "</section>";
//}
} while ($row_fetch = mysql_fetch_assoc($fetch));
echo "</data>";
?>
Any help on this welcome, don't know why I can't think of it!
And if you still want to generate XML manually, I suppose, something like this will work:
$section = "NoSectionStartedYet";
while ($row_fetch = mysql_fetch_assoc($fetch)) {
$char = $row_fetch["surname_add"];
if ($char[0] != $section)
{
if ($section != "NoSectionStartedYet")
{
echo "</section>";
}
$section = $char[0];
echo "<section><character>".$section."</character>";
}
echo "<person>";
echo "<name>".$row_fetch["firstname_add"]." ".$row_fetch["surname_add"]."</name>";
echo "<title>".$row_fetch["title_add"]."</title>";
echo "</person>";
}
echo "</section>";
To be sure that your XML is valid it is better to build a DOM tree, here is an example from the PHP manual:
<?php
$doc = new DOMDocument;
$node = $doc->createElement("para");
$newnode = $doc->appendChild($node);
echo $doc->saveXML();
?>

Categories