Page 1 of 1

Populating Array....

Posted: Thu Nov 27, 2008 10:17 am
by bunker
Hi all,

First post, Hope I can explain myself and also recive a quick response.

My website has categories and subcategories, for example News is Cat1 Subcat0, Sport-News is Cat1 Subcat1, Travel-News is Cat 1 Subcat2, Restaurants is Cat2 Subcat0, Italian-Restaurants is Cat2 Subcat1, etc...

In the Mysql DB there is a field called url_modrewrite, that I want to fetch. Ive figuered out how to fetch and populate an array only looking at the Category, but not both the Category and the Subcategory...

$sql = mysql_query("SELECT url_modrewrite, cat, subcat FROM content WHERE port = '$port' AND lang = '$lang' AND ubic_menu LIKE '%sup%' ");
$data = array();
//populate array with modrewrites
while($d = mysql_fetch_array($sql)){
$data[$d['cat']] = $d['url_modrewrite'];
}

After I want to printf the value of url_modrewrite: print_r($data[1]);

How could I change the code so I also control the Subcategories!¿

Muchas muchas Gracias x adelantado ( Something like thx in advanced )

Re: Populating Array....

Posted: Thu Nov 27, 2008 12:57 pm
by draco
I'm not exactly sure what you mean. But I think this is what you want:

Code: Select all

while ( $d = mysql_fetch_array($sql) )
{
    $cat = $d['cat'];
    $subcat = $d['subcat'];
    $data[$cat][$subcat] = $d['url_modrewrite'];
}
 
// Now you can see the url_modrewrite for each subcategory...
echo $data['Cat1']['SubCat1']; //would display the url_modrewrite for Sports-News

Re: Populating Array....

Posted: Fri Nov 28, 2008 5:56 pm
by bunker
Yes!

And WOW... Youve made me discover a whole new world!!!

Thx a lot