#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    23
    Rep Power
    0
    Hi, I am trying to develop a link catalog like DMOZ or Yahoo for my own website. However I couldn't figure out how to have an infinite number of subcategories. Here is the code I have now, which only allows my directory to be three layers deep.

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>function getCatsinOptions() {
    $query = "Select * FROM Categories";
    $result = mysql_query($query);

    while($row = mysql_fetch_array($result)) {
    if(empty($row["Parent"])) {
    $myCats .= "<option value="" . $row["CatID"] . "">" . $row["Name"] . "</option>n";
    }
    else {
    $meANDmyParents = getParents($row["CatID"]);
    $myCats .= "<option value="" . $row["CatID"] . "">" . $meANDmyParents . "</option>n";
    }
    }
    return $myCats;
    }

    function getParents($id) {
    $query = "Select Name,Parent FROM Categories WHERE CatID='$id'";
    $result = mysql_query($query);$parentid = mysql_fetch_row($result);

    $query2 = "Select Name,Parent FROM Categories WHERE CatID='$parentid[1]'";
    $result2 = mysql_query($query2);$parentInfo = mysql_fetch_row($result2);

    if(empty($parentInfo[1])) {
    $parentString = $parentInfo[0] . "/" . $parentid[0];
    }
    else {
    $query3 = "Select Name FROM Categories WHERE CatID='$parentInfo[1]'";
    $result3 = mysql_query($query3);$topParent = mysql_fetch_row($result3);

    $parentString = $topParent[0] . "/" . $parentInfo[0] . "/" . $parentid[0];
    }
    return $parentString;
    }
    [/code]

    Don't worry about the output of $myCats, I jsut designed that for drop down menus for my administrators. If anybody understands what I am trying to do or think they can explain it better, please give me a hand.



    ------------------
    Aristotle concluded that those who admit their stupidity are truly wise.
    -
    Gil Hildebrand Jr
    4atcost.com Senior Web Developer
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    London/UK
    Posts
    91
    Rep Power
    15
    Try this, I to am writing a links thing for back-end

    <?
    function select_cats($array,$parent,$level=0,$selected) { # used to build a SELECT DD

    $indent = "---->";
    unset($prefix);
    for ($x=0;$x<$level;$x++) {
    $prefix .= $indent;
    }
    $level++;
    while (list ($catID, $category) = each ($array[$parent])) {
    print "n<option";
    if($catID == $selected){
    print " SELECTED";
    }
    print " value="" . $catID . "">" . $prefix . $category . "</option>";
    if($array[$catID]){
    select_cats($array,$catID,$level,$selected);
    }
    }
    }

    function category_matrix($ereg=true) {

    $sql = "select catID,parentID,category from categories order by category asc";
    $get_categories = do_sql($sql);
    while($loop = mysql_fetch_array($get_categories)){
    if ($ereg) {
    $cat_matrix[$loop["parentID"]][$loop["catID"]] = eregi_replace("_"," ",$loop["category"]);
    } else {
    $cat_matrix[$loop["parentID"]][$loop["catID"]] = $loop["category"];
    }
    }
    return $cat_matrix;
    }

    function get_categories($selected) { # forms SELECT DD for adding new cat
    $matrix = category_matrix();
    select_cats($matrix,0,0,$selected);
    }
    ?>

    just do:

    <select name="whatever">
    <?
    get_categories($catID);
    ?>
    </select>

    after you've made your specific mods, obviously

    Bealers

    ------------------
    http://back-end.org
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    23
    Rep Power
    0
    Bealers, thanks so much for your input. I have to admit, I never thought of using a multi-dimensional array. Good logic!

    The only things that I really had to change were (1) $level should not be set to 0 in the select_cats function statement and (2) $level++; is perl thinking. Try $level=++$level;

    Once again, thanks for the help. It has helped me a great deal.


    ------------------
    Aristotle concluded that those who admit their stupidity are truly wise.
    -
    Gil Hildebrand Jr
    4atcost.com Senior Web Developer
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    London/UK
    Posts
    91
    Rep Power
    15
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>
    $level=++$level;
    [/quote]

    no way! that's more typing =)

    heh

    Bealers

    ------------------
    http://back-end.org

    [This message has been edited by Bealers (edited September 14, 2000).]

Similar Threads

  1. is number a prime number program
    By easy in forum Java Help
    Replies: 2
    Last Post: January 30th, 2004, 03:36 PM
  2. Replies: 23
    Last Post: January 2nd, 2004, 09:28 AM
  3. Replies: 12
    Last Post: December 6th, 2003, 06:31 PM
  4. Replies: 1
    Last Post: November 22nd, 2003, 03:02 PM

IMN logo majestic logo threadwatch logo seochat tools logo