Simple_Html_Dom parser not working inside function - php

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);

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);
}

SimpleXML Export "SimpleXMLElement::__set_state(array(" in Variable

I'm trying to take a variable from an xml file and save it as a php file
$xmlresponse = simplexml_load_string($dom->saveXML());
$shipmentid = $xmlresponse->ListInboundShipmentsResult->ShipmentData->member->ShipmentId;
echo '<br />' . $shipmentid . '<br />';
$todaydt = new DateTime('today');
$today3 = $todaydt->format('m-d-Y') . PHP_EOL;
$today = rtrim($today3);
$var_str = var_export($shipmentid, true);
$var = "<?php\n\n\$shipmentid = $var_str;\n\n?>";
file_put_contents('shipmentid_' . $today . '.php', $var);
}
But in my shipmentid_11-11-2016.php file it is giving me
$shipmentid = SimpleXMLElement::__set_state(array(
0 => 'shipmentid',
));
How can I save the variable so it is just simply
$shipmentid = 'shipmentid';
?
When you do
echo '<br />' . $shipmentid . '<br />';
php implicitly converts $shipmentid value to a string. var_export does not do it, so you need to convert it by yourself:
$var_str = strval($shipmentid);
Next thing is that $var_str is a string and does not contains any quotes around. So, again you need to write quotes by yourself:
// with double quotes
$var = "<?php\n\n\$shipmentid = \"$var_str\";\n\n?>";
// with single quotes
$var = "<?php\n\n\$shipmentid = '$var_str';\n\n?>";

php variable variables to access property of an object

I want to store an property->object name in a table and access in code.
What is the proper way to accomplish the following.
$patient = new stdClass();
$patient->patient_number = '12345';
$cls = 'patient';
$prp = 'patient_number';
echo 'test1 = ' . $patient->patient_number . '<br>';
//--- this prints 12345 as expected;
echo 'test2 = ' . $$cls->patient_number . '<br>';
//--- this print 12345 as expected;
echo 'test3 = ' . $$cls->${$prp} . '<br>';
//--- this generates undefined variable patient_number;
Use this:
echo 'test3 = ' . ${$cls}->{$prp} . '<br>';

PDO: UPDATE not working

I wrote some code to update a mySQL table via php/PDO.
But it is not working and I just can't figure out where my mistake is.
The execute() returns true, but the changes never actually show up in the table.
My code looks pretty much like this:
$columnObject = array(
"emailAddress"=>"aaa#aaa.com",
"passwordHash"=>"56bj5g63j4g57g567g5k75jh7gk4g74j5hg67",
"name"=>"qweqweqwe",
"lastActivity"=>4128649814
);
$knownColumnName = "emailAddress";
$knownColumnData = "aaa#aaa.com";
foreach ($columnObject as $columnName => $columnData) {
$pdoUpdateString .= $columnName . "=:" . $columnName . ",";
$pdoExecuteObject[$columnName] = $columnData;
}
$pdoUpdateString = rtrim($pdoUpdateString, ",");
$pdoExecuteObject['knownColumn'] = $knownColumnData;
$q = $this->hCon->prepare('UPDATE ' . $this->name . ' SET ' . $pdoUpdateString . ' WHERE ' . $knownColumnName . '=:knownColumn');
$q->execute($pdoExecuteObject);

php echo statement inside $output

I have the following php code:
$skizzar_masonry_item_width = $masonry_item_width;
$skizzar_masonry_item_padding = $masonry_item_padding;
$skizzar_double_width_size = $masonry_item_width*2 +$masonry_item_padding;
$output .= '<style>.skizzar_masonry_entry.skizzar_ma_double, .skizzar_masonry_entry.skizzar_ma_double img {width:'.$skizzar_double_width_size.'}</style>';
return $output;
For some reason though, the value of $skizzar_double_width_size is not being added into the $output - is there a way to echo a value in an output variable?
As #Rizier123 mentioned, ensure you initialise any string variables before trying to append to them.
$var = '';
$var .= 'I appended';
$var .= ' a string!';
I would also like to strongly discourage you from using inline styles as well as generating them with inline PHP. Things get very messy very quickly.
In a situation like this you need to check that all the variables you are using in the calculation are valid before you panic.
So try
echo 'before I use these values they contain<br>';
echo '$masonry_item_width = ' . $masonry_item_width . '<br>';
echo '$masonry_item_padding = ' . $masonry_item_padding . '<br>';
$skizzar_masonry_item_width = $masonry_item_width;
$skizzar_masonry_item_padding = $masonry_item_padding;
$skizzar_double_width_size = $masonry_item_width*2 +$masonry_item_padding;
echo 'after moving the fields to an unnecessary intemediary field<br>';
echo '$skizzar_masonry_item_width = ' . $skizzar_masonry_item_width . '<br>';
echo '$skizzar_masonry_item_padding = ' . $skizzar_masonry_item_padding . '<br>';
echo '$skizzar_double_width_size = ' . $skizzar_double_width_size . '<br>';
$output .= '<style>.skizzar_masonry_entry.skizzar_ma_double, .skizzar_masonry_entry.skizzar_ma_double img {width:'.$skizzar_double_width_size.'}</style>';
echo $output;
This should identify which fields are causing you problems.
Also while testing always run with display_errors = On It saves so much time in the long run.

Categories