0

I need to create a HTML sitemap similar to apple.com/sitemap with Parent Category and sub category under it. I have created two tables as such: Primary Category:

ID | TITLE | URL

Sub Category as:

ID|Parent_Category_ID|TITLE|URL ...

How do i retrieve it so that it shows something like:

Parent_Category

  • Sub_Cat
  • Sub_Cat

Edit: Followed some online suggestions, so now there is one table:

ID      Parent       Name        URL
1          0         Parent1     URL1
2          0         Parent2     URL2  
3          1         Sub1        URL3
4          0         Parent3     URL4
5          2         Sub2        URL5

Here is the code using which I'm able to generate an array of categories with sub categories

<?php

   include('config.php');   



echo '<pre>';

$categories = Sitemap::getTopCategories();
print_r($categories);

echo '</pre>';

class Sitemap
{ 


public static function getTopCategories()
{
    return self::getCategories('parent = 0');
}

public static function getCategories($where = '')
{
    if ($where) $where = " WHERE $where";
    $result = mysql_query("SELECT * FROM testing $where");

    $categories = array();

    while ($category = mysql_fetch_array($result)){
    $my_id = $category['id'];
    $category['children'] = Sitemap::getCategories("parent = $my_id");
            $categories[] = $category;
        }

    mysql_free_result($result);
    return $categories;
  }
 }
 ?>

Now what I have to follow so that it displays in a proper way as of apple.com/sitemap. I' ready with the CSS for proper placement however not able to display it.

Please Help!!

1 Answers1

0

Here is the function I used to get the array in a proper format..

function outputCategories($categories, $startingLevel = 0)  
{

    foreach ($categories as $key => $category)
    {
        if (count($category['children']) > 0)
        {
            echo '<li><a title="'.$category['name'].'" href="'.$category['url'].'">'.$category['name'].'</a><ul>';
            outputCategories($category['children'], $startingLevel+1);
            echo "</li>";
        }
        else
        {
            echo '<li><a title="'.$category['name'].'" href="'.$category['url'].'">'.$category['name'].'</a></li>';
        }
    }
    echo "</ul>";
    return self;
}

For more info refer this post: Link

Community
  • 1
  • 1