gkovar Posted August 28, 2007 Share Posted August 28, 2007 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 More sharing options...
gkovar Posted August 28, 2007 Author Share Posted August 28, 2007 In addition... Is there a way to close simplexml_load_file($mySite)? Could part of my problem be that I am trying to open the same file twice? Thanks Link to comment https://forums.phpfreaks.com/topic/67043-logical-error-in-array-processing/#findComment-336772 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.