SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb how to build a directory

    Good evening

    I want to build a directory.

    I have a table:


    CREATE TABLE IF NOT EXISTS `business_category` (
    `business_category_id` int(11) NOT NULL auto_increment,
    `business_category_name` varchar(255) NOT NULL,
    `business_category_rewrite_name` varchar(255) NOT NULL default 'default',
    `parent_id` int(11) NOT NULL,
    PRIMARY KEY (`business_category_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=25 ;


    some categories have other categories as parents and possibly their own sub categories.

    All categories (Top level)
    - Travel (1st level)
    - Media (1st level)
    - Newspapers (2nd level) (no sub categories
    - Radio Staition (2nd Level

    If someone has selected a category I want to show the sub categories of that category

    and show a bread crumb trail of the categories
    >All categories > Media > Newspapers

    when a category has no sub categories I want to show the website links in that category

    How would you go about achieving this?

  2. #2
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    13 Post(s)
    Tagged
    0 Thread(s)
    Two seperate items there,

    To select whether you want to show the sub categories, or the items you will need to do a query for categories where the current category id is the parent id.
    Code SQL:
    SELECT * FROM business_category WHERE parent_id = $current_category_id

    If this returns no rows, then you know that there are no children categories and can proceed to show all the items, otherwise show the sub categories that the sql returns.

    As for the building of a bread crumb, you'll need to create a function that completes a while loop until it finds a category with no parent id. Please note I have no idea how you are accessing your database so I've put in some generic database calls, but it won't be very different from how you access yours.

    e.g.

    PHP Code:
    function getCrumbArray($categoryID){
     
    //create empty array
     
    $crumbs = array();
     while(
    $categoryID 0){
      
    $sql "SELECT parent_id, business_category_name FROM business_category WHERE business_category_id = $categoryID";
      if(
    $result $db->query($sql)){
       while(
    $row $result->fetch()){
        
    $crumbs[$categoryID] = $row->business_category_name;
        
    $categoryID $row->parentID;
       }
      }
     }
     return 
    $crumbs;

    After you call that function, you have an associative array with key value pairs for the id and name. You can loop through this however you like.

    PHP Code:
    $crumbs getCrumbArray(22);// Category 22 - replace with a real one
    foreach($crumbs as $category_id => $category_name){
     echo 
    $category_name " ($category_id) > ";


  3. #3
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Fantastic, I'll have a go at putting that together shortly.

  4. #4
    SitePoint Evangelist tangledman's Avatar
    Join Date
    Sep 2005
    Location
    Puerto de Mazarron, Murcia, Spain
    Posts
    425
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    I have got the directory part working, but not the crumbs

    http://www.spanishpropertymagazine.c...ategories.html

    PHP Code:
    function getCrumbArray($directory_category_id){
     
    //create empty array
     
    $crumbs = array();
     while(
    $directory_category_id 1){
      
    $sql "SELECT parent_id, directory_category_name FROM directory_category WHERE directory_category_id = '$directory_category_id'";

      if(
    $result $db->query($sql)){
        while(
    $row $result->fetch()){
        
    $crumbs[$directory_category_id] = $row->directory_category_name;
        
    $directory_category_id $row->parent_id;
       }
      }
     }
     return 
    $crumbs;



    $crumbs getCrumbArray($directory_category_id);

    foreach(
    $crumbs as $directory_category_id => $directory_category_category_name){

     echo 
    $directory_category_name " ($category_id) > ";



  5. #5
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    13 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $crumbs getCrumbArray($directory_category_id);

    foreach(
    $crumbs as $directory_category_id => $directory_category_category_name){

     echo 
    $directory_category_category_name " ($directory_category_id) > ";


    Try that, you weren't using the right variables (close, but no cigar).

    Cheers


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •