I have an XML file which has the following structure:
<item>
<title></title>
<link></link>
<description></description>
<content:encode></content:encode>
<pubDate></pubDate>
<author></author>
<guid></guid>
<dc:date></dc:date>
</item>
I am displaying specific data from the above.
<?php
$importPathHref="test.xml";
$importBuffer = implode("",file($importPathHref));
$dom = new DomDocument();
$dom->loadXML($importBuffer);
$xpath = new DOMXpath($dom);
$items = $xpath->query("//item");
$results = parse($items);
$col = $dom->getElementsByTagName('item');
$i = 0;
foreach( $col as $item ){
$blogTitle=$item->childNodes[1]->nodeValue;
$blogImage=$item->childNodes[5]->nodeValue;
$blogDate=$item->childNodes[9]->nodeValue;
$blogAuthor=$item->childNodes[11]->nodeValue;
if(++$i > 4) break;
?>
<?php if ($i == 1) { ?>
<div class="item__card">
<div class="item__cardImg">
<?php echo $blogImage; ?>
</div>
<div class="item__cardContent">
<span class="author"><?php echo $blogAuthor; ?></span>
<span class="title"><?php echo $blogTitle; ?></span>
<span class="date"><?php echo $blogDate; ?></span>
</div>
</div>
<?php } ?>
<?php } ?>
<!-- end for loop -->
I'm trying to run the for to get results (notice if(++$i > 4) break;). Everything else works fine, however, the $blogAuthor, only shows for the first item. Author is empty for the last three results. Unsure why?
<?php if ($i == 1) { ?>will result in displaying just one item.