results from query stored in multi-dimensional array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I have a bunch of categories stored in a MySQL db with their corresponding subcategories. Each subcategory has a link to its page (the categories are just headers for the home page and don't link anywhere).

I need to retrieve this data, and then display it in a table three columns across - ie
Jewish Community Torah Israel
subcat1, subcat2, subcat1,subcat2 subcat1, subcat2
subcat3



I thought the easiest way to echo the data would be to store it all in a multi-dimensional array, rather than just looping through the resultset because it made echoing the HTML code MUCH less complicated (i.e. it's easier to know when you get to the end of a row, column, etc because you know in advance the length of that array item).


I've got the following code so far to create the array:

//query here
for ($i=1; $i<=$cat_numrows; $i++) {
   $cat_a = mysql_fetch_assoc($cat_r);
   $main = $cat_a[maincat];
   $sub = $cat_a[subcat];
   $main_array[$main][] = $sub;
}

and then I loop through the array to list categories and subcategories with the following code:

while (list ($key1) = each ($main_array)) { //list each category
echo "$key1";
$length = count($main_array[$key1]); //num of subcategories in this category
$last = $length - 1; //index num of last subcategory
while (list ($key2, $val2) = each ($main_array[$key1])){
echo "$val2"; //list each subcategory
if ($key2 != $last) { //if not last subcat, put comma to separate
echo ", ";
}
}
}


My problem is that I can't figure out how to add the links ($cat_a[link]) to the array - this is just one more dimension than I've ever dealt with before and I'm not sure where it goes.

I think it should look something like
   $main_array[$main][subcat][name] = $sub;
   $main_array[$main][subcat][link] = $link;

but then I'm not sure how to properly loop through it.

Any help would be greatly appreciated, from how to change the table structure if necessary, to the proper code.

My table structure is as follows:

CREATE TABLE categories (
subcat varchar(35) default NULL,
maincat enum('Jewish Community','Torah','Israel','Tourism','Shopping','Jewish Living','Music','Kosher Food','Social','Mazel-Tov Guide','Jewish Art and Judaica','Education & Information','Computers') default NULL,
link varchar(150) default NULL
) TYPE=MyISAM;


This may not be the best structure but I was trying to come up with the best way to store all the information in one table, rather than having a category table and then a separate subcategory table with corresponding links. I thought this would simplify the query.

Sorry to be so long, winded, just wanted to be clear.

Thank so much,

-Lisi

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux