A categories issues???

Discussion in 'PHP' started by asgsoft, Jan 18, 2008.

  1. #1
    Hi

    All day today I've been trying to do an admin panel for creating categories for the website.

    I would like to create this effect using php and a front-end form.

    
    Cat 1
    - Sub 1
    - Sub 2
    - Sub 3
    - Sub 4
    - Sub 5
    - Sub 6
    - Sub 7
    - Sub 8
    
    Cat 2
    - Sub 1
    - Sub 2
    
    Cat 3
    - Sub 1
    - Sub 2
    - Sub 3
    
    PHP:
    So how can I go about it?

    It's been a problem which has bothered me for a couple of hours.

    Thanks
     
    asgsoft, Jan 18, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Use multi-dimensional arrays:

    
    <?php
    $tree = array(
        'Cat 1' => array('Sub 1' => 'file.php', 'Sub 2' => 'file2.php'),
        'Cat 2' => array('Sub 1' => 'file3.php', 'Sub 2' => 'file4.php')
    );
    foreach ($tree as $sub => $subtree){
        echo "{$sub}<br>";
        foreach ($subtree as $title => $link){
            echo "- $title [$link]<br>";
        }
        echo "<br>";
    }
    ?>
    
    PHP:
    Should give something like:

    
    Cat 1
    - Sub1 [file1.php]
    - Sub2 [file2.php]
    
    Cat 2
    - Sub1 [file3.php]
    - Sub2 [file4.php]
    
    PHP:
    Hope this helps,

    Jay

    Note: This code is untested and is provided as a theory / guideline only.
     
    jayshah, Jan 18, 2008 IP
  3. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #3
    how could I relate this using MySQL?
     
    asgsoft, Jan 18, 2008 IP
  4. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Ah! I thought you didn't want MySQL

    Just use two tables

    categories
    subs

    and do

    SELECT * FROM catagories

    then for each category

    SELECT * FROM subs WHERE catid='x'

    Jay
     
    jayshah, Jan 19, 2008 IP
  5. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #5
    OK if I do that how can i put them in order

    so cat 1 would have its sub cats under it?

    and also make it loop for all the values of cat
     
    asgsoft, Jan 19, 2008 IP
  6. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Make 1 table called Categories

    Fields for Categories:
    -ID (int) (autoincrement) (primary key)
    -Name (text)
    -ParentID (int) (default: 0) <-- 0 means top-level category (is not a subcategory)

    
    
    $sql = "SELECT * FROM Categories WHERE ParentID = 0";
    $res = mysql_query($sql);
    while ($row = mysql_fetch_assoc($res))
    {
         echo $row["Name"];
    
         $sql_sub = "SELECT * FROM Categories WHERE ParentID = " . $row["ID"];
         $res_sub = mysql_query($sql_sub);
         while ($row_sub = mysql_fetch_assoc($res_sub))
         {
              echo "<br/>&nbsp;&nbsp;&nbsp;-" . $row_sub["Name"];
         }
         echo "<br/>";
    }
    
    
    Code (markup):
    This is very basic, but you could do up a recursive function to have subcategories of subcategories, down as many levels as you want. What I wrote above will give you the top 2 levels (Top-level Categories and their immediate subcategories.
     
    james_r, Jan 19, 2008 IP
    asgsoft likes this.
  7. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #7
    Hey

    Thank you very much

    That works fine.
     
    asgsoft, Jan 20, 2008 IP
  8. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    did you create the table exactly as described and enter your categories? For subcategories, the ParentID is the ID of the category you want it to be a subcategory of. Try this:

    
    $sql = "SELECT * FROM Categories WHERE ParentID = 0";
    $res = mysql_query($sql) or die($sql . "<br><br>" . mysql_error());
    while ($row = mysql_fetch_assoc($res))
    {
         echo $row["Name"];
    
         $sql_sub = "SELECT * FROM Categories WHERE ParentID = " . $row["ID"];
         $res_sub = mysql_query($sql_sub) or die($sql_sub . "<br><br>" . mysql_error());
         while ($row_sub = mysql_fetch_assoc($res_sub))
         {
              echo "<br/>&nbsp;&nbsp;&nbsp;-" . $row_sub["Name"];
         }
         echo "<br/>";
    }
    
    Code (markup):
    and post the message you get back..
     
    james_r, Jan 20, 2008 IP
  9. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #9
    That issue has been fixed now

    I am trying to make this work with htaccess file now.

    So I created a file which adds the categories.

    The bit I am stuck with right now is how do I get it to write the fillowing:

    cat1/subcat-name-1/ cat.php?id=5
     
    asgsoft, Jan 20, 2008 IP
  10. james_r

    james_r Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    hey there, sorry, I'm not sure exactly what you mean. Can you explain what you're trying to do in a bit more detail?
     
    james_r, Jan 20, 2008 IP
  11. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #11
    I am trying to use .htaccess to make the URLs SEO friendly.

    So if I have a page that displays the content of category with ID 5 then rather than using show.php?id=5 instead make the URL like: cat5_name/

    and the same with sub-cats. If it has ID 7 and is a sub cat of cat3, then go to cat3_name/subcat_name/

    Is that any clearer?
     
    asgsoft, Jan 20, 2008 IP
  12. asgsoft

    asgsoft Well-Known Member

    Messages:
    1,737
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    160
    #12
    Have a look at : http://www.thinkrugby.net/nav1.php

    I am getting this error:
    Any Idea why?
     
    asgsoft, Jan 21, 2008 IP