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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.