I've exported all product items from my shop as an XML-File and parsed these items with PHP in my single.php of my theme. The parse works fine so far.
Now i want to only display products which category name matches with the ones that have been set in the article editor of WP. This is my code:
<?php
// Parse XML Feed
$html = "";
$url = "http://example.com/feed.xml";
$xml = simplexml_load_file($url);
$item = $xml->channel->item;
$categories = get_the_category();
for($i = 0; $i < count($item); $i++){
$title = $item[$i]->title;
$link = $item[$i]->link;
$category = $item[$i]->category;
$img = $item[$i]->description->a->img["src"];
// This approach breaks my site. A loop in another loop seems not to be that good
// foreach ($categories as $i => $cat) {
// $catName = $cat->name;
// if (preg_match("/\b$catName\b/", $category)) {
// $html .= "
// <a href='$link'><h3>$title</h3>
// <img src='$img'>
// <p>$category</p></a>
// <hr>";
// }
// }
if (preg_match("/\bCATEGORY_NAME\b/", $category)) {
$html .= "
<a href='$link'><h3>$title</h3>
<img src='$img'>
<p>$category</p></a>
<hr>";
}
}
echo $html;
?>
If i change CATEGORY_NAME to an category name which exits, then the output works like i want. The other approach with that loop breaks my site with an "Fatal error: Allowed memory size of *** bytes exhausted…".
Would love to hear some tips
EDIT: Here is some example code
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="www.example.com/export.xml" rel="self" type="application/rss+xml" />
<title>www.example.com</title>
<description>desc</description>
<link>www.example.com</link>
<language>1-1</language>
<image>
<url>www.example.com/logo.gif</url>
<title>test page</title>
<link>http://example.com</link>
</image>
<item>
<title>Example item</title>
<guid>www.example.com/item</guid>
<link>www.example.com/item</link>
<description>
<a href="www.example.com/item" style="border:0 none;">
<img src="www.example.com/image.png" align="right" style="padding: 0pt 0pt 12px 12px; float: right;" />
</a>
</description>
<category>Categoryname</category>
<pubDate>Thu, 24 Aug 2017 17:19:17 +0200</pubDate>rn</item>
</channel>
</rss>
Related
Below is my PHP code and XML file, I have been trying with so many different echos to loop through my images in my XML file to display each image to correct product but can only display the first image to all three products.
XML code:
<my_products>
<product>
<id>1</id>
<image> csuT.jpg</image>
<name>Champion T-Shirt</name>
<price>18.00</price>
<description>
Get the perfect look to let everyone know you are a stylish fan!
</description>
</product>
<product>
<id>2</id>
<image> webBook.jpg</image>
<name>C# Programming: Analysis to Program Design</name>
<price>192.00</price>
<description>
Your hands-on guide to Microsoft Visual C# fundamentals with Visual Studio 2017
</description>
</product>
<product>
<id>3</id>
<image> calcPic.jpg</image>
<name>Calculator TI-BAII Plus 10DIG/24CASH</name>
<price>39.00</price>
<description>
Performs common math as well as various financial functions
</description>
</product>
</my_products>
PHP code:
$xml = simplexml_load_file($file);
$script_images="";
$script_products="";
$script_product_prices="";
//Loop through the products defined in the products.xml file
foreach ($xml->product as $r)
{
$script_images.="products[".($r->id)."]=\"".($r->image)."\";\n";
$script_products.="products[".($r->id)."]=\"".($r->name)."\";\n";
$script_product_prices.="product_prices[".($r->id)."]=\"".($r->price)."\";\n";
?>
<div>
<p class="lead">
<h3 class="pull-right no-top-margin"><?php echo $currency_symbol;?><?php echo $r->price;?></h3>
<h3><?php echo "<image src='csuT.jpg' 'calcPic.jpg' 'webBook.jpg'/>";?></h3>
</p>
<h3><?php echo $r->name;?></h3>
</p>
<p>
<?php echo $r->description;?>
</p>
<br/>
<?php
//If there is details link set for the product, show a Details button
if(trim($r->details_link)!="")
{
?>
<a target="_blank" href="http://<?php echo str_replace("http://","",trim($r->details_link));?>"</a>
<?php
}
?>
<a class="btn btn-xs btn-info" href="javascript:AddToCart(<?php echo $r->id;?>)">Add to Cart</a>
</div>
<hr/>
<?php
}
?>
<script>
var currency_symbol="<?php echo $currency_symbol;?>";
var products=Array();
<?php echo $script_images;?>
var product_images=Array();
<?php echo $script_products;?>
var product_prices=Array();
<?php echo $script_product_prices;?>
</script>
You just need to extract the image from the XML (the same way as done for the Javascript) and I've added a trim() to remove any spaces round the field...
<h3><?php $image = trim($r->image);
echo "<image src='$image'/>";?></h3>
Also add trim to...
$script_products.="products[".($r->id)."]=\"".($r->name)."\";\n";
What is this? :
<h3><?php echo "<image src='csuT.jpg' 'calcPic.jpg' 'webBook.jpg'/>";?></h3>
your src attribute is static and you must change it
Hi I am trying to load content from an XML file called articles.xml:
It has and as the elements.
<?xml version="1.0"?>
<RecentArticles>
<Article author="The Reddest">
<Title>Silverlight and the Netflix API</Title>
<Date>1/13/2009</Date>
<Description>Description</Description>
<Link></Link>
</Article>
<Article author="The Hairiest">
<Title>Cake PHP 4 - Saving and Validating Data</Title>
<Date>1/12/2009</Date>
<Description>Description</Description>
<Link></Link>
</Article>
<Article author="The Tallest">
<Title>Silverlight 2 - Using initParams</Title>
<Date>1/6/2009</Date>
<Description>Description</Description>
<Link></Link>
</Article>
<Article author="The Fattest">
<Title>Controlling iTunes with AutoHotkey</Title>
<Date>12/12/2008</Date>
<Description>Description</Description>
<Link></Link>
</Article>
</RecentArticles>
This is the following PHP code I am using to print the elements into the table:
<!--Make table and print each xml element into it-->
<center>
<table border="1">
<tr>
<th>Title</th>
<th>Date</th>
<th>Description</th>
<th>Link</th>
</tr>
<?php
//Load the xml file int a variable for use in the table below.
$xml = simplexml_load_file("articles.xml");
echo("<tr>");
foreach ($xml->RecentArticles->Article as $entry)
{
$title = $entry['Title']
$date = $entry['Date'];
$description = $entry['Description'];
$link = $entry['Link'];
echo("<td>$title</td>");
echo("<td>$date</td>");
echo("<td>$description</td>");
echo("<td>$link</td>");
}
echo("</tr>");
?>
</table>
</center>
However nothing is printing into the table.. does anyone have any idea why?
You are accessing a (non-existent) attribute value rather than the node value for your content.
Access "Title" attribute value on the "Article" node:
$title = $entry['Title']
Access "Title" node value:
$title = $entry->Title
Example XML:
<RecentArticles>
<Article author="The Reddest" Title="This is what you are accessing with $entry['Title']">
<Title>This is what you should be accessing with $entry->Title</Title>
<Date>1/13/2009</Date>
<Description>Description</Description>
<Link></Link>
</Article>
</RecentArticles>
See this documentation for more information on how to access XML elements.
I am currently writing a PHP script to read all nodes of a XML file with more than three node levels (depth > 2). However I only could read accurately upto first level child.
I would highly appreciate, if you could let me know the error I have made while I am trying to read second level child nodes.
My xml file
<?xml version="1.0" encoding="utf-8"?>
<document nipperstudio="2.3.10.3500" xmlversion="2" xmlrevision="3">
<report>
<part index="1" title="Your Report" ref="YOURREPORT">
<section index="1.1" title="Introduction" ref="INTRODUCTION">
<text>Inside the section 1.1.:</text>
<list type="bullet">
<listitem>detailed description of list item 01;</listitem>
<listitem>detailed description of list item 02;</listitem>
</list>
</section>
<section index="1.2" title="Report Conventions" ref="REPORTCONVENTIONS">
<text>This report makes use of the text conventions.</text>
<table index="3" title="Report text conventions" ref="REPORTTEXTCONVENTIONS">
<headings>
<heading>Convention</heading>
<heading>Description</heading>
</headings>
</table>
</section>
</part>
<part index="2" title="Security Audit" ref="SECURITYAUDIT">
<section index="2.1" title="Introduction" ref="INTRODUCTION">
<text>Inside the section 2.1.:</text>
<list type="bullet">
<listitem>detailed description of list item 01;</listitem>
<listitem>detailed description of list item 02;</listitem>
</list>
<section index="2.1.1" title="Issue Overview" ref="ISSUEOVERVIEW">
<text>Inside the section 2.1.1</text>
<text title="Issue Finding">The is the body text of 2.1.1.</text>
</section>
<section index="2.1.2" title="Rating Overview" ref="RATINGSYSTEM">
<text>Inside the section 2.1.1</text>
<text title="Issue Finding">The is the body text of 2.1.1.</text>
</section>
</section>
<section index="2.2" title="section title" ref="SECTION2.2">
<section index="2.2.1" title="Finding" ref="FINDING">
<text>Inside the section 2.2.1</text>
<text title="Issue Finding">The is the body text of 2.2.1.</text>
</section>
</section>
</part>
</report>
</document>
My PHP Script is given below.
Test XML Reader
<html>
<title>Test XML Reader</title>
<body>
<p>Output from xmlreader</p>
<?php
readXmlFiles();
?>
</body>
</html>
<?php
function readXmlFiles(){
// create the reader object
$reader = new XMLReader();
// reader the XML file.
$reader->open("./fwxml/03.xml"); //open the xml file to read.
while($reader->read()) {
switch($reader->nodeType) {
case (XMLREADER::ELEMENT):
if ($reader->localName == 'report') { //read the local name of the node
$node = $reader->expand();
$dom = new DomDocument();
$n = $dom->importNode($node,true);
$dom->appendChild($n);
foreach(($dom->getElementsByTagName('part')) as $fwpart) {
$parttitle = $fwpart->getAttribute('title');
echo "=====".$parttitle."=====<br>";
foreach(($fwpart->childNodes) as $cnode){
if($cnode->nodeName == 'section'){
$index = $cnode->getAttribute('index');
$title = $cnode->getAttribute('title');
$ref = $cnode->getAttribute('ref');
echo "Index = " .$index."<br>";
echo "Title = " .$title."<br>";
echo "Ref = " .$ref."<br>";
$fwsec = $dom->getElementsByTagName('section');
echo $fwsec->item(0)->nodeValue."<br>";
echo "<br><br><br>";
}//end of if
}//end of foreach
}
}
break; //end of XMLREADER::ELEMENT
case (XMLREADER::END_ELEMENT):
// do something based on when the element closes.
break;
}
}
} //end of function
?>
This question already has answers here:
SimpleXML: Selecting Elements Which Have A Certain Attribute Value
(2 answers)
Closed 8 years ago.
I am trying to build a basic shopping cart using only PHP and XML. I have chosen to use simplexml and xpath here. Items are added fine, but when it comes to the "getProduct($id)" method, which will return the product with a matching code, I am faced with the following error:
Warning: Invalid argument supplied for foreach() in
/srv/disk6/1664324/www/mattc.biz.ht/cart.php on line 203
Here is a snippet from my XML (For the Structure)
<?xml version="1.0" encoding="utf-8"?>
<GreenTrade>
<brand id="Alara">
<product code="1">
<name>Fair Trade Muesli x 500g</name>
<desc>Piced with cinnamon and honey</desc>
<image>1.jpg</image>
</product>
<product code="2">
<name>Luxury Gluten Free Muesli x 500g</name>
<desc>Gluten Free - Wheat Free - High Proteins (Over 10 Percent) - Balanced Nutrition - Vegan </desc>
<image>luxuryglutenfree.jpg</image>
</product>
<product code="3">
<name>Of the Earth - Goji berries x 60g</name>
<desc>Bursting with Beta-Carotene and all 8 Essential Amino Acids </desc>
<image>gojiberries.jpg</image>
</product>
</brand>
<brand id = "BigOz">
<product code="5">
<name>Corn Flakes x 350g</name>
<desc> Gluten Free – Dairy free – Low in Sodium – No added artificial flavours, colours or preservatives – Naturally cholesterol and sodium free </desc>
<image>5.jpg</image>
</product>
</brand>
<brand id="Windmill">
<product code="8">
<name>Amisa Organic Corn and Rice Rigatoni</name>
<desc>Organic, Gluten Free, Dairy Free, Wholegrain</desc>
<image>corn_rice_rigatoni.jpg</image>
</product>
</brand>
</GreenTrade>
And here is my PHP / HTML. For some reason I was not allowed to add PHP tags so i put where they would be in comments. The page would be accessed from a URL Such as:
http://www.mattc.biz.ht/cart.php?action=add&item=4 (Actual Site)
<body>
<div class="container">
<div class="header"><img src="Mattc.fw.png" alt="Insert Logo Here" name="Insert_logo" width="180" height="90" id="Insert_logo" style="background-color: #C6D580; display:block;" />
<!-- end .header --></div>
<div class="sidebar1">
<ul class="nav">
<li>Home</li>
<li>Store</li>
<li>Cart</li>
<li>Email</li>
</ul>
<p>Left Side Bar</p>
<!-- end .sidebar1 --></div>
<div class="content">
<h1>Your Shopping Cart</h1>
//OPEN PHP TAG
$xml = simplexml_load_file('GreenTrade.xml');
$brands = $xml->xpath('//brand');
$prodz = $xml->xpath('//product');
$product_id = $_GET [item];
$action = $_GET [action];
//$contains = $_SESSION['my_cart'];
$brandz = 3;
//Adds 1 to QTY
if (!empty($_GET['item']) && $action == "add") {
$_SESSION['cart'][$product_id]++;
}
//Delete 1 From QTY
if (!empty($_GET['item']) && $action == "del") {
$_SESSION['cart'][$product_id]--;
if($_SESSION['cart'][$product_id] <= 0){
unset($_SESSION['cart'][$product_id]);
}
}
if ($action == "empty") {
unset($_SESSION['cart']);
}
echo '</br><hr align="CENTER">';
if($_SESSION['cart']){
echo "<div align='center'><a href='cart.php?action=empty'>Empty Cart</a></div>";
foreach($_SESSION['cart'] as $prd => $quantity){
$product=getProduct($prd); //<--- Call to getProduct
echo "Name: " . $product->name . '</br>';
echo "Description: " . $product->desc . '</br>';
echo '<img src=' . $product->image . ' >' . '</br>';
echo "Ordered: " . $quantity . '</br>';
echo 'Add Another' . '</br>';
echo 'Remove One' . '</br>';
echo '</br><hr align="CENTER">';
}
} else {
echo '<h2>'."Your Cart Is Empty".'</h2>';
}
//VVV Here Is the Troublesome Function VVV
function getProduct($id){
foreach($prodz as $prod){
if($prod['code'] == $id){
return $prod;
}
}
}
// CLOSE PHP TAG
<!-- end .content --></div>
<div class="footer">
<p>MattC.biz.ht (C) Matthew Cassar 2014</p>
<p>A Chris Porter Web Project <!-- end .footer --></p>
</div>
<!-- end .container --></div>
</body>
The array must be passed as a paramter because $prodz is out of scope (compare: PHP variable scope.
function getProduct(array $products, $id)
{
foreach ($products as $product) {
if ($product['code'] == $id) {
return $product;
}
}
}
for the past 6 weeks I have been developing a custom social network in php and mysqli. Anyway today I decided to create a feed for the blogging system. I have checked my script with the w3 feed validation service and it works fine!
The problem is that none of the feeds are showing up on the page, here is my script.....>
<?php include"php_includes/db_conx.php"; ?>
<?php header('Content-type: text/xml'); ?>
<?php echo '<?xml version="1.0" encoding="iso-8859-1"?>';
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Birdbooks Blog Feed</title>
<description>Birdbook is a Social Network with Birders in mind</description>
<link>http://www.birdbook.org.uk/</link> <copyright> copyright 2013 Birdbook Social Network</copyright>
<atom:link href="http://www.birdbook.org.uk/feed/" rel="self" type="application/rss+xml" />
<?php $query = "SELECT * FROM 'blog' ORDER BY 'delimiter' DESC LIMIT 0,15";
$results = $db_conx->query($query);
$number = $results->num_rows;
for ($i = 1; $i <= $number; $i++) {
$row = $results->fetch_assoc();
$title = $row['blogtitle'];
$description = $row['description'];
$link = $row['linklabel'];
$date = date("r", $row['lastmodified']);
?>
<item>
<title><?php echo $title; ?></title>
<description><?php echo $description; ?></description>
<link><?php echo $link; ?></link>
<pubDate><?php echo $date; ?></pubDate>
<guid><?php echo $link; ?></guid>
</item>
<?php } ?>
</channel>
</rss><?php $db->close(); ?>
Anyone got any Ideas?
Many thanks.. Phillip Dews