I'm using a code to extract the items from an ebay rss feed, the only problem is that it is only extracting one item.
I suspected it was because of for each, but after searching this whole site, I couldn't find a solution. The feed URL will output 8 items (entriesPerPage=8), if you access the feed, you'll that the full xml code is there, but the parser is only getting one item.
<?php
$feedurl = "http://rest.ebay.com/epn/v1/find/item.rss?keyword=%28jewelry%2Ccraft%2Cclothing%2Cshoes%2Cdiy%29&sortOrder=BestMatch&programid=1&campaignid=5337945426&toolid=10039&listingType1=All&lgeo=1&topRatedSeller=true&hideDuplicateItems=true&entriesPerPage=8&feedType=rss";
$rss = simplexml_load_file($feedurl);
foreach ($rss->channel->item as $item) {
$link = $item->link;
$title = $item->title;
$description = $item->description;
}
?>
<div class="mainproductebayfloatright-bottom">
<div class="aroundebay">
<?
print "<div class=\"titleebay\">" . $title . "</div>";
print $description;
?>
</div>
</div>
?>
Move your html inside a loop, as currently on each iteration your variables are overwritten and after the loop is over what you have is values of the last xml-item:
foreach ($rss->channel->item as $item) {
$link = $item->link;
$title = $item->title;
$description = $item->description;?>
<div class="mainproductebayfloatright-bottom">
<div class="aroundebay">
<?php
// simple title
print "<div class=\"titleebay\">" . $title . "</div>";
// title-link
print "<a href=\"" . $link . "\">" . $title . "</div>";
print $description;
?>
</div>
</div>
<?php
}
Related
I have a result from a curl request from a page like this:
$result =
<div class="c-wrapper">
<a href="link-to-a-page.php">
<div class="c-content-img">
<img src="...">
</div>
<div class="c-link-data">
<div class="c-link-data-title">
<h4>TITLE</h4>
</div>
</div>
</a>
<div>
<div class="c-wrapper">
<div class="c-content-img">
<img src="...">
</div>
<div class="c-link-data">
<div class="c-link-data-title">
<h4>TITLE 2</h4>
</div>
</div>
<div>
Now I have to count how many c-wrapper is present:
I use correctly this:
$doc = new DOMDocument();
#$doc->loadHTML($result);
$xpath = new DOMXPath($doc);
$divs = $xpath->query("//div[contains(#class, 'c-wrapper')]");
echo $divs-length; //<--- printed: 2
Then I have to print all titles:
I use correctly this:
$titles = $xpath->query("//div[contains(#class, 'c-link-data-title')]/h4");
foreach ($titles as $title) {
echo $title->textContent . "<br>";
}
Now the part I don't know: In the first div is present a link, in the second one no link. I'd like to edit my print of titles like this:
foreach ($titles as $title) {
if ( $link_extracted !="" )
echo "<a href='" . $link_extracted . "'>" . $title->textContent . "</a><br>";
else
echo $title->textContent . "<br>";
}
How can I edit $titles = $xpath->query("//div[contains(#class, 'c-link-data-title')]/h4"); to achieve this?
Rather than doing this in separate stages, the code finds the c-wrapper elements and then further uses XPath to find the various parts you want inside that particular element, so in
$link_extracted = $xpath->evaluate("a/#href", $div)[0];
it is looking for an <a> element relative to the $div element. Using [0] as you want only the first one.
$doc = new DOMDocument();
#$doc->loadHTML($result);
$xpath = new DOMXPath($doc);
$divs = $xpath->query("//div[contains(#class, 'c-wrapper')]");
echo $divs->length;
foreach ( $divs as $div ) {
$link_extracted = $xpath->evaluate("a/#href", $div)[0];
$title = $xpath->evaluate("descendant::div[contains(#class, 'c-link-data-title')]/h4/text()"
, $div)[0];
if ( !empty($link_extracted->nodeValue) ) {
echo "<a href='" . $link_extracted->nodeValue . "'>" . $title->textContent . "</a><br>";
}
else {
echo $title->textContent . "<br>";
}
}
which for your test HTML gives...
2<a href='link-to-a-page.php'>TITLE</a><br>TITLE 2<br>
I have a page where I need to pull the titles of 3 of the latest records in one bootstrap row and then few lines further in the next row I need to pull the image of each record (only 3) again.
How can I do this?
On your loop construct two series of HTML entries, one for your titles and another one for your pictrures. Then echo each HTML string to the div you want them to be.
<?php
titlesHTML = "";
imagesHTML = "";
foreach ($result as $row) {
$contentTitle = $row['cont_title'];
$contentImage = $row['cont_picture'];
$titlesHTML .= "<p>" . $contentTitle . "</p>";
$imagesHTML .= "<p>" . $contentImage . "</p>";
// if you want not to echo the image location but the image itself try the following code instead:
// $imagesHTML .= "<img src='" . $contentImage . "'>";
}
?>
<div class="row">
<div class="col-em-12">
<?php echo $titlesHTML; ?>
</div>
<div>
<div class="row">
<div class="col-em-12">
<?php echo $imagesHTML; ?>
</div>
<div>
I am trying to scrape some content using simple_html_dom without luck.
I am trying to grab the title, image path and the link and display it.
The HTML structure is:
<div class="article_item clearfix">
<h2 class="title">My amazing Title</h2>
<p class="date">September 22 2014</p>
<p class="image_left">
<a href="http://www.demodomain/articleid=1">
<img src="http://www.demodomain/photos/cef78533cd5.jpg" alt="My amazing post ">
</a>
</p>
<p>This is a demo description<strong>of this amazing</strong> article</p>
<p class="more">Read more...</p>
</div>
My code so far:
foreach($html->find('article_item') as $article) {
$item['title'] = $article->find('.title, a', 0)->plaintext;
$item['thumb'] = $article->find('.image_left img', 0)->src;
$item['details'] = $article->find('p', 0)->plaintext;
$item['url'] = $article->find('.more, a', 0)->plaintext;
echo 'Title: ' . $item['title'];
echo "</br>";
echo "image url: " . $item['thumb'];
echo "</br>";
echo "Description: " . $item['details'];
echo "</br>";
echo "Read More Url: " . $item['url'];
}
// Clear dom object
$html->clear();
unset($html);
You didn't state whats not working but consider this example:
foreach($html->find('div.article_item') as $div) {
// ^ point to div tag with class name article_item
$title = $div->find('h2.title a ', 0)->innertext;
// ^ target the h2 tag with class title with child anchor
// just same as accessing dom with jquery
$thumb = $div->find('p.image_left img ', 0)->src;
$details = $div->children(3)->plaintext;
// $url = $div->find('p.more', 0)->plaintext;
$url = $div->find('p.more a', 0)->href;
echo $title . '<br/>';
echo $thumb . '<br/>';
echo $details . '<br/>';
echo $url . '<br/>';
}
Basically, this is just the same as selecting selectors.
can you try like this
$item['title'] = $article->find('h2.title')->plaintext;
$item['thumb'] = $article->find('p.image_left')->find('img')->src;
Right now I have the following PHP:
<?php
$pages = get_pages( array( 'sort_column'=>'menu_order', 'parent'=>'0') );
foreach ($pages as $page_data) {
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
$slug = $page_data->post_name;
$pageid=$page_data->ID;
echo '<div class="section'.(($title=='intro')?' intro-section':"").'">';
echo $content;
echo "</div>";
}
?>
There are 5 Pages, 2 of which have many child Pages. I want to take the above code further one level, so that each page will have its own wrapper, and, if a Page has children Pages, those pages will be placed within the same .section, but each within a separate div. Any advice? I'm a bit inexperienced in Wordpress so any help would be great. Below should give an example of what I want:
<div class="section">
<div class="page parent"></div>
</div>
<div class="section">
<div class="page parent"></div>
<div class="page child"></div>
<div class="page child"></div>
</div>
<div class="section">
<div class="page parent"></div>
</div>
Below is the code I'm working with right now.
<?php while (have_posts()) : the_post(); ?>
<?php
$pages = get_pages( array( 'sort_column'=>'menu_order', 'parent'=>'0') );
foreach ($pages as $page_data) {
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
$slug = $page_data->post_name;
$pageid=$page_data->ID;
//Put a container around every page's content
if (count(get_pages('child_of=' . $page_data->ID.''))) {
// Loop through $page_data array to print the div you want
echo '<div class="section">';
echo '<div class="slide">';
echo $content;
echo "</div>";
$children = get_pages('child_of=' . $page_data->ID.'');
foreach( $children as $child ) {
$childcontent = apply_filters('the_content', $child->post_content);
echo '<div class="slide">';
echo $childcontent;
echo "</div>";
}
echo "</div><!--end section-->";
} else {
echo '<div class="section'.(($title=='intro')?' intro-section':"").'">';
echo $content;
echo "</div>";
}
}
?>
<?php endwhile ?>
Use child_of parameter of get_pages() function inside the loop. This parameter lists the sub-pages of a single Page only. It uses the ID for a Page as the value.
Eg.
if (count(get_pages('child_of=' . $page_data->ID))) {
// Loop through $page_data array to print the div you want
}
I'm fetching an RSS feed with Simplepie on a Wordpress blog. Each RSS item has a div container, and I want each one to have a unique id number (something like <div id="div-#">).
How would I generate a unique number for each item? Here's the code I'm using:
<ul>
<?php include_once(ABSPATH . WPINC . '/feed.php');
$rss = fetch_feed(http://www.example.com);
if (!is_wp_error( $rss ) ) :
$maxitems = $rss->get_item_quantity(6);
$rss_items = $rss->get_items(0, $maxitems);
endif; ?>
<?php
if ($maxitems == 0) echo '<li>No items.</li>';
foreach ( $rss_items as $item ) : ?>
<a href="<?php echo $item->get_link();?>">
<?php
echo '<li><div class="item-headline" id="div-">'.
$item->get_title().
'</div><div class="item-info">'.
$item->get_description().
'</div></li>';
?>
</a>
<?php endforeach; ?>
</ul>
Use get_id(true) to get a unique MD5 hash of the item.
echo '<li><div class="item-headline" id="div-'.$item->get_id(true).'">'.
Try something like:
<?php
foreach ($feed->get_items() as $item) {
echo '<div id="item-<?php echo $num ?>">';
// ...
}