Php Xpath query returning "Array" - php

ive got my xml file and also heres my php script
$db = simplexml_load_file("BIN/videos.xml");
$id = $_GET['id'];
$tq = "//video['#id=" . $id . "']/title[0]";
$dq = "//video['#id=" . $id . "']/description[1]";
$eq = "//video['#id=" . $id . "']/embed[2]";
$title = $db->xpath($tq);
$description = $db->xpath($dq);
$embed = $db->xpath($eq);
include("design/lyt.php");
echo $embed . '<br>
<h1>' . $title . '</h1>
<p>' . $description . '</p>';
?>
Its supposed to display "Test" for them all! but it says "Array"

Access the elements of the array returned from xpath...
PHP >= 5.4:
$title = $db->xpath($tq)[0];
PHP < 5.4:
Update PHP :-)
or
list($title,) = $db->xpath($tq);
or
$title = $db->xpath($tq);
$title = $title[0];

Related

Parsing json with a nested element

does not work with a nested element
the elements of the first level are output, and the nested array with data is not read, as it is possible to get values - id, title and location?
<?php
function removeBomUtf8($s){
if(substr($s,0,3)==chr(hexdec('EF')).chr(hexdec('BB')).chr(hexdec('BF'))){
return substr($s,3);
}else{
return $s;
}
}
$url = "https://denden000qwerty.000webhostapp.com/opportunities.json";
$content = file_get_contents($url);
$clean_content = removeBomUtf8($content);
$decoded = json_decode($clean_content);
while ($el_name = current($decoded)) {
// echo 'total = ' . $el_name->total_items . 'current = ' . $el_name->current_page . 'total = ' . $el_name->total_pages . '<br>' ;
echo ' id = ' . $el_name->data[0]->id . ' title = ' . $el_name->data.title . ' location = ' . $el_name->data.location . '<br>' ;
next($decoded);
}
?>
$el_name->data[0]->id is correct
$el_name->data.title is not
you see the difference?
and $decoded is the root (no need to iterate over it) - you want to iterate over the data children
<?php
foreach($decoded->data as $data)
{
$id = (string)$data->id;
$title = (string)$data->title;
$location = (string)$data->location;
echo sprintf('id = %s, title = %s, location = %s<br />', $id, $title, $location);
}

Simple_Html_Dom parser not working inside function

I'm trying to use Simple_Html_Dom parser to parse a web page for NBA statistics. I'm going to have a lot of different urls, but parsing the same data, so I figured I would create a function. Outside of the function, this parser works great, however as soon as I place the parsing inside the function, I receive a connection error. Just wondering, if anyone knows why I can't run the file_get_html inside the function. Here is the code below. Please help!
include('simple_html_dom.php');
$nbaPlayers = 'playerstats/15/1/eff/1-2';
function nbaStats($url){
$html = 'http://www.hoopsstats.com/basketball/fantasy/nba/';
$getHtml = $html . $url;
$a = file_get_html("$getHtml");
foreach($a->find('.statscontent tbody tr') as $tr){
$nbaStatLine = $tr->find('td');
$nbaName = $nbaStatLine[1]->plaintext;
$nbaGamesPlayed = $nbaStatLine[2]->plaintext;
$nbaMinuesPlayed = $nbaStatLine[3]->plaintext;
$nbaTotalPoints = $nbaStatLine[4]->plaintext;
$nbaRebounds = $nbaStatLine[5]->plaintext;
$nbaAssists = $nbaStatLine[6]->plaintext;
$nbaSteals = $nbaStatLine[7]->plaintext;
$nbaBlocks = $nbaStatLine[8]->plaintext;
$nbaTurnovers = $nbaStatLine[9]->plaintext;
$nbaoRebounds = $nbaStatLine[11];
$nbadRebounds = $nbaStatLine[12];
$nbaFieldGoals = $nbaStatLine[13];
$nbaFieldGoals = explode("-", $nbaFieldGoals);
$nbaFieldGoalsMade = $nbaFieldGoals[0];
$nbaFieldGoalsAttempted = $nbaFieldGoals[1];
// Player Stat Line
$playerStats = $nbaName . ': gp - ' . $nbaGamesPlayed . ' mp - ' . $nbaMinutesPlayed . ' pts - ' . $nbaTotalPoints . ' rb - ' . $nbaRebounds . ' as - ' . $nbaAssists . ' s - ' . $nbaSteals . ' bl - ' . $nbaBlocks . ' to - ' . $nbaTurnovers;
echo $playerStats . '<br /><br />';
}
}
nbaStats($nbaPlayers);
When passing a variable to a function you don't need to wrap the variable in quotations. Quotations are used when passing text directly to a function or assigning text to a variable.
Change this:
$a = file_get_html("$getHtml");
To this:
$a = file_get_html($getHtml);

Wrong Foreach cycle

i am doing something wrong with my foreach cycle. But looks my knowledge is not enugh to figure out what's wrong. My code is pretty simple:
$xnl_file = "xml.xml";
$xml = simplexml_load_file($xnl_file);
$my_file = 0;
foreach ($xml as $value){
var_dump($value);
$CountryOrganizationId = "<CountryOrganizationId>".$xml->Partnership->CountryOrganizationId."</CountryOrganizationId>";
$PartnershipId = "<PartnershipId>".$xml->Partnership->PartnershipId."</PartnershipId>";
$OwnerId = "<OwnerId>".$xml->Partnership->OwnerId."<OwnerId>";
$PartnerIdList = "<PartnerIdList><String>".$xml->Partnership->PartnerIdList->String."</String></PartnerIdList>";
$CountryOrganizationId_contact = "<Contract><CountryOrganizationId>".$xml->Partnership->Contract->CountryOrganizationId."</CountryOrganizationId>";
$ContractId = "<ContractId>".$xml->Partnership->Contract->ContractId."</ContractId>";
$data = "<Partnership>".$CountryOrganizationId.$PartnershipId.$OwnerId.$PartnerIdList.$CountryOrganizationId_contact.$ContractId.$Role1.$Category1.$Rate1.
$Role2.$Category2.$Rate2.$Role3.$Category3.$Rate3."</Partnership>";
echo $data;
}
I am getting data from XML and try to parse it on multiple one, but this just copy same data again and again. I am not sure what i am doing wrong. In my opinion data should rewrite each other every time cycle is doing same but they are not changing. At echo $data i get as many results as i should, problem is just they are same.
If I var_dump $value at start i get nice result that data are coming to cycle but why the output is the same all the time?
Please can somebody give me advise?
Thanks
The $value variable is never used, you're always using the $xml. Try it like:
$xnl_file = "xml.xml";
$xml = simplexml_load_file($xnl_file);
$my_file = 0;
foreach ($xml as $value){
var_dump($value);
$CountryOrganizationId = "<CountryOrganizationId>" . $value->CountryOrganizationId . "</CountryOrganizationId>";
$PartnershipId = "<PartnershipId>" . $value->PartnershipId . "</PartnershipId>";
$OwnerId = "<OwnerId>" . $value->OwnerId . "<OwnerId>";
$PartnerIdList = "<PartnerIdList><String>" . $value->PartnerIdList->String . "</String></PartnerIdList>";
$CountryOrganizationId_contact = "<Contract><CountryOrganizationId>" . $value->Contract->CountryOrganizationId . "</CountryOrganizationId>";
$ContractId = "<ContractId>" . $value->Contract->ContractId . "</ContractId>";
$data = "<Partnership>" . $CountryOrganizationId . $PartnershipId . $OwnerId . $PartnerIdList . $CountryOrganizationId_contact .
$ContractId . $Role1 . $Category1 . $Rate1 . $Role2 . $Category2 . $Rate2 . $Role3 . $Category3 . $Rate3 .
"</Partnership>"afdsf
echo $data;
}
Concat $data to its previous value { $data .= "......"}
foreach ($xml as $value)
{
var_dump($value);
$CountryOrganizationId = "<CountryOrganizationId>".$xml->Partnership->CountryOrganizationId."</CountryOrganizationId>";
$PartnershipId = "<PartnershipId>".$xml->Partnership->PartnershipId."</PartnershipId>";
$OwnerId = "<OwnerId>".$xml->Partnership->OwnerId."<OwnerId>";
$PartnerIdList = "<PartnerIdList><String>".$xml->Partnership->PartnerIdList->String."</String></PartnerIdList>";
$CountryOrganizationId_contact = "<Contract><CountryOrganizationId>".$xml->Partnership->Contract->CountryOrganizationId."</CountryOrganizationId>";
$ContractId = "<ContractId>".$xml->Partnership->Contract->ContractId."</ContractId>";
$data .= "<Partnership>".$CountryOrganizationId.$PartnershipId.$OwnerId.$PartnerIdList.$CountryOrganizationId_contact.$ContractId.$Role1.$Category1.$Rate1.
$Role2.$Category2.$Rate2.$Role3.$Category3.$Rate3."</Partnership>";
}
echo $data;

Pulling data from Google Cal xml

I'm running into an error when trying to pull some data from Google Cal using PHP. Here is the code:
$url = $_POST['url'];
$xml = file_get_contents($url);
$feed = simplexml_load_string($xml);
$ns= $feed->getNameSpaces(true);
foreach ($feed->entry as $entry) {
$eventdetail = $entry->children($ns["gd"]);
$when_atr = $eventdetail->when[0]->attributes();
$title = $entry->title;
echo "<div class='eventTitle'>".$title . "</div>";
$where = $eventdetail->where[0]->attributes();
echo "<div class='eventLocation'>" . $where . "</div>" . '<br />';
$start = new DateTime($when_atr['startTime']);
echo "<div class='eventTime'>".$start->format('D F jS, g:ia') . " to ";
$end = new DateTime($when_atr['endTime']);
echo $end->format('g:ia')."</div>" . '<br />' ;
}
The titles work just fine, but for both the "when" and "where" I'm running into problems with the code when[0]->attributes(); and where[0]->attributes();.
The error I get is
Fatal error: Call to a member function attributes() on a non-object
What can I do to prevent this from happening?

Syntax problem with html in PHP

im getting a syntax error on this line of code and don't know the correct formatting for it.
This is the part i'm having problems with -
echo "
<ul>";
foreach($photos as $photo) {
$farm = $photo['farm'];
$server = $photo['server'];
$photo_id = $photo['id'];
$secret = $photo['secret'];
$photo_title = $photo['title'];
<li><img src="http://farm'.$photo['farm'].'.static.flickr.com/'.$photo['server'].'/'.$photo['id'].'_'.$photo['secret'].'_t.jpg" alt="'.$photo['title'].'" ></li>
The problem is with that li tag. How can i format it properly?
Per the comments, you probably mean to have the following for your last line:
echo '<li><img src="http://farm'.$photo['farm'].'.static.flickr.com/'.$photo['server'].'/'.$photo['id'].'_'.$photo['secret'].'_t.jpg" alt="'.$photo['title'].'" ></li>';
The <li> part should be quoted. Try:
echo "
<ul>";
foreach($photos as $photo) {
$farm = $photo['farm'];
$server = $photo['server'];
$photo_id = $photo['id'];
$secret = $photo['secret'];
$photo_title = $photo['title'];
echo '<li><img src="http://farm' . $photo['farm'] . 'static.flickr.com/' . $photo['server'] . '/' . $photo['id'] . '_' . $photo['secret'] . '_t.jpg" alt="' . $photo['title'] . '" ></li>';
}
echo '</ul>';

Categories