Jump to content

Logical Error in array processing?


gkovar

Recommended Posts

I have a huge .xml cms file that I'm playing with. While looping through the array I need to count the occurrence of the category. I believe the following code should do that for me, however the output indicates each category has just 1 article. (my test data set has just five entries, all of them listed in the same category). Any help pointing out (what I can only assume is a logic error on my part) is appreciated.

 

<?php

$mySite = "cms.xml" ;

$xml = simplexml_load_file($mySite) or die ("no file loaded") ;

 

echo "<h2>Categories</h2>";

echo "<h3>Title</h3>";

//loop through xml file and display category info

foreach ($xml->article as $article) {

//display categories

$iscat = $article->iscat;

if ($iscat==1){

echo "<div><a href=#>";

echo $article->title;

echo "</a></div>";

 

//trying to count the amount of items per category

$categorymatchcount = 0;

foreach($xml->article as $catcount) {

$catcount_cat = $catcount->cat;

$categorymatch = $article->cat;

if ($categorymatch==$catcount_cat){

$categorymatchcount = $categorymatchcount+1;

}

}

echo $categorymatchcount ." in this cat";

}

//end category display info

?>

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/67043-logical-error-in-array-processing/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.