Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-20-2010, 08:31 PM   PM User | #1
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
Exclamation Multi dimentional array help please!

Hi All,

i have 5 sets of info a category and its description i am trying to put them into 1 array and display the data using php

here is my code
PHP Code:
    <?php
    
if($_SESSION['page'] == "entertainment")
    {
        
$category_list = array( 
                         
"Category" => array("DVD","CD","Video Games","Blu-Ray","Books"),
                            
"Sub" => array("The latest blockbuster DVDs.""Chart hit CDs from the hottest artists.","Top video games from all the major platforms including xbox 360, ps3, nintendo wii","The latest blockbuster Blu-Ray DVDs.","Bestselling hardback, paperback titles from the best authors")
                         );
    }
?>

    <div id="category_left">
    <?php
    $x
=0;
    foreach(
$category_list as $category){
    echo 
$category["Category"][$x];
    echo 
$category["Category"][$x];
    
$x++;
    }
?>
    </div>
any ideas?
thanks
LJackson is offline   Reply With Quote
Old 01-20-2010, 08:43 PM   PM User | #2
hinch
Regular Coder

 
hinch's Avatar
 
Join Date: Sep 2005
Location: UK
Posts: 921
Thanks: 25
Thanked 79 Times in 79 Posts
hinch is on a distinguished road
why not do it as simply

Category,Sub
Category,Sub
Category,Sub


instead of what your doing atm which is

(cat,cat,cat,cat,cat),(sub,sub,sub,sub,sub)

so it'd be something like (bit rusty here only just getting back into things after xmas break)

PHP Code:
$category_list = array();
$category_list[] = array("DVD","The latest blockbuster DVDs.");
$category_list[] = array("CD","blah");

    
$x=0;
    foreach(
$category_list as $category){
    echo 
$category["Category"][$x];
    echo 
$category["Category"][$x+1];
    
$x++;
    } 
(code above is actually wrong I'm just too tired and rusty to work it out ) should give you a pointer for structuring the array better though
__________________
A programmer is just a tool which converts caffeine into code

My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
My hobby: http://www.angel-computers.co.uk
My life: http://www.furious-angels.com
hinch is offline   Reply With Quote
Old 01-20-2010, 09:11 PM   PM User | #3
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
thanks mate solved it

PHP Code:
    if($_SESSION['page'] == "entertainment")
    {
    $category_list = array();
    $category_list[] = array("category"=>"DVD","desc"=>"The latest blockbuster DVDs.");
    $category_list[] = array("category"=>"CD","desc"=>"Chart hit CDs from the hottest artists.");
    $category_list[] = array("category"=>"Video Games","desc"=>"Top video games from all the major platforms including xbox 360, ps3, nintendo wii");
    $category_list[] = array("category"=>"Blu-Ray","desc"=>"The latest blockbuster Blu-Ray DVDs.");
    $category_list[] = array("category"=>"Books","desc"=>"Bestselling hardback, paperback titles from the best authors");
    }?>

    <div id="category_left">
    <?php
    $x
=0;
    foreach(
$category_list as $category){
    echo 
$category["category"];
    echo 
"<br />";
    echo 
$category["desc"];
    echo 
"<br />";
    
$x++;
    }
cheers
LJackson is offline   Reply With Quote
Old 01-20-2010, 09:17 PM   PM User | #4
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
I was just about to post my solution when I see you have solved it. Since mine is slightly different I will post it any ways.

PHP Code:
<?php

$category_list 
= array('DVD' => 'The latest blockbuster DVDs.',
'CD' => 'Chart hit CDs from the hottest artists.',
'Video Games' => 'Top video games from all the major platforms including xbox 360, ps3, nintendo wii',
'Blu-Ray' => 'The latest blockbuster Blu-Ray DVDs.',
'Books' => 'Bestselling hardback, paperback titles from the best authors');

$counter 1;

foreach (
$category_list as $category => $sub_category) {

echo 
"$counter ";
echo 
"<b>$category</b><br>";
echo 
"$sub_category<br><hr>";
$counter++;

}

------------
__________________
Leonard Whistler
Len Whistler is offline   Reply With Quote
Old 01-20-2010, 09:34 PM   PM User | #5
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
Hi Len,

thanks for your reply, its nice to see another solution will try and remomber that looks much neater than mine

cheers
Luke
LJackson is offline   Reply With Quote
Old 01-20-2010, 09:46 PM   PM User | #6
hinch
Regular Coder

 
hinch's Avatar
 
Join Date: Sep 2005
Location: UK
Posts: 921
Thanks: 25
Thanked 79 Times in 79 Posts
hinch is on a distinguished road
I thought about lens way its basically making the key the name of your category then you just need a single array to store your information rather than an array of arrays as i suggested
__________________
A programmer is just a tool which converts caffeine into code

My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
My hobby: http://www.angel-computers.co.uk
My life: http://www.furious-angels.com
hinch is offline   Reply With Quote
Old 01-24-2010, 02:51 PM   PM User | #7
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
decided to go the way you guys suggested, it works nicely

what would i do if i wanted to add more data to the array for example for each of the categorys how would i add a new piece of data e.g

PHP Code:
$category_list = array('DVD' => 'The latest blockbuster DVDs.' => 'More data here',
'CD' => 'Chart hit CDs from the hottest artists.'=> 'More data here',
'Video Games' => 'Top video games from all the major platforms including xbox 360, ps3, nintendo wii'=> 'More data here',
'Blu-Ray' => 'The latest blockbuster Blu-Ray DVDs.'=> 'More data here',
'Books' => 'Bestselling hardback, paperback titles from the best authors'=> 'More data here'); 
then do this
PHP Code:
    foreach ($category_list as $category => $sub_category => $test
for example. i tried the above but could not get it to work it breaks my page.

any help would be appreciated
thanks
Luke
LJackson is offline   Reply With Quote
Old 01-25-2010, 01:44 PM   PM User | #8
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
bump...
LJackson is offline   Reply With Quote
Old 01-25-2010, 03:51 PM   PM User | #9
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
http://www.webcheatsheet.com/php/mul...nal_arrays.php



---------------
__________________
Leonard Whistler
Len Whistler is offline   Reply With Quote
Old 01-25-2010, 04:17 PM   PM User | #10
LJackson
Senior Coder

 
Join Date: Jun 2008
Location: Cornwall
Posts: 1,973
Thanks: 289
Thanked 12 Times in 12 Posts
LJackson is on a distinguished road
hello mate,

i've seen that page but because the way you showed me before is different to those on there i thought i'd continue using your solution

or are they different because there are more items in the array?

cheers
Luke
LJackson is offline   Reply With Quote
Old 01-25-2010, 11:50 PM   PM User | #11
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
Another solution. This is easy to read and produces nice output.


PHP Code:
<?php

//array with each string a CSV (=>) seperated value.
$category_list = array('DVD=>The latest blockbuster DVDs.=>$12.22 - $34.66',
'CD=>Chart hit CDs from the hottest artists.=>$19.99 - $29.99',
'Video Games=>Top video games from all the major platforms including xbox 360, ps3, nintendo wii=>$9.99 - $23.88',
'Blu-Ray=>The latest blockbuster Blu-Ray DVDs.=>$1.55 - $12.99',
'Books=>Bestselling hardback, paperback titles from the best authors=>$0.88 - $5.00');

echo 
"<table>";
echo 
"<th>#</th><th>Product</th><th>Description</th><th>Price Range</th>";

$counter 1;
foreach (
$category_list as $category) {

// string to array, in this case a 3 element array.
$data explode('=>',$category);
echo 
"<tr><td>$counter</td><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td></tr>";
$counter++;

echo 
"</table>";


---------
__________________
Leonard Whistler
Len Whistler is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:57 AM.


Advertisement
Log in to turn off these ads.