Is there PHP built-in foreach limit? (with DOM) - php

I working on a XML parse script. The XML has more than 12.000 pieces items ("term" the element name) but the script gives only 4489 pieces element. Here my code:
<?php
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->load('theXML.xml');
$i=0;
while(is_object($ajanlatok = $doc->getElementsByTagName("term")->item($i)))
{
foreach($ajanlatok->childNodes as $nodename)
{ echo $i;
if($nodename->nodeName=='dep')
{
echo "<u>Indulás</u><br> Innen: ".$nodename->getAttribute('from')." - Ide: ".$nodename->getAttribute('to')." - Dátum: ".$nodename->getAttribute('date')."<br>";
}
elseif($nodename->nodeName=='prices')
{
foreach($nodename->childNodes as $subNodes)
{
echo $subNodes->nodeName."(ár) - ".$subNodes->nodeValue."<br>";
}
}
elseif($nodename->nodeName=='surcharges')
{
foreach($nodename->childNodes as $subNodes)
{
echo $subNodes->nodeName."(felár) - ".$subNodes->nodeValue."<br>";
}
}
elseif($nodename->nodeName=='rooms')
{
foreach($nodename->childNodes as $subNodes)
{
echo $subNodes->nodeName."(szoba) - ".$subNodes->nodeValue."<br>";
}
}
else
{
echo $nodename->nodeName." - ".$nodename->nodeValue."<br>";
}
}
echo "<hr>";
$i++;
}
?>
Is there any limitation or why I don't get all the content. The XML file large, 70mb.

Related

Add some data from php function into mpdf

I have php function, whose create some blocks with data from loop.
finction.php:
function drawcomplect() {
for ($i=0;$i<count($new_arr);$i++) {
if ($i%2==0) {
echo "<div class='numeric'>".$j++."</div>";
echo "<div class='item'>".$new_arr[$i]."</div>";
} else {
echo "<div class='count'>".$new_arr[$i]."</div>";
}
}
}
And mPdf file for output:
$html ='<html>'.drawcomplect().'</html>'
If run this function in php file, it's work. But mPdf not work, print "Page error". How I can add data to pdf?
function drawcomplect() {
$str = '';
for ($i=0;$i<count($new_arr);$i++) {
if ($i%2==0) {
$str .="<div class='numeric'>".$j++."</div><div class='item'>".$new_arr[$i]."</div>";
} else {
$str .="<div class='count'>".$new_arr[$i]."</div>";
}
}
return $str;
}
ANd Mpdf:
$html ='<html><body>'.drawcomplect().'</body></html>'

If-else condition gives me errors in PHP

I have two websites, each with it's own domain name. On load I have execute a php file that is used on both sites and has the same functionality.
Here is the code of that file:
<?php
echo '1';
if ($_SESSION["template"]=="template1";)
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template1/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template1/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template1/c.php');
}
else if ($_SESSION["template"]=="template2";)
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template2/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template2/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template2/c.php');
}
else {
echo 'NO DATA';
}
echo '4';
?>
On each of the two sites I set a session variable but in the above code it doesn't seem to work as I expect it to.
Am i missing something?
remove semicolon from if() and else if() statement, also add brackets when you using nested if else because it makes someone to understand easier and looks better
<?php
echo '1';
if ($_SESSION["template"]=="template1")
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template1/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template1/b.php');
}
else if { ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template1/c.php');
}
}
else if ($_SESSION["template"]=="template2")
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template2/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template2/b.php');
}
else if ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template2/c.php');
}
}
else {
echo 'NO DATA';
}
echo '4';
?>
After reviewing your code I edited my answer. The below code will do exactly the same as your code but requires alot less code.
<?php
if (isset($_SESSION['template']) && isset($_REQUEST['cmdAction'])) {
echo file_get_contents('http://localhost/images/'.$_SESSION['template'].'/'.strtolower($_REQUEST['cmdAction']).'.php');
} else {
echo 'NO DATA';
}
?>

How to call a Php Function from html

I am using a PHP scraper by using simple PHP DOM library. I wrote a function which scrapes data that I need. How can I call that function from html page? I need to show that data in my html page. I am using AngularJS framework and I am working with views.
Here is my code.
function Islamabad_data(){
// Array Declaration
var isb_hi_temp = new Array();
$fhtml1 = file_get_contents('http://www.accuweather.com/en/pk/islamabad/258278/daily-weather-forecast/258278');
$fhtml1 = str_get_html($fhtml1);
$day6 = file_get_contents('http://www.accuweather.com/en/pk/islamabad/258278/daily-weather-forecast/258278?day=6');
$day6 = str_get_html($day6);
echo "Islamabad Data<br>";
echo" <br>High temperature <br>";
foreach($fhtml1->find('#feed-tabs .temp') as $element){
echo $element->plaintext;
?>
isb_hi_temp.push('<?php echo $element; ?>');
<?php
}
$i=0;
foreach($day6->find('#feed-tabs .temp')as $element)
{
if($i<2)
{
echo $element->plaintext;
?>
isb_hi_temp.push('<?php echo $element; ?>');
<?php
$i++;
}
}
echo isb_data;
}
I know this is not an answer but I've simplified your code. It looks like you want to generate a JavaScript array that can be fetched by Angular. Step 1 one is to properly generate the JSON you need. There were a lot of mistakes in your code -- you cannot intermix JavaScript and PHP in the way that you were doing. Usually in PHP we'll generate an array object using PHP then call json_encode to create the JSON object that will be accessed by the client (in this case, you are using Angular as your client).
function Islamabad_data(){
$fhtml1 = file_get_contents('http://www.accuweather.com/en/pk/islamabad/258278/daily-weather-forecast/258278');
$fhtml1 = str_get_html($fhtml1);
$day6 = file_get_contents('http://www.accuweather.com/en/pk/islamabad/258278/daily-weather-forecast/258278?day=6');
$day6 = str_get_html($day6);
//echo "Islamabad Data<br>";
//echo" <br>High temperature <br>";
$hiTemp = array();
foreach($fhtml1->find('#feed-tabs .temp') as $element){
$hiTemp[] = $element;
}
$i=0;
foreach($day6->find('#feed-tabs .temp')as $element) {
if ($i<2) {
$hiTemp[] = $element;
}
$i++;
}
echo json_encode( $hiTemp );
}
Step 2. Use Angular to call your PHP service. Here is an oversimplified example to help you:
function Hello($scope, $http) {
$http.get('/some/where/somefile.php').
success(function(data) {
$scope.islamabadData = data;
});
}
Also you should really be using Accuweather's API instead of doing data scraping.
http://apidev.accuweather.com/developers/
It might help you:
function loadCitiesData($scope, $http) {
$http.get('/some/where/somefile.php?Islamabad=1').
success(function(data) {
$scope.islamabadData = data;
});
$http.get('/some/where/somefile.php?Lahore=1').
success(function(data) {
$scope.LahoreData = data;
});
}
And your somefile.php file:
<?php
if( !empty($_GET['Islamabad']) ){
$fhtml1 = file_get_contents('http://www.accuweather.com/en/pk/islamabad/258278/daily-weather-forecast/258278');
$fhtml1 = str_get_html($fhtml1);
$day6 = file_get_contents('http://www.accuweather.com/en/pk/islamabad/258278/daily-weather-forecast/258278?day=6');
$day6 = str_get_html($day6);
//echo "Islamabad Data<br>";
//echo" <br>High temperature <br>";
$hiTemp = array();
foreach($fhtml1->find('#feed-tabs .temp') as $element){
$hiTemp[] = $element;
}
$i=0;
foreach($day6->find('#feed-tabs .temp')as $element) {
if ($i<2) {
$hiTemp[] = $element;
}
$i++;
}
echo json_encode( $hiTemp );
}elseif( !empty($_GET['Lahore']) ){
// your code, $yourArray = array('something');
// echo json_encode( $yourArray );
}elseif( !empty($_GET['Multan']) ){
// your code, $yourArray = array('something');
// echo json_encode( $yourArray );
}
?>

Get album art from MP3 PHP

I am using this code to get ID3 information of an MP3 File.
<?php
class CMP3File {
var $title;var $artist;var $album;var $year;var $comment;var $genre;
function getid3 ($file) {
if (file_exists($file)) {
$id_start=filesize($file)-128;
$fp=fopen($file,"r");
fseek($fp,$id_start);
$tag=fread($fp,3);
if ($tag == "TAG") {
$this->title=fread($fp,30);
$this->artist=fread($fp,30);
$this->album=fread($fp,30);
$this->year=fread($fp,4);
$this->comment=fread($fp,30);
$this->genre=fread($fp,1);
fclose($fp);
return true;
} else {
fclose($fp);
return false;
}
} else { return false; }
}
}
?>
I am then using this code to call it.
include ("CMP3File.php");
$filename="music_file.mp3";
$mp3file=new CMP3File;
$mp3file->getid3($filename);
echo "Title: $mp3file->title<br>\n";
echo "Artist: $mp3file->artist<br>\n";
echo "Album: $mp3file->album<br>\n";
echo "Year: $mp3file->year<br>\n";
echo "Comment: $mp3file->comment<br>\n";
echo "Genre: " . Ord($mp3file->genre) . "<br>\n";
Is there a way to also get and echo out the album art as an image?

Having ULs for foreach loops

Couple of issues here I'm not sure on what the issue is but with how my coding is set up it looks odd in the firebug with how the uls, lis, and h2s are rendered. Any ideas why and also I need some suggestions on how to fix the errors I am getting which is caused by some categories not having child pages. In which case if they don't have child pages I don't want my code to do anything just pass over it.
kansasoutlawwrestling.com/site-map
<?php
echo "<pre>";
print_r($categoriesArray);
echo "</pre>";
if((isset($categoriesArray)) AND ((!empty($categoriesArray))||($categoriesArray !== NULL)))
{
if(count($categoriesArray) <= 0)
{
echo "There are no content pages on this site!";
}
else
{
foreach ($categoriesArray as $row)
{
echo "<h2>".stripslashes($row['name'])."</h2>";
echo "<ul>";
foreach ($row['children'] as $row2)
{
echo "<li>".stripslashes($row2['link_name'])."</li>";
if (count($row2) > 0)
{
echo "<ul>";
foreach ($row2['child_pages'] as $row3)
{
echo "<li>".stripslashes($row3['link_name'])."</li>";
}
echo "<ul>";
}
}
echo "</ul>";
}
}
}
else
{
echo "There are no content pages on this site!";
}
?>
EDIT: Any other ideas for me to try?
You could do;
if(count($row2['child_pages']) < 1) {
continue;
}
Hope it helps

Categories