Populating Array....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bunker
Forum Newbie
Posts: 2
Joined: Thu Nov 27, 2008 10:16 am

Populating Array....

Post 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 )
draco
Forum Newbie
Posts: 7
Joined: Wed Nov 26, 2008 11:45 pm

Re: Populating Array....

Post 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
bunker
Forum Newbie
Posts: 2
Joined: Thu Nov 27, 2008 10:16 am

Re: Populating Array....

Post by bunker »

Yes!

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

Thx a lot
Post Reply