Jump to content

Recommended Posts

Hi,

 

I've been trying to solve this for the past 6 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 (underneath).

 

[attachment deleted by admin]

Here's an array that should work.  Then just iterate through each main category and sub-category and print them out in a table...

 

$categories = array(
  'Main Category One' => array('totalPosts' => 9154,
                               'subCategories' => array('Sub-Category One' => 4512,
                                                        'Sub-Category Two' => 2515,
                                                        'Sub-Category Three' => 3245)),
  'Main Category Two' => array('totalPosts' => 6245,
                               'subCategories' => array('Sub-Category One' => 3521,
                                                        'Sub-Category Two' => 4153)),
  'Main Category Three' => array('totalPosts' => 9233,
                               'subCategories' => array('Sub-Category One' => 4554,
                                                        'Sub-Category Two' => 6444,
                                                        'Sub-Category Three' => 7451,
                                                        'Sub-Category Four' => 1645)),
);

Cheers Slurpee that's a great help, though how extract the information from the arrays, in order to display them in a

table? Normally I would use a for loop, but as the keys are words, I'm pretty sure this is not possible (correct me if I'm wrong). If this is the case, how would you do it?

 

I need to display the array data in the table using the layout shown in the picture (attached to my previous post :))

Not really ideal way for the array but thi9s would output it for display.

 

<?php
$cat = array(
  'Main Category One' => array('Total Posts' => 9154,
                               'Sub Categories' => array('Sub Category One' => 4512,
                                                        'Sub Category Two' => 2515,
                                                        'Sub Category Three' => 3245)),
  'Main Category Two' => array('Total Posts' => 6245,
                               'Sub Categories' => array('Sub Category One' => 3521,
                                                        'Sub Category Two' => 4153)),
  'Main Category Three' => array('Total Posts' => 9233,
                               'Sub Categories' => array('Sub Category One' => 4554,
                                                        'Sub Category Two' => 6444,
                                                        'Sub Category Three' => 7451,
                                                        'Sub Category Four' => 1645)),
);

foreach($cat as $mkey => $mvalue)
{
echo "<b>$mkey - ";
foreach($mvalue as $skey => $svalue)
{
	if(is_array($svalue))
	{
             foreach($svalue as $key => $value)
	 {
              echo "$key Total Posts - $value<br>";
	 }
	}
	else{
         		echo "$skey $svalue</b><br>";
                 }
}
echo '<p>';
}

?>

 

Main Category One - Total Posts 9154

Sub Category One Total Posts - 4512

Sub Category Two Total Posts - 2515

Sub Category Three Total Posts - 3245

 

Main Category Two - Total Posts 6245

Sub Category One Total Posts - 3521

Sub Category Two Total Posts - 4153

 

Main Category Three - Total Posts 9233

Sub Category One Total Posts - 4554

Sub Category Two Total Posts - 6444

Sub Category Three Total Posts - 7451

Sub Category Four Total Posts - 1645

 

 

HTH

Teamatomic

I like this better. Easier to build from a DB.

 

<?php
$cat = array(
'0' => array(
  'Main Category One'  => 9154,
  'Sub Category One' => 4512,
  'Sub Category Two' => 2515,
  'Sub Category Three' => 3245),

'1' => array(
  'Main Category Two' => 6245,
  'Sub Category One' => 3521,
  'Sub Category Two' => 4153),

'2' => array(
  'Main Category Three' => 9233,
  'Sub Category One' => 4554,
  'Sub Category Two' => 6444,
  'Sub Category Three' => 7451,
  'Sub Category Four' => 1645)
);

foreach($cat as $main)
{      $i=1;
	foreach($main as $key => $value)
{
($i ==1)?$line= "<b>$key - $value</b><br>":$line = "$key - $value<br>";
        echo "$line";
 $i++;
}
  echo '<p>';
  $i=1;
}

?>

 

Main Category One - 9154

Sub Category One - 4512

Sub Category Two - 2515

Sub Category Three - 3245

 

Main Category Two - 6245

Sub Category One - 3521

Sub Category Two - 4153

 

Main Category Three - 9233

Sub Category One - 4554

Sub Category Two - 6444

Sub Category Three - 7451

Sub Category Four - 1645

 

HTH

Teamatomic

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)";

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)";

 

Is that how the data is formatted in the current hard-coded array? If so, how do you determine how many posts there are in a sub-category. Also, how are the sub-categories related to the main category - if it is by primary index then each main category could only have one sub-category. Please give exact details about the current format of the hard-coded array and how this data is obtained (from db query?).

Thanks for the reply mjdamato.

 

The following code is how the array stands at the moment. The information will be coming from two separate arrays. I'm guessing you could find out "count()" to find out how many main categories and sub categories there are.

 

The current hard coded array I need to be changed using "$categories['mainCategory'][0].....etc":

 

<?php
$cat = array(
'0' => array(
  'Main Category One'  => 9154,
  'Sub Category One' => 4512,
  'Sub Category Two' => 2515,
  'Sub Category Three' => 3245),

'1' => array(
  'Main Category Two' => 6245,
  'Sub Category One' => 3521,
  'Sub Category Two' => 4153),

'2' => array(
  'Main Category Three' => 9233,
  'Sub Category One' => 4554,
  'Sub Category Two' => 6444,
  'Sub Category Three' => 7451,
  'Sub Category Four' => 1645)
);

foreach($cat as $main)
{
  $i=1;
  foreach($main as $key => $value)
  {
    ($i ==1)?$line= "<b>$key - $value</b><br>":$line = "$key - $value<br>";
    echo "$line";
    $i++;
  }
  echo '<p>';
  $i=1;
}

?>

 

The main category array:

 

Array
(
    [Main Category One] => 8745
    [Main Category Two] => 7622
    [Main Category Three] => 94
    etc.......
)

 

The subcategory array:

 

Array
(
    [sub Category One] => 4512
    [sub Category Two] => 2515
    [sub Category Three] => 3245
    [sub Category One] => 3521
    [sub Category Two] => 4153)
    etc.......
)

 

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.