Get DIV Element contents thru DOMDocument PHP - php

I have to recover some news from a div of a site. The div is structured as follows:
The HTML Markup:
<ul id="news-accordion" class="rounded" style="padding: 2px;">
<li class="o">
<h3>
<span>TITLE ARTICLE</span>
<span>30/10/2014</span>
</h3>
<div style="display: none;">
<p>text of article</p>
</div>
</li>
<li class="e">
<h3>
<span>TITLE ARTICLE</span>
<span>28/10/2014</span>
</h3>
<div style="display: none;">
<p>text of article</p>
</div>
</li>
<li class="o">
<h3>
<span>TITLE ARTICLE</span>
<span>29/10/2014</span>
</h3>
<div style="display: none;">
<p>text of article</p>
</div>
</li>
</ul>
PHP
<?php
$doc = new DomDocument;
$doc->validateOnParse = true;
$doc->loadHtml(file_get_contents('http://www.xxxxxxxxx/news.php'));
$news = $doc->getElementById('news-accordion');
$li = $news->getElementsByTagName('li');
foreach ($li as $row){
$title = $row->getElementsByTagName('h3');
echo $title->item(0)->nodeValue."<br><br>";
/*foreach ($title as $row2){
echo $row2->nodeValue."<br><br>";
//echo $row2->item(0)->nodeValue."<br><br>";
}*/
$text = $row->getElementsByTagName('p');
echo utf8_decode($text->item(0)->nodeValue)."<br><br><br>";
}
?>
The code works correctly, but when I print the contents of the span tag echo $title->item(0)->nodeValue;,
The text of the two span is printed together.
How can I take the contents of the two span separately? Thanks.

Yes you can, just adjust the ->item() index. Just like what you have done already in the other elements, point it to that header element, then just explicitly point it to those span children:
foreach ($li as $row){
$h3 = $row->getElementsByTagName('h3')->item(0);
$title = $h3->getElementsByTagName('span')->item(0); // first span
$date = $h3->getElementsByTagName('span')->item(1); // second span
echo $title->nodeValue . '<br/>';
echo $date->nodeValue . '<br/>';
$text = $row->getElementsByTagName('p');
echo utf8_decode($text->item(0)->nodeValue)."<br><br><br>";
}

$title = $row->getElementsByTagName('h3');
echo $title->item(0)->nodeValue."<br><br>";
Replace above two line with below (instead of using h3 tag use span tag)
$title = $row->getElementsByTagName('span');
echo $title->item(0)->nodeValue."<br><br>";
echo $title->item(1)->nodeValue."<br><br>";
It's working for me.

Related

Simple HTML DOM find tags and fetch data from page link

Simple HTML DOM find tags and fetch data from page link
Hi I'm Simple HTML DOM, basically i need to get h2 title and the content from
the links (page/id/1). The point I'm getting stack is getting data from page .
The format should be the same that is
Title
contet form lik1 ,
content from link5
title 2
content from link ,
content from 2
<section class="level">
<h2> title </h2>
<a class="links" href="page/id/1">link1 </a>
<a class="links" href="page/id/2">link2 </a>
<a class="links" href="page/id/3">link3 </a>
<a class="links" href="page/id/4">link4 </a>
<a class="links" href="page/id/5">link5 </a>
</section>
<section class="level">
<h2> title 2 </h2>
<a class="links" href="page/id/7">link1 </a>
<a class="links" href="page/id/8">link2 </a>
</section>
<section class="level">
<h2> title 3 </h2>
<a class="links" href="page/id/9">link2 </a>
<a class="links" href="page/id/10">link3 </a>
</section>
I know it should be along these line any help guys
foreach ($html->find('h2') as $key => $value) {
echo $html->find('h2',0)->plaintext;
//this is where Im stack getting the data from the link
foreach ( ) {
echo data from the link example.com/page.php/id/1
echo data from the link example.com/page.php/id/2
}
}
You could find the <section> with the classname level using find('section[class=level]') Then you could for example loop the childnodes and check the nodeName.
To get only the anchors, you could use find('section[class=level] a')
For example:
$html = new simple_html_dom();
$html->load($data);
$result = $html->find('section[class=level]');
foreach ($result as $item) {
foreach($item->childNodes() as $childNode) {
if ($childNode->nodeName() === "h2") {
echo $childNode->innertext . "<br>";
}
if ($childNode->nodeName() === "a") {
echo $childNode->getAttribute("href") . "<br>";
}
}
}
Result
title
page/id/1
page/id/2
page/id/3
page/id/4
page/id/5
title 2
page/id/7
page/id/8
title 3
page/id/9
page/id/10

MySQLi query in variable/function (that can be later used to echo content)

So I'm stuck with a problem. I'm creating some kind of "templated" page (no, I can't use Twig or anything like that) in PHP, that will later be incremented with actual content.
My main problem here is to echo links in navbar. I'm using MaterializeCSS, so in order to make the navbar responsive is to write the links twice inside different ULs. I can easily echo it one time, like this:
<?php
$sql = "SELECT id, pagename, filename FROM NavbarLinks";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$navstructure =
<<<HTML
<header>
<nav class="$primarycolor">
<div class="container">
<div class="nav-wrapper">
$sitename
<ul class="right hide-on-med-and-down">
HTML;
echo $navstructure;
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<li>' . $row['pagename'] . '</li>
' ;
}
}
echo "
</ul>
</div>
</div>
</nav>
</header>";
?>
This works with no errors. The problem is, I need that (according to MaterializeCSS specifications/documentation):
<nav>
<div class="nav-wrapper">
Logo
<i class="material-icons">menu</i>
<ul class="right hide-on-med-and-down">
// links
</ul>
<ul class="side-nav" id="mobile-demo">
// links again
</ul>
</div>
</nav>
So what I wanted to do was simple: Make a navbar.php and have the structure on a HEREDOC variable, and in the variable $navbarlinks I would print all the links I got from the first code I showed you. The problem for me is that putting the links in a variable instead of showing all the results will only print one, and I couldn't find a way to change this.
What should I do to have a $navbarlinks or navbarlinks() that prints all the mySQL results and work everywhere?
You want to just put it in an array for later use. You should also try to separate your PHP from your HTML, this might be a good start but still needs things like HTML escaping. Note in particular the use of alternative syntax and short echo tags.
<?php
$links = [];
$sql = "SELECT id, pagename, filename FROM NavbarLinks";
$result = $conn->query($sql);
if ($result) {
while($row = $result->fetch_assoc()) {
$links[] = array_map('htmlspecialchars', $row);
}
}
// PHP is finished now, here's the HTML
?>
<?php if (count($links)):?>
<header>
<nav class="<?=$primarycolor?>">
<div class="container">
<div class="nav-wrapper">
<?=$sitename?>
<ul class="right hide-on-med-and-down">
<?php foreach ($links as $link):?>
<li><?=$link['pagename']?></li>
<?php endforeach?>
</ul>
<ul class="side-nav" id="mobile-demo">
<?php foreach ($links as $link):?>
<li><?=$link['pagename']?></li>
<?php endforeach?>
</ul>
</div>
</div>
</nav>
</header>
<?php endif?>
no longer practicing php but as far as i can remember html/css elements don't have to be pass to a variable.. you can just end php before the css elements and open php again from your loop
<?php
$sql = "SELECT id, pagename, filename FROM NavbarLinks";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<HTML
<header>
<nav class="$primarycolor">
<div class="container">
<div class="nav-wrapper">
<?php=$sitename?>
<ul class="right hide-on-med-and-down">
<?php
while($row = $result->fetch_assoc()) {
echo '<li>' . $row['pagename'] . '</li>
' ;
}
?>
</ul>
</div>
</div>
</nav>
</header>";
<?php
}
?>
<!-- end -->

Unable pull out the node value of src using getattribute

I am trying to echo out the href and the image src using getattribute but though the href gets echoed correctly I am unable to retrieve the image src...plz guide. below is my
html mockup
<div id="hot-deals">
<div class="all-deals">
<ul>
<li><a href="http://url1.com">
<img src="http://imagelink1.com"></a>
</li>
<li><a href="http://url2.com">
<img src="http://imagelink2.com"></a>
</li>
<li><a href="http://url3.com">
<img src="http://imagelink3.com"></a>
</li>
</ul>
</div>
</div>
my code
$nodes = $my_xpath->query( '//div[#id="hot-deals"]/div[#class="all-deals"]/ul/li/a' );
foreach( $nodes as $node )
{
$title=$node->getAttribute('href');
$img=$node->getAttribute('img/src');
echo $title.",".$img."<br>";
}
src is not attribute of a tag, so you need one more step to get inner img tag and then take its attribute
foreach( $nodes as $node ) {
$title = $node->getAttribute('href');
$imgTags = $node->getElementsByTagName('img');
$img = $imgTags->item(0)->getAttribute('src');
echo $title . "," . $img . "<br>";
}
You can try this code.
<?php
$str = '<div id="hot-deals">
<div class="all-deals">
<ul>
<li><a href="http://url1.com">
<img src="http://imagelink1.com"></a>
</li>
<li><a href="http://url2.com">
<img src="http://imagelink2.com"></a>
</li>
<li><a href="http://url3.com">
<img src="http://imagelink3.com"></a>
</li>
</ul>
</div>
</div>';
$nodes = simplexml_import_dom(DOMDocument::loadHTML($str))->xpath('//div[#id="hot-deals"]/div[#class="all-deals"]/ul/li/a');
foreach( $nodes as $node )
{
$title = $node['href'];
$src = $node->img['src'];
echo $title ." " . $src . '<br>';
}

How to get the element in arrays

I'm working on my PHP to search for the elements. There are are one element called <p id='links'>, I use simple_html_dom method to parsing the contains from my script called get-listing.php.
Here is the example output from get-listing.php:
<p id='channels'>101 ABC FAMILY</p>
<p id='links'>
<a href='http://www.mysite.com/get-listing.php?channels=ABC FAMILY&id=101'>http://www.mysite.com/get-listing.php?channels=ABC FAMILY&id=101</a>
</p>
<a id="aTest" href="">Stream 1</a>
<p id='channels'>102 CBS</p>
<p id='links'>
<a href='http://www.mysite.com/get-listing.php?channels=CBS&id=102'>http://www.mysite.com/get-listing.php?channels=CBS&id=102</a>
</p>
<a id="aTest" href="">Stream 1</a>
<p id='channels'>103 CNN USA</p>
<p id='links'>
<a href='http://www.mysite.com/get-listing.php?channels=CNN USA&id=103'>http://www.mysite.com/get-listing.php?channels=CNN USA&id=103</a>
</p>
<a id="aTest" href="">Stream 1</a>
<p id='channels'>105 ESPN USA</p>
<p id='links'>
<a href='http://www.mysite.com/get-listing.php?channels=ESPN USA&id=105'>http://www.mysite.com/get-listing.php?channels=ESPN USA&id=105</a>
</p>
<a id="aTest" href="rtmp://$OPT:rtmp-raw=rtmp://ny.iguide.to/edge playpath=49f5xnbs2wra0ut swfUrl=http://player.ilive.to/player_ilive_2.swf pageUrl=http://www.ilive.to token=UYDk93k#09sdafjJDHJKAD873">Stream 1</a>
<p id='channels'>106 FOX News</p>
<p id='links'>
<a href='http://www.mysite.com/get-listing.php?channels=FOX News&id=106'>http://www.mysite.com/get-listing.php?channels=FOX News&id=106</a>
</p>
<a id="aTest" href="">Stream 1</a>
<p id='channels'>107 Animal Planet</p>
<p id='links'>
<a href='http://www.mysite.com/get-listing.php?channels=Animal Planet&id=107'>http://www.mysite.com/get-listing.php?channels=Animal Planet&id=107</a>
</p>
<a id="aTest" href="">Stream 1</a>
<p id='channels'>108 USA Network</p>
<p id='links'>
<a href='http://www.mysite.com/get-listing.php?channels=USA Network&id=108'>http://www.mysite.com/get-listing.php?channels=USA Network&id=108</a>
</p>
<a id="aTest" href="">Stream 1</a>
Here is my PHP script:
<?php
ini_set('max_execution_time', 300);
$errmsg_arr = array();
$errflag = false;
$link;
include ('simple_html_dom.php');
$base1 = "http://www.mysite.com/get-listing.php";
$html = file_get_html($base1);
$countp = $html->find('p');
header("Content-type: text/xml");
$xml .= "<?xml version='1.0' encoding='UTF-8' ?>";
//echo $xml;
$xml .= '<tv generator-info-name="www.testbox.elementfx.com/xmltv">';
?>
I want to create the loops to get the url in each array from get-listing.php with one element id=links.
Can you please tell me how I can do that?
Assuming simple_html_dom.php gets your data as described here http://simplehtmldom.sourceforge.net/ then you should be able to use
foreach to go through the results
$links = $html->find('p[id=links] a');
foreach ($links as $link) {
//Get raw URL's here
$urls[] = $link->href;
}
EDIT
if you want to sort through the hrefs you could do a few simple tests here
foreach ($links as $link) {
//Get raw URL's here
if (strstr($link->href,'get_listing')) {
$listings[] = $link->href;
} else {
$general[] = $link->href;
}
}

php display data in div, h1, ul using while loop

I am not very good in php and mysql but based on my code, I am able to display the results that I wanted but, it is not displaying in the right DOM structure.
I would like to display like below:
<div class="container">
<h1>Header</h1>
<ul><li>
<p>data 1</p>
<p>data 2</p>
</li></ul>
</div>
<div class="container">
<h1>Header</h1>
<ul><li>
<p>data 1</p>
<p>data 2</p>
</li></ul>
</div>
I have no idea why my code keep returning me the data and display everything in the 1st level ul instead of separating like below:
<div class="container">
<h1>Header</h1>
<ul><li>
<p>data1</p>
<p>data 2</p>
<div class="container">
<h1>Header</h1>
<ul><li>
<p>data1</p>
<p>data 2</p>
</li></ul>
</div>
</li></ul>
</div>
I have played around with the code and tried different ways but still unable to get it display properly. Below is my code:
<?php
$data='';
$previousVal = '';
// Update new images
$levelArray=array('B1','L1','L2','main','L3','L4','L5');
foreach ($levelArray as $i=>$level) {
$img = "img/".$level.".jpg";
if($level=='main'){
$result = mysqli_query($con,"SELECT * FROM floor_directory ORDER BY categories");
}
else {
$result = mysqli_query($con,"SELECT * FROM floor_directory WHERE level='$level' ORDER BY categories");
}
while($row = mysqli_fetch_array($result)){
if($previousVal != $row['categories']){
$data .= '<div class="container"><h1>'.$row['categories'].'</h1>';
$previousVal = $row['categories'];
}
$data .= '<ul><li>
<p class="float_left">'.$row['name'].'</p>
<p class="float_right">'.$row['unit_number'].'</p>
</li></ul></div>';
}
if($levelArray[$i]=='main'){
echo '<div class="swiper-slide">
<img src="'.$img.'" alt="" />
<div class="content_container_main">'.$data.'</div>
</div>';
}
else {
echo '<div class="swiper-slide">
<img src="'.$img.'" alt="" />
<div class="content_container">'.$data.'</div>
</div>';
}
$data='';
$previousVal = '';
}
?>
Hope you guys understand. Thanks in advance for the help guys.
changing code in while will make it work, change this code,
while($row = mysqli_fetch_array($result)){
if($previousVal != $row['categories']){
$data .= '<div class="container"><h1>'.$row['categories'].'</h1>';
$previousVal = $row['categories'];
}
$data .= '<ul><li>
<p class="float_left">'.$row['name'].'</p>
<p class="float_right">'.$row['unit_number'].'</p>
</li></ul></div>';
}
with this,
while($row = mysqli_fetch_array($result)){
if($previousVal != $row['categories']){
$previousVal = $row['categories'];
}
$data .= '<div class="container"><h1>'.$row['categories'].'</h1>';
$data .= '<ul><li>
<p class="float_left">'.$row['name'].'</p>
<p class="float_right">'.$row['unit_number'].'</p>
</li></ul></div>';
}
you may even remove the if check (inside while loop) if not using in further script.

Categories