Author Topic: I Need Urgent Help With An Array Problem!  (Read 444 times)

0 Members and 1 Guest are viewing this topic.

Offline FishSwordTopic starter

  • Enthusiast
  • Posts: 59
    • View Profile
I Need Urgent Help With An Array Problem!
« on: March 14, 2010, 04:58:07 PM »
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]
« Last Edit: March 14, 2010, 04:59:57 PM by FishSword »

Offline slurpee

  • Irregular
  • Posts: 42
    • View Profile
Re: I Need Urgent Help With An Array Problem!
« Reply #1 on: March 14, 2010, 05:10:59 PM »
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)),
);

Offline FishSwordTopic starter

  • Enthusiast
  • Posts: 59
    • View Profile
Re: I Need Urgent Help With An Array Problem!
« Reply #2 on: March 14, 2010, 06:13:14 PM »
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 :))
« Last Edit: March 14, 2010, 06:22:45 PM by FishSword »

Offline teamatomic

  • Devotee
  • Posts: 1,197
  • Gender: Male
  • fat skis rule!
    • View Profile
    • myPHPtemplate
Re: I Need Urgent Help With An Array Problem!
« Reply #3 on: March 14, 2010, 07:06:29 PM »
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
« Last Edit: March 14, 2010, 07:08:21 PM by teamatomic »
when in doubt...ski fast
when scared...ski faster
when terrified...point em down

Offline teamatomic

  • Devotee
  • Posts: 1,197
  • Gender: Male
  • fat skis rule!
    • View Profile
    • myPHPtemplate
Re: I Need Urgent Help With An Array Problem!
« Reply #4 on: March 14, 2010, 07:39:06 PM »
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
when in doubt...ski fast
when scared...ski faster
when terrified...point em down

Offline FishSwordTopic starter

  • Enthusiast
  • Posts: 59
    • View Profile
Re: I Need Urgent Help With An Array Problem!
« Reply #5 on: March 15, 2010, 05:58:47 PM »
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)";
« Last Edit: March 15, 2010, 05:59:56 PM by FishSword »

Offline Psycho

  • Guru
  • Freak!
  • *
  • Posts: 7,753
    • View Profile
Re: I Need Urgent Help With An Array Problem!
« Reply #6 on: March 15, 2010, 06:27:58 PM »
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?).
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

Offline FishSwordTopic starter

  • Enthusiast
  • Posts: 59
    • View Profile
Re: I Need Urgent Help With An Array Problem!
« Reply #7 on: March 16, 2010, 03:00:34 AM »
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.......
)