Fetch problem again. Can't understand the function

I have drop down menu.
[php]

  • menu
  • cars
    • Some Text Here
[/php]

The problem is that i know how to build structure in php that fetch and shows only the category.
The problem is how to create php function that automaticly build UL LI in the ul li when the category have some sub categories and not always.
I try many things but i really don’t amagine how to do this. hope you can help.
THANKS!

Hi red wolf,

A lot depends on how your data is structured, but take a look at this example:[php]$menu = array(‘Cars’=>array(‘Mercedes Benz’,‘Lexus’,‘Porsche’),‘Watches’=>array(‘Rolex’,‘Ball’),‘Toasters’=>array());

echo ‘

    ’;

    foreach($menu as $cat=>$subcats)
    {
    echo “

  • $cat
  • ”;
  if(count($subcats) > 0)
    {
        echo '<ul><li>';
        echo implode('</li><li>',$subcats);
        echo '</li></ul>';
    }

}

echo ‘’;[/php]
This will produce the following html:[code]

  • Cars
    • Mercedes Benz
    • Lexus
    • Porsche
  • Watches
    • Rolex
    • Ball
  • Toasters
[/code](I inserted line breaks above to make it easier to view)

This assumes you have an array of categories, some of which have an array of subcategories. It first generates the main category list item and then checks to see if there are subcategories and outputs them if they exist. Obviously, you would need to adjust this to fit your actual data structure.

You could easily accomplish the same thing by substituting a foreach or other similar loop, and might need to add a reiterating loop if your subcategories have subcategories, etc.

Let me know if you have any trouble implementing something like this with your actual data.

Thanks you!
But i have more question.
I need to put categories from mysql to array.
And more problem. when i try fetch this its look like
cars => private
cats => sport

I need it like
cats: private
sport.

Without dubbled category.

Thanks if you can help.

If I understand correctly, you are using a mysql query to return your list of categories and subcategories. This query is returning $results in the form of category=>subcategory.

In this case, I would do something like the following:[php]while($results = mysql_fetch_array($result, MYSQL_ASSOC))
{
$row[$results[‘category’]][] = $results[‘subcategory’];
}[/php]

If your query returned “cars”=>“private” and “cars”=>“sport”; then this would create the following array:
“cars”=>array(“private”,“sport”)

Sponsor our Newsletter | Privacy Policy | Terms of Service