I have next html structure:
<li id="REQUIRED_ITEM_1" class="listing-post">
<a class="listing-thumb" href="blah" title="blah" data-palette-listing-image="">
<img src="REQUIRED_ITEM_2" width="75" height="75" alt="blah"> </a>
<div class="listing-detail ">
<div class="listing-title">
<div class="listing-icon hidden"></div>
blah
<div class="listing-maker">
<span class="name wrap">blah</span>
</div>
</div>
<div class="listing-date">
REQUIRED_ITEM_6
</div>
<div class="listing-price">
Sold
</div>
</div>
</li>
There are few dozens of these <li> on the same page, all with different id and content. The content that I need is marked REQUIRED_ITEM_1 - REQUIRED_ITEM_6.
I am collecting the data from these <li>s with the help of Xpath.
Here is the code I use:
foreach($xpath->query("//li[#class='listing-post']") as $link) {
$REQUIRED_ITEM_1 = $link->getAttribute('id');
$REQUIRED_ITEM_2 = $xpath->query(".//img", $link)->item(0)->getAttribute('src');
$REQUIRED_ITEM_3 = $xpath->query(".//a", $link)->item(1)->getAttribute('href');
$REQUIRED_ITEM_4 = $xpath->query(".//a", $link)->item(1)->getAttribute('title');
$REQUIRED_ITEM_5 = $xpath->query(".//a", $link)->item(2)->getAttribute('href');
$REQUIRED_ITEM_6 = $xpath->query("./div/text", $link)->item(4);
}
It works as intended for the first 5 REQUIRED_ITEMs, however it seems the code to get text contained within listing-date div (REQUIRED_ITEM_6) is wrong.
Also, is this the best way to parse my html and collect data, or is there a better approach?
Here is the xPath to get REQUIRED_ITEM_6
//li[#class='listing-post']//div[#class='listing-date']/text()
That would be little bit faster (but first version may be more safe, since it is less dependent on XML structure).
//li[#class='listing-post']/div/div[#class='listing-date']/text()
So your code must look like something like this (but you may need to adjust it little bit with your php, not sure why you used item(4)).
$REQUIRED_ITEM_6 = $xpath->query(".//div[#class='listing-date']/text()", $link)->item(0)->textContent;
Related
Actually I am beginner programmer in HTML, CSS and PHP. I have simple website for add and register in courses. The user should be add course into website and the courses should be posted on the site.so users can browse and register.
Actually my problem is how to call the course name from database and how to format it with HTML code as I want.
This is the page of courses which is content the list of available courses in website ( please note it is only HTML code, I do that to see how the page will be )
Screenshot of page:
So as you see, the first page include many this HTML code to add course list into website with the following code:
<div class="card card-1">
<a href="http://127.0.0.1/project2/course details/course1.php">
<img src="http://127.0.0.1/project2/icons/coursepic.jpg" alt="Avatar" style="width:101% "></a> <div class="container">
<h4 class="textstyle"><b>Operating System</b> </h4>
<p class="textstyle">Free Course</p>
</div>
</div>
what i want do with PHP?
I want to write a PHP code to replace the P and h4 with the course name, cost of courses from my database for each available course.
Please note: the pic for each course it will be from my pc, no need to call the pic from database.
I tried to write PHP code like below:
<div>
<div class="card card-1">
<a href="http://127.0.0.1/project2/course details/course1.php">
<img src="http://127.0.0.1/project2/icons/coursepic.jpg" alt="Avatar" style="width:101% "></a> <div class="container">
<?php
include_once("db.php");
$result = mysqli_query(OpenCon(), "SELECT Course_Name,cost FROM `course`");
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
while($res = mysqli_fetch_array($result)) {
echo "<p>".$res['Course_Name']."</p>";
echo "<p>".$res['cost']."</p>";
}
?>
</div>
</div>
</div>
This is my result:
It's okay but I want the style to be like the first screenshot. each course should have picture.
After that when the user click on course name. I want move to another page which is content the course details ( for the same course that user clicked ) also it's called for my database
like this:
I hope any one help my to solve this problem only, I should solve this problem within 2 days only. and sorry if my explanation is bad.
Thanks in advance for everyone.
Put the code in a PHP loop.....
So, this
<div class="card card-1">
<a href="http://127.0.0.1/project2/course details/course1.php">
<img src="http://127.0.0.1/project2/icons/coursepic.jpg" alt="Avatar" style="width:101% ">
</a>
<div class="container">
<h4 class="textstyle"><b>Operating System</b> </h4>
<p class="textstyle">Free Course</p>
</div>
</div>
Becomes (after cleaning up the code a bit - I think you didn't mean to use two <p> in there, but I left them so you can see it. Note that using different lines for the segments makes it a lot easier to see what you have.)
include_once("db.php");
$result = mysqli_query(OpenCon(), "SELECT Course_Name,cost FROM `course`");
$count = 0;
while($res = mysqli_fetch_array($result)) {
$count ++;
// NOTE: Here is the LOOP! - not outside the query, but INSIDE it
// First you 'jump out' of PHP, going back to HTML
?> <!-- now you are in HTML (when you need PHP again, you 'jump in' and 'jump out' as needed - see the code below....) -->
<div class="card card-<?php echo $count;?>">
<a href="http://127.0.0.1/project2/course details/course<?php echo $count;?>.php">
<img src="http://127.0.0.1/project2/icons/coursepic.jpg" alt="Avatar" style="width:101% ">
</a>
<div class="container">
<h4 class="textstyle">
<b><p><?php echo $res['Course_Name'];?></p></b>
</h4>
<p class="textstyle">
<p><?php echo $res['cost'];?></p>
</p>
</div>
</div>
<?php // we are in PHP again....
}
That should do what you asked for - though I would go a step (well, more than one...) further and make as much of this dynamic as you can.
For this I will presume that:
your database table has a column called 'id' (if it doesn't, you should have) and it relates to the course number (you could make a course number column if they don't match up, but I'm keeping it simple)
you have all your pictures labeled 'coursepicX' where the X is the course number.
We'll use 'coursepic' as a default in case there isn't a picture yet...
Now, the code is more dynamic!
include_once("db.php");
$result = mysqli_query(OpenCon(), "SELECT id,Course_Name,cost FROM `course`");
while($res = mysqli_fetch_array($result)) {
// NOTE: Here is the LOOP! - not outside the query, but INSIDE it
// First you 'jump out' of PHP, going back to HTML
?> <!-- now you are in HTML (when you need PHP again, you 'jump in' and 'jump out' as needed - see the code below....) -->
<div class="card card-<?php echo $res['id']?>">
<a href="http://127.0.0.1/project2/course details/course<?php echo $res['id']?>.php">
<?php
$pic = "http://127.0.0.1/project2/icons/coursepic.jpg";
if(file_exists("http://127.0.0.1/project2/icons/course" . $res['id'] . ".jpg") {
$pic = "http://127.0.0.1/project2/icons/course" . $res['id'] . ".jpg";
}
<img src="<?php echo $pic; ?>" alt="Avatar" style="width:101% ">
</a>
<div class="container">
<h4 class="textstyle">
<b><p><?php echo $res['Course_Name'];?></p></b>
</h4>
<p class="textstyle">
<p><?php echo $res['cost'];?></p>
</p>
</div>
</div>
<?php // we are in PHP again....
}
Note that this is the basic 'shopping cart' sort of program - you will likely use it many (many) times in your career.
Happy Coding!
I have website and it's source html code looks something like below.
<li class="item" xx-href-xx="http://xx.xx/s/randomtext/randomtext?NOTradnomtext" yy-href-gg="http://xx.xx/X/RANDOMTEXTWHATIWANT/STILLRADNOMTEXTWHATIWANT?NOTradnomtext" data="212123134" data-title="TITLE">
<a class="front" href="#" xx-href="http://xx.xx/s/randomtext/randomtext?NOTradnomtext">
<img src="http://photo.jpg" alt="">
<div class="cock">
<div class="action"></div>
</div>
</a>
<div class="label">
<div>
<h3 class="title">Example</h3>
<p>2013-10-25 : 03:35</p>
</div>
</div>
</li>
... And so on same kind of classes (only titles and texts changing) ...
How to preg_match yy-href-gg="http://xx.xx/X/TEXTWHATIWANT/TEXTWHATIWANT?NOTradnomtext from all of those records and include also title for result. So result should look in this case something like that
Example
TEXTWHATIWANT/TEXTWHATIWANT
Example2
TEXTWHATIWANT/TEXTWHATIWANT
and so on.
Sorry if my post is little bit unclear, I should to go sleep..
I'm using XPath helper in order to create my paths, however for the first time i seem to get a complete wrong output. I've created following path to get the articles links on the current day. just to test i've hardcoded the current date.
//b[contains(., '22/4 - 2015')]/parent::div/following-sibling::div[#class='newsItem']
Instead of returning each newsItem, which it does in XPath Helper, it returns the whole page? how come is that. here is my code
function scrape() {
$hltv = file_get_html("http://www.hltv.org/?pageid=96");
foreach($hltv->find("//b[contains(., '22/4 - 2015')]/parent::div/following-sibling::div[#class='newsItem']") as $hltv_element) {
echo $hltv_element;
}
}
It's not entirely clear what you would like to get as the result, but here is a snippet of relevant HTML to hopefully make that clearer:
<div style="margin-bottom:5px;margin-top:5px;">
<b>22/4 - 2015</b>
</div>
<div class="newsItem">
<a href="/news/14794-video-pyth-vs-dignitas" id="newsitem14794" title="Video: pyth vs. dignitas">
<span style="float:left;">
<img style="vertical-align: 1px;" src="http://static.hltv.org//images/mod_csgo.png" title="Counter-Strike: Global Offensive"/>
<img src="http://static.hltv.org//images/flag/se.gif" alt="" /> </span> <span style="float:left;cursor: hand;width:350px;color:#000000"/>
<b>Video: pyth vs. dignitas</b>
</span>
</a>
<span style="float: right;">(22)</span>
</div>
<div style="clear:both"></div>
<div class="newsItem"><a href="/news/14795-video-keev-vs-myxmg" id="newsitem14795" title="Video: keev vs. myXMG">
<span style="float:left;">
<img style="vertical-align: 1px;" src="http://static.hltv.org//images/mod_csgo.png" title="Counter-Strike: Global Offensive"/>
As you can see, there is a <b>22/4 - 2015</b> that is selected. But its parent, the first div in the snippet, has more than one following div siblings where #class="newsItem". Perhaps you meant to have
//b[contains(., '22/4 - 2015')]/parent::div/following-sibling::div[#class='newsItem'][1]
is simple html dom using an old version of XPath or?
In my opinion, all libraries that include "simple" in their name (SimpleXML, Simple HTML DOM) are not so simple really, and often cause problems. All libraries use XPath 1.0, so that's not the problem. You are better off using DOMDocument and DomXPath.
EDIT
just to be clear: I want to get the titles of the news on the current date
Then use
//b[contains(., '22/4 - 2015')]/parent::div/following-sibling::div[#class='newsItem'][1]/a/#title
I have the following html code:
<div class="media row-fluid">
<div class="span3">
<div class="widget">
<div class="well">
<div class="view">
<img src="img/demo/media/1.png" alt="" />
</div>
<div class="item-info">
Title 1
<p>Info.</p>
<p class="item-buttons">
<i class="icon-pencil"></i>
<i class="icon-trash"></i>
</p>
</div>
</div>
</div>
<div class="widget">
<div class="well">
<div class="view">
<img src="img/demo/media/2.png" alt="" />
</div>
<div class="item-info">
This is another title
<p>Some info and details go here.</p>
<p class="item-buttons">
<i class="icon-pencil"></i>
<i class="icon-trash"></i>
</p>
</div>
</div>
</div>
</div>
Which basically alternates between a span class with the widget class, and then the widget class without the span3 class.
What I wanted to know was if there was a way to have php "echo" or populate the details for and details under the "item-info" class. Would I need to use a foreach statement to get this done? I would be storing the information in a mysql database, and while I can get it to fill in the info one by one (repeatedly entering the and echoing out each image and item title) it's not practical when the content needed to be displayed is over 15 different items. I'm not well versed in foreach statements so I could definitely use some help on it.
If someone could help me perhaps structure a php script so that it can automatically output the html based on the number individual items in the database, that'd be greatly appreciated!
I'm wondering if the html + php (not including the foreach) would look like this:
<div class="span3">
<div class="widget">
<div class="well">
<div class="view">
<img src="img/<? $file ?>" alt="" />
</div>
<div class="item-info">
<?$title?>
<p>Info.</p>
<p class="item-buttons">
<i class="icon-pencil"></i>
<i class="icon-trash"></i>
</p>
</div>
</div>
</div>
EDIT:
I wanted to add some more information. The items populated would be based on a type of subscription - which will be managed by a group id.
I was initially going to use <? (if $_SESSION['group_id']==1)>
echo <div class="item-info">
$title
<p>$info</p>
</div>
so that only the subscribed items would populate. But, I would need it to iterate through all the items for group1 table and list it. Currently I know that I can do
<? (if $_SESSION['group_id']==1)
while ($row=mysql_fetch_assoc($sqlItem))
{
$itemInfo = $row['info'];
$image = $row['image'];
$title = $row['title'];
$url = $row['url'];
};
>
$sqlItem for now can only be assigned one thing (manually - as in: $sqlItem = '123'), unless I iterate through which is what I'm trying to figure out.
Just read that 'mysql_fetch_assoc' is being depreciated with 5.5, here is the new way and looks better, easier I think.. Hope this helps, was updated today.
I hope this helps http://php.net/manual/en/mysqli-stmt.fetch.php
replace the printf with echo '//then your html stuff
This will iterate through the rows in your database until their are no more matching records.
shouldn't a while be enough? It depends on the structure of your database and website (we didn't need so much HTML I think. Some more PHP maybe). Hope this helps.
i am using file_get_html() to get some external HTML, but i have a issue. I cannot seem to target text inside a div, while avoiding getting the rest of the content.
Lets say the layout is this:
<div class="post">
<h1>Andromeda v1.4 – WordPress – The Beauty of Simplicity</h1>
<div class="infos b20">
<img class="post_img" src="/imagini/512b93babf84b.jpg" alt="Andromeda v1.4 – WordPress – The Beauty of Simplicity">
<div style="width:610px; margin:10px 0; overflow:hidden; display:block;">
enter code here
Andromeda is a clean theme with functional CMS and unique features. A massive pack of backend CMS options was created for this product to give you full control while creating and editing the site and its features. The main idea behind this theme was to create a something clean and simple, useful, nice looking and easy to modify.
<p></p>
<h6>Demo</h6>
<code>http://themeforest.net/item/andromeda-wordpress-the-beauty-of-simplicity/107876</code>
<h6>Download:</h6>
<div class="link alert clearfix">
<div class="link alert clearfix">
<div class="link alert clearfix">
<div class="link alert clearfix">
<div class="link alert clearfix">
<div class="link alert clearfix">
<p></p>
<ul id="social_post" class="clearfix sharingbtns">
<div class="comments">
</div>
If i do a
$text = $dom->find('div[class=post]');
$text = $text[0]->plaintext;
I get all the content, I only want the text, inside the main div with the class post, and not all the other content.
What would be the best way to achive this?
Text and amount of other divs are variable, but the div class post, and the text will always be there, in the same position.
EDIT: To elaborate, i only want the text thats inside post, and has no tag
just to answer you quickly without checking out if it works:
http://simplehtmldom.sourceforge.net/manual_api.htm
Try this:
$text = $dom->find('div[class=post]');
$text = $text[0]->innertext;
or:
$text = $dom->find('div[class=post]');
$text = $text[0]->outertext;
By the way:
<div style="width:610px; margin:10px 0; overflow:hidden; display:block;">
has no closing tag so there is no text that's inside the DIV you;re talking about. Please clarify.
$res = $html->find('div[class=post]',0)->plaintext;