Parsing page source using PHP - php

I'm having a lot of trouble parsing the page source of a results page. The results page returns data about businesses in a city. This data includes name, address, phone number, owner name and URL. Any help would be much appreciated.
This is an example of one of the results (There are hundreds in the original file):
<div class="ListingResults_All_CONTAINER ListingResults_Level3_CONTAINER">
<div class="ListingResults_Level3_HEADER">
<div class="ListingResults_All_ENTRYTITLERIGHT">
<div><img src="/external/wcpages/images/L3more.gif" alt="317 at Montgomery"></div>
</div>
<div class="ListingResults_All_ENTRYTITLELEFT">
<div class="ListingResults_All_ENTRYTITLELEFTBOX"><strong><span itemprop="name">317 at Montgomery</span></strong></div>
</div>
</div>
<div class="ListingResults_Level3_MAIN">
<div class="ListingResults_Level3_MAINRIGHT">
<div class="ListingResults_Level3_MAINRIGHTBOX">
<div class="ListingResults_Level3_LOGO"><img src="http://www.centerstateceo.com/external/wcpages/wcwebcontent/webcontentpage.aspx?contentid=2071" class="ListingResults_Level3_LOGOIMG"><div style="width:100%;height:1px;overflow:hidden;"></div>
</div>
<div class="ListingResults_MAINRIGHTBOXDIVIDER" style="width:100%;overflow:hidden;height:1px;">_</div>
<div class="ListingResults_Level3_AFFILIATIONS"></div>
</div>
</div>
<div class="ListingResults_Level3_MAINLEFT">
<div class="ListingResults_Level3_MAINLEFTBOX" itemtype="http://data-vocabulary.org/Address" itemscope="" itemprop="address"><span itemprop="street-address">317 Montgomery St.</span><br><span itemprop="locality">Syracuse</span>, <span itemprop="region">NY</span> <span itemprop="postal-code">13202 </span><div class="ListingResults_Level3_MAINCONTACT"><img src="/external/wcpages/images/maincontact.gif" alt="Mr. Dean Whittles">Mr. Dean Whittles</div>
<div class="ListingResults_Level3_PHONE1"><img src="/external/wcpages/images/phone.gif" alt="Work Phone: (315) 214-4267">(315) 214-4267</div>
</div>
</div>
</div>
<div class="ListingResults_Level3_FOOTER">
<div class="ListingResults_Level3_DESCRIPTION">
<div class="ListingResults_Level3_DESCRIPTIONBOX"></div>
</div>
<div class="ListingResults_Level3_FOOTERRIGHT">
<div class="ListingResults_Level3_FOOTERRIGHTBOX">
<div class="ListingResults_Level3_SOCIALMEDIA"></div>
</div>
</div>
<div class="ListingResults_Level3_FOOTERRIGHT">
<div class="ListingResults_Level3_FOOTERRIGHTBOX">
<div class="ListingResults_Level3_COUPONS"></div>
</div>
</div>
<div class="ListingResults_Level3_FOOTERLEFT">
<div class="ListingResults_Level3_FOOTERLEFTBOX"><span class="ListingResults_Level3_LEARNMORE"><a href="/Restaurants/317-at-Montgomery-7897" class="level3_footer_left_box_a friendly">
Learn More
</a></span><span class="ListingResults_Level3_VISITSITE"> | <a href="http://www.317syr.com" onclick="recordReferralOnClick('20947', '7897', 'W');" target="_blank">
Visit Site
</a></span><span class="ListingResults_Level3_MAP"> | Show on Map</span></div>
</div>
</div>
</div>
PHP Code from Comment:
<?php
$dom = new DOMDocument();
$dom->loadHtml($data);
$spans = $dom->getElementsByTagName('span');
foreach ($spans as $el) {
$children = $el->childNodes->item(1);
if (is_object($children) AND $children->tagName == 'a') {
$url = $children->getAttribute('href');
echo $url;
continue;
}
$user_param = $el->getAttribute('itemprop');
$value = $el->nodeValue;
if ($user_param != "") {
echo $user_param . " " . $value . "\n";
}
}
?>

Related

After 500 results displayed from DB html/css loses formatting

I have a page that runs off a local webserver that is uses SQLite as its database. As its used local I am not worried about listing all results on one page as they load super fast. I am having an issue with it though as after 500 results are displayed from SQLite3 the formatting goes all wonky and starts stacking them on top of each other. Everything before that is fine. Its written in php. Info was entered into the database using htmlspecialchars so I dont believe that is the issue. The code that builds each record in the loop is
$list = '';
while($row = $results->fetchArray()) {
$id = $row["id"];
$MovieTitle = $row["MovieTitle"];
$MovieYear = $row["MovieDate"];
$MovieRes = $row["MovieRes"];
$FileName = $row["FileName"];
$Summary = $row["Summary"];
$Genres = $row["Genres"];
$PictureLocation = $row["PictureLocation"];
$Rating = $row["Rating"];
$ReleaseDate = $row["ReleaseDate"];
$list .= '<div class="box">
<div class="movie">
<div class="movie-image"><span class="play"><span class="name">'.$MovieTitle.'</span></span><img src="'.$ThumbnailPic.'" alt=""></div>
<div class="rating">
<p>RATING: '.$Rating.'</p>
<div class="stars">
<div class="'.$StarGraphic.'"></div>
</div>
<span class="comments"></span></div>
</div>';
}
and i just echo them them in the html as such
<html>
<body>
<div id="main">
<br>
<?php echo $list; ?>
</div>
</body>
</html>
Your HTML is wrong, you did not close <div class="box"> and <span class="play"> tags properly.
Correct HTML is:
<div class="box">
<div class="movie">
<div class="movie-image">
<span class="play">
<a href="movielist.php?movie='.$FileName.'">
<span class="name">'.$MovieTitle.'</span>
<img src="'.$ThumbnailPic.'" alt="">
</a>
</span>
</div>
<div class="rating">
<p>
RATING: '.$Rating.'
</p>
<div class="stars">
<div class="'.$StarGraphic.'"></div>
</div>
<span class="comments"></span>
</div>
</div>
</div>
Aso, you can have some tags or quotes in your database records. So you have to use escaping your variables before output http://php.net/manual/en/function.htmlspecialchars.php
Something like this:
$list = '';
while($row = $results->fetchArray()) {
$id = htmlspecialchars($row["id"]);
$MovieTitle = htmlspecialchars($row["MovieTitle"]);
$MovieYear = htmlspecialchars($row["MovieDate"]);
$MovieRes = htmlspecialchars($row["MovieRes"]);
$FileName = htmlspecialchars($row["FileName"]);
$Summary = htmlspecialchars($row["Summary"]);
$Genres = htmlspecialchars($row["Genres"]);
$PictureLocation = htmlspecialchars($row["PictureLocation"]);
$Rating = htmlspecialchars($row["Rating"]);
$ReleaseDate = htmlspecialchars($row["ReleaseDate"]);
$list .= '<div class="box">
<div class="movie">
<div class="movie-image"><span class="play"><span class="name">'.$MovieTitle.'</span></span><img src="'.$ThumbnailPic.'" alt=""></div>
<div class="rating">
<p>RATING: '.$Rating.'</p>
<div class="stars">
<div class="'.$StarGraphic.'"></div>
</div>
<span class="comments"></span></div>
</div>';
}

DOMPDF sometimes work,and sometimes not

im using DOMPDF to make downloadable webpage.
but sometime it works, and Sometimes the page takes so long and finally displays "This site can not be reached, The connection was reset".
how to fix this?
its code error or its error on my webbrowser?
thanks :)
function __construct()
{
parent::__construct();
$this->load->model("Mproses");
}
public function download($db,$p,$id) {
// Load all views as normal
$data['hasil'] = $this->Mproses->selectWhere($db,$p,$id);
$this->load->view('hasil',$data);
// Get output html
$html = $this->output->get_output();
// Load library
$this->load->library('dompdf_gen');
$today = date('D-M-Y h:i:s');
// Convert to PDF
$this->dompdf->load_html($html);
$this->dompdf->render();
$this->dompdf->stream($today.".pdf",array('Attachment'=>1));
//or without preview in browser
//$this->dompdf->stream("welcome.pdf");
}
thats my controller for dompdf
HTML file im tryin to download :
<div class="row">
<div class="container">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Hasil Pengecekan! <p class="pull-right"><span class="fa fa-backward" aria-label="Kembali"></span> | <b>Kembali</b></p></h3>
</div>
<div class="panel-body">
<p style="color:black;">
<?php if(count($hasil)>0) { ?>
<?php foreach($hasil as $d) { ?>
<hr>
<br>
<center>
<h2 style="color:black;">Hasil Dari User : <?php echo $d->nama; ?></h2>
</center>
<br>
<hr>
<br>
<ul>
<li style="color:black;"><h5>Penyakit</h5><p style="font-size:20px;"><?php echo $d->penyakit;?></p></li>
<li style="color:black;"><h5>Pilihan</h5><p style="font-size:20px;">"<?php echo $d->pil;?>"</p></li>
<li style="color:black;"><h5>Nilai CF</h5><p style="font-size:20px;"><?php echo $d->nilaicf;?> || <?php echo $d->nilaicf*100;?>%</p></li>
</ul>
<?php } ?>
<?php } else { echo 'Maaf Data Yang Dicari Tidak Ada !'; } ?>
</p>
<br>
<hr>
<br>
<center><?php echo anchor(URL.'index.php/topdf/download/riwayat/id_riwayat/'.$d->id_riwayat,'Download',array('style' => 'color:black;font-size:14px;padding:14px;border:1px black solid;')); ?></center>
<br>
</div>
</div>
</div>
</div>
</div>

Limiting number of items to display per page

I have files like (for an instance ) being shown as shown in
http://lawcommission.gov.np/site/NepalLawCommissionFinal/NepalLawCommissionFinal/document.php?cat=112&sub_cat=10008
The document.php has following html to display the files:
<section class="three-fourth">
<div class="sort-by">
<h3>Sort by</h3>
<ul class="sort">
<li>Date ascendingdescending</li>
<li>Name ascendingdescending</li>
<li>Category ascendingdescending</li>
</ul>
<ul class="view-type">
<li class="grid-view">grid view</li>
<li class="list-view">list view</li>
</ul>
</div>
<div class="deals clearfix">
<!--deal-->
<?php
$id = $_GET['cat'];
$base_obj = new Base();
$lst = $base_obj->list_doc($id);
$lst->execute();
foreach ($lst as $rec)
{
$sub_head = $rec['Head_Id'];
$get_sub = $base_obj->list_by_id('Head_Id', $sub_head, 'tbl_heading');
$get_sub->execute();
foreach ($get_sub as $h)
{
$heading = $h['Head_Name'];
}
?>
<article class="one-fourth">
<figure><span style="float:left"><img src="images/uploads/london1.jpg" alt="" style="width:100px" /></span><span style="float:left"></span></figure>
<div class="details">
<h1><?=$rec['Doc_Name'];?></h1>
<span class="address"><?=$heading;?></span>
<span class="price">Added On <em><?=substr($rec['Uploaded_Date_Time'],0,10);?></em> </span>
Download
</div>
</article>
<!--//deal-->
<?php
} ?>
<!--bottom navigation-->
<div class="bottom-nav">
<!--back up button-->
Back up
<!--//back up button-->
<!--pager-->
<div class="pager">
<span>First page</span>
<span><</span>
<span class="current">1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
<span>></span>
<span>Last page</span>
</div>
<!--//pager-->
</div>
<!--//bottom navigation-->
</div>
</section>
<!--//three-fourth content-->
</div>
<!--//main content-->
</div>
<div class="wrap clearfix">
<!--info boxes--><!--//info boxes-->
</div>
Can you guys please suggest a way to show only limited no. of pdf files in one page?
here is how you need to change your function:
public function list_doc($cat, $page=0) {
$itemsPerPage = 20;
$cat = intval($cat, 10);
$start = intval($page, 10) * $itemsPerPage;
if ($start < 0) $start = 0;
$qry = $this->dbobj->db1->prepare("SELECT SQL_CALC_FOUND_ROWS *
FROM tbl_documents, tbl_cat_head_doc
WHERE tbl_cat_head_doc.Cat_Id = " . $cat ." AND
tbl_cat_head_doc.Doc_Id = tbl_documents.Doc_Id AND
tbl_documents.Status = 'a'
limit " . $start . "," . $itemsPerPage);
return $qry;
}
and in main page call function like this:
$lst = $base_obj->list_doc($id, $_GET['page']);
to prevent next question: read about SQL_CALC_FOUND_ROWS and FOUND_ROWS()

Default image using php, can't tell if its catching id

i'm trying to relay the id through this if statement to see if the file exits. If it does I want to use the id to grab that image from the corporation-logos/ folder. If not i'd like to simply show a default image. I can't tell if its catching the id or not as it keeps spitting out the default.
if ($productCount > 0) {
// get all the product details
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$imgStock="http://www.stocksandstocks.com/corporation-logos/$id.jpg";
$http_image="http://www.stocksandstocks.com/images/no-logo-default.png";
if(file_exists($imgStock))
{
$imgPick = "$imgStock";
}
else
{
$imgPick = "$http_image";
}
echo "
<ul>
<li>
<div class='wrapPicks'>
<div class='symbolPicks'>
<div class='quoteTitle'>$quoteTitle</div>
</div>
<div class='bgPicks'>
<img src='$imgPick' height='50px' widht='80px' alt='stock symbol $quoteTitle logo' />
<div class='separateWrap'>
<div class='quote'>
<b>Entry Price:</b> $$entry
</div>
<div class='quote'>
<b>Last trade:</b> $$lastPrice
</div>
</div>
<div class='quoteReturn'>
<b>Open P/L:</b>
<div class='bigGreen'>
<span style=\"color: $color\">$positive $$profit </span>
</div>
</div>
</div>
</div>
</li>
</ul>";

javascript executing php code

i have a php web application project ... and i want to implement a javascript onclick code ..
for example :
when user clicks follow this post ... the MySQL query inserts into database that the user followed this ... so the page is refreshed and after that it will appear as FOLLOWED ..
what's the javascript code needed to do this ... or is there any example may fit ??
here's a sample code
<div id="main_content">
<?php
if(isset($_GET['et']))
{
$et = $_GET['et'];
}
$result = mysql_query("select * from events where event_type =$et") ;
require_once('queries/category_extract.php') ;
?>
<div id="body_content">
<div id="event_tag">
<a><?php echo $cattitle["categ_title"]?>s</a>
</div>
<?php
while($row=mysql_fetch_array($result) )
{
require('queries/followers_extract.php')
?>
<div id="postpreview">
<div id="postpreview_up">
<div id="postpreview_up_left">
<div id="postpreview_up_left_left">
<a>Event Title :</a>
</div>
<div id="postpreview_up_left_right">
<a><?php echo $row["event_title"] ?></a>
</div>
</div>
<div id="postpreview_up_right">
<img src="images/read_more.gif" />
</div>
</div>
<div id="postpreview_bottom">
<div id="postpreview_bottom_left">
<div id="postpreview_bottom_left_left">
<a>Date :</a>
</div>
<div id="postpreview_bottom_left_right">
<a><?php echo $row["event_date"] ?></a>
</div>
</div>
<div id="postpreview_bottom_right">
<div id="postpreview_bottom_right_left">
<?php
if($follower['follower_id'] ==NULL){echo " <img src='images/follow_button.jpg' /> " ; }
else { echo " <img src='images/follow_closed.jpg' /> " ;}
?>
</div>
<div id="postpreview_bottom_right_right">
<?php
if($follower['follower_id'] !=NULL){ echo " <img src='images/following_button.jpg' /> " ; }
else { echo " <img src='images/following_Closed.jpg' /> " ;}
?>
</div>
<div id="postpreview_bottom_right_right">
<?php
if($follower['follower_id'] !=NULL){ echo " <img src='images/unfollow_button.jpg' /> " ; }
else { echo " <img src='images/unfollow_closed.jpg' /> " ;}
?>
</div>
</div>
</div>
</div>
<div id="splitter"></div>
<?php
}
?>
<!--End Of Post Preview-->
<!--End Of Post Preview-->
ok go for this: http://www.9lessons.info/2009/04/exactly-twitter-like-follow-and-remove.html
you have to learn the basics as i read.

Categories