I Need Urgent Help With An Array Problem!

Discussion in 'PHP' started by FishSword, Mar 14, 2010.

  1. #1
    Hi,

    I've been trying to solve this for the past 3 hours, but can't get my head around it.

    Basically, I want to create an array of categories and sub-categories. I need to store the category name and the total number of posts contained within each categories.

    I want to try and group the category and total number of posts (and if possible the main and sub-categories) together inside the array, but can't figure out the best way to do it.

    The main category name and total posts then needs to be displayed inside a table, along with the sub-categories name and total posts.

    View attachment 36740
     
    Last edited: Mar 14, 2010
    FishSword, Mar 14, 2010 IP
  2. shockworks

    shockworks Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    send your source code, I try to help you
     
    shockworks, Mar 14, 2010 IP
  3. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    Cheers ShockWorks, much appreciated. :)

    My code is below.

    What would be the best way to add the data to the array, so it can easily be displayed in the table. Also, how do I display the information like in the picture attached to my previous post? - Also, is it possible to group the data somehow?:

    Many Thanks,

    <?php          
    $categories['mainCategory'][0]= "Main Category One";
    $categories['mainCategory'][1]= "Main Category Two";
    $categories['mainCategory'][2]= "Main Category Three";
    
    $categories['mainCategoryPosts'][0]= "9154";
    $categories['mainCategoryPosts'][1]= "6245";
    $categories['mainCategoryPosts'][2]= "9233";
    
    $categories['subCategory'][0]= "Sub-Category One (for Main Category One)";
    $categories['subCategory'][1]= "Sub-Category Two (for Main Category One)";
    $categories['subCategory'][2]= "Sub-Category Three (for Main Category One)";
    $categories['subCategory'][3]= "Sub-Category One (for Main Category Two)";
    $categories['subCategory'][4]= "Sub-Category Two (for Main Category Two)";
    $categories['subCategory'][5]= "Sub-Category One (for Main Category Three)";
    $categories['subCategory'][6]= "Sub-Category Two (for Main Category Three)";
    $categories['subCategory'][7]= "Sub-Category Three (for Main Category Three)";
    $categories['subCategory'][8]= "Sub-Category Four (for Main Category Three)";
    
    $categories['subCategoryPosts'][0]= "4512";
    $categories['subCategoryPosts'][1]= "2515";
    $categories['subCategoryPosts'][2]= "3245";
    $categories['subCategoryPosts'][3]= "3521";
    $categories['subCategoryPosts'][4]= "4158";
    $categories['subCategoryPosts'][5]= "4554";
    $categories['subCategoryPosts'][6]= "6444";
    $categories['subCategoryPosts'][7]= "7451";
    $categories['subCategoryPosts'][8]= "1645";
    
    echo "<table border='1'>";
    echo   "<tr>";
    echo     "<td>Category Name</td>";
    echo     "<td>Total Posts</td>";
    echo   "</tr>";
    echo "</table>";
    
    echo '<pre>';
    print_r($categories);
    echo  '</pre>';
    ?>
    PHP:
     
    FishSword, Mar 14, 2010 IP
  4. dazst

    dazst Active Member

    Messages:
    115
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    78
    #4
    Here's the idea, but dont try the code! If you want real code, PM me:

    foreach($posts as $key => $values)
    {
    $categories[$Main_Category][$Sub_Category]++;
    }
    $Total_Main_Category = count($categories[$Main_Category]);
    $Total_Sub_Category = count($categories[$Main_Category][$Sub_Category]);
     
    dazst, Mar 14, 2010 IP
  5. shockworks

    shockworks Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    At first you could reorganizate your arrays like:
    And then you can build recursive function, that makes your table.

    PS: This solution is good, because you can build unlimited level tree in the future.
     
    shockworks, Mar 15, 2010 IP
  6. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    Currently the array is hard coded, how do I add the Main Categories and Sub-Categories to the array, from two separate arrays?
    I've worked out that I need to do something like the following, and put the code in a loop in order to populate the categories array.

    Basically, my question is how do I create the categories array shown in your previous post using the below method?:

    $categories['mainCategory'][0]= "Main Category One";
    $categories['mainCategoryPosts'][0]= "9154";
    $categories['subCategory'][0]= "Sub-Category One (for Main Category One)";
    PHP:
     
    FishSword, Mar 15, 2010 IP
  7. shockworks

    shockworks Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    $categories['Main Category One']['posts'] = 9154;
    $categories['Main Category One']['childs']['Sub-Category One (for Main Category One)']['posts'] = 4512;
    $categories['Main Category One']['childs']['Sub-Category One (for Main Category One)']['childs'] = NULL;
     
    shockworks, Mar 24, 2010 IP