LJackson 01-20-2010, 08:31 PM 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
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
hinch 01-20-2010, 08:43 PM 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)
$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
LJackson 01-20-2010, 09:11 PM thanks mate solved it :)
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
Len Whistler 01-20-2010, 09:17 PM 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
$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++;
}
------------
LJackson 01-20-2010, 09:34 PM Hi Len,
thanks for your reply, its nice to see another solution will try and remomber that looks much neater than mine :)
cheers
Luke
hinch 01-20-2010, 09:46 PM 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
LJackson 01-24-2010, 02:51 PM 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
$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
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 01-25-2010, 01:44 PM bump...
Len Whistler 01-25-2010, 03:51 PM http://www.webcheatsheet.com/php/multidimensional_arrays.php
---------------
LJackson 01-25-2010, 04:17 PM 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
Len Whistler 01-25-2010, 11:50 PM Another solution. This is easy to read and produces nice output.
<?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>";
---------
|
|