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")
);
}?>
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"); }?>
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) {
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
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.
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');
$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>";