Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2004
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry having problems with loops

    Hi ppl

    i would like to display my categories in my menu , but i got some conditions , for example ;
    MAINCAT1 subcat1a subcat1b subcat1c


    MAINCAT2 subcat2a subcat2b subcat2c

    MAINCAT3 subcat3a subcat3b subcat3c
    ....


    i would like to display thEsE categories like this (when MAINCAT1 link or one of its sub categories link is active )

    MAINCAT1
    subcat1a
    subcat1b
    subcat1c
    MAINCAT2
    MAINCAT3
    MAINCAT4
    ....


    OR (when MAINCAT3 or one of its sub categories is active seemed on the page)
    MAINCAT1
    MAINCAT2
    MAINCAT3
    subcat3a
    subcat3b
    subcat3c
    MAINCAT4

    i have try to organize 2 while loops inside of each other but nothing successfull.

    plesa check the code i wrote at the bottom of the page ...
    also my structure of my database where categories take place is like

    CATEGORY TOPCATEGORY
    MAINCAT1 0
    MAINCAT2 0 (all main cats hae "o" value in topcategory column)
    MAINCAT3 0
    subcat1a MAINCAT1
    subcat1b MAINCAT1
    subcat1c MAINCAT1
    subcat2a MAINCAT2
    subcat2b MAINCAT2
    subcat2c MAINCAT2

    so on ......

    thanks by now for your valuable helps by now.

    PHP Code:
     <? function category_list () {
    global 
    $req_topcategory$kategori$sql_req$req_category$result3$sql3$adult$categoryname1$categorydesc$categoryid$topcategory$conn ;
    // kategoriyi kontorol edelim

    $sql1 "SELECT * FROM category WHERE topcategory = '0'" ;

    print 
    "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
    print 
    "<tr>\n"
    print 
    "<td bgcolor=\"#0076A3\"><div align=\"center\"><strong><font color=\"#FFFFFF\">CATEGORIES</font></strong></div></td>\n";
    print 
    "</tr>\n";
    $result1 mysql_query($sql1$conn) or die(mysql_error());
    while (
    $na1 mysql_fetch_array($result1)) {
    $category $na1['category'];
    $categorydesc $na1['categorydesc'];
    print 
    "<tr>";
    print 
    " <td width=\"100%\"><font size=\"2\" face=\"verdana\"><div align=\"left\"><a href=\"category.php?name=$category\"><b>$categorydesc </b> </a>(<font color=\"red\"> </font>)</div></font></td>"
    print 
    "</td>";
    print 
    "</tr>\n";
    $_GET['name'] = $req_category
    $sql_req "SELECT * FROM category WHERE category = '".$req_category."' ";
    $result_req mysql_query($sql_req$conn) or die(mysql_error());
    $na_req mysql_fetch_array($result_req);

    $req_topcategory $na_req['topcategory']; 
    switch(
    $req_topcategory){
    case 
    '0' ;
    $sql2 "SELECT * FROM category WHERE topcategory = '".$req_category."'" ;
    break;
    default ; 
    $sql2 "SELECT * FROM category WHERE topcategory = '".$req_topcategory."'" ;
    }

    $result2 mysql_query($sql2$conn) or die(mysql_error());

    while (
    $na2 mysql_fetch_array($result2)) {
    $category2 $na2['category'];
    $categorydesc2 $na2['categorydesc'];

    print 
    "<tr>";
    print 
    " <td width=\"100%\"><font size=\"2\" face=\"verdana\"><div align=\"left\"><a href=\"category.php?name=$category2\">$categorydesc2 </a>(<font color=\"red\"> </font>)</div></font></td>"
    print 
    "</td>";
    } }
    print 
    "</table>";

    }



    ?>

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Location
    Scotland
    Posts
    407
    Thanks
    0
    Thanked 0 Times in 0 Posts
    a very quick answer (cos i'm in a rush) so you'll need to fill in the gaps:

    PHP Code:
    <?
    $sql1 
    "SELECT * FROM category WHERE topcategory = '0'";
    $result1 mysql_query($sql1$conn) or die(mysql_error());
    while (
    $na1 mysql_fetch_array($result1))
    {
       
    $category $na1['category'];
       
    $categorydesc $na1['categorydesc'];
       print(
    '<a href="category.php?name="'.$category.'">'.$categorydesc.'</a><br>');
       if(
    $name == $category)
       {
          
    $sql2 "SELECT * FROM category WHERE topcategory = '".$category."'";
          
    $result2 mysql_query($sql2$conn) or die(mysql_error());
          while (
    $na2 mysql_fetch_array($result2))
          {
             
    $subcategory $na2['subcategory'];
             
    $subcategorydesc $na2['subcategorydesc'];
             print(
    '<a href="category.php?name="'.$subcategory.'">'.$subcategorydesc.'</a><br>');
          }
       }
    }
    ?>
    not checked if it works yet though
    My body's a temple... and like those ancient Greek ones it's a ruin
    sweenster.co.uk

  • #3
    New Coder
    Join Date
    Jan 2004
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Many thanks sweenster!!!

    Your code gave me the right logic to sort it out
    FOr ppl's future reference i am writing the working BELOW working code for my problem , of cource it can be shortened and written in other way
    By the way , your code was showing the categories with their sub categories only when $name(requested categori) is a main categori ... now it is showing respectless if the requested category is main one or not )
    Thanks again

    PHP Code:
    <? function category_list () {
            print 
    "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
               print 
    "<tr>\n"
               print 
    "<td bgcolor=\"#0076A3\"><div align=\"center\"><strong><font color=\"#FFFFFF\">CATEGORIES</font></strong></div></td>\n";
            print 
    "</tr>\n";
         global 
    $topcategorye$subcategory$subcategorydesc$name$req_topcategory$kategori$sql_req$kategori$result3$sql3$adult$categoryname1$categorydesc$categoryid$topcategory$conn ;
    $sqle "SELECT * FROM category WHERE category = '".$name."'";
    $resulte mysql_query($sqle$conn) or die(mysql_error());    
    $nae mysql_fetch_array($resulte);        
    $topcategorye $nae['topcategory'];

    switch (
    $topcategorye) {
    case 
    '0' ;
    $sql1 "SELECT * FROM category WHERE topcategory = '0'";
    $result1 mysql_query($sql1$conn) or die(mysql_error());
    while (
    $na1 mysql_fetch_array($result1))
    {
       
    $category $na1['category'];
       
    $topcategory $na1['topcategory'];
       
    $categorydesc $na1['categorydesc'];
       print 
    "<tr>\n"
       print(
    "<td><a href=\"category.php?name=".$category."\"><b>".$categorydesc."</b></a><br></td></tr>");
       if(
    $name == $category)
       {
          
    $sql2 "SELECT * FROM category WHERE topcategory = '".$category."'";
          
    $result2 mysql_query($sql2$conn) or die(mysql_error());
          while (
    $na2 mysql_fetch_array($result2))
          {
             
    $subcategory $na2['category'];
             
    $subcategorydesc $na2['categorydesc'];
             print 
    "<tr>\n";
             print(
    "<td><a href=\"category.php?name=".$subcategory."\"> > ".$subcategorydesc."</a><br></td></tr>");
          }
       }
        
    }
    break;
    default;
    $sql1 "SELECT * FROM category WHERE topcategory = '0' ";
    $result1 mysql_query($sql1$conn) or die(mysql_error());
    while (
    $na1 mysql_fetch_array($result1))
    {
       
    $category $na1['category'];
       
    $topcategory $na1['topcategory'];
       
    $categorydesc $na1['categorydesc'];
       print 
    "<tr>\n"
       print(
    "<td><a href=\"category.php?name=".$category."\"><b>".$categorydesc."</b></a><br></td></tr>");
        if(
    $category == $topcategorye)
       {
          
    $sql2 "SELECT * FROM category WHERE topcategory = '".$topcategorye."'";
          
    $result2 mysql_query($sql2$conn) or die(mysql_error());
          while (
    $na2 mysql_fetch_array($result2))
          {
             
    $subcategory $na2['category'];
             
    $subcategorydesc $na2['categorydesc'];
               print 
    "<tr>\n";
             print(
    "<td><a href=\"category.php?name=".$subcategory."\"> > ".$subcategorydesc."</a><br></td></tr>");
          }
       }
    }    
    }
    print 
    "</table>\n";
    }
    ?>
    Last edited by taydag; 01-27-2004 at 10:47 PM.

  • #4
    New to the CF scene
    Join Date
    Jan 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    levels n ?

    Hi,

    Your function resolve tree structure too?

    - categories 1
    -subcategories 11
    -subcategories 111
    -subcategories 1111
    -subcategories 1112
    -subcategories 1113
    -subcategories 112
    .......

    I want to say if the function create tree structure with infinit sublevels ?

    Marian

  • #5
    New Coder
    Join Date
    Jan 2004
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi...
    it is currently good for ONE MAIN and ITS SUBMENUS.
    If you want to display more submenu levels under main menus, i think you sould change the databse structure and add 2 new loops under the main loop

    If i could write , i would write the code and explain the database structure in details , i just dont wanna mess things since iam not that good in PHP I hope somebody who is better can write the revised code in this thread
    Last edited by taydag; 01-30-2004 at 08:55 PM.

  • #6
    New to the CF scene
    Join Date
    Jan 2004
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    thank's taydag

    thank's for your sugestion.

    I have a table "folders" with fields:
    id // id for directors
    id_sub // id for subdirectors
    name // name directors

    And I tryed this script:

    function build_category_tree_sitemap1(&$output, $parent, $indent="") {




    $qid = db_query("SELECT id, name FROM folders WHERE parent_id = $parent");

    while ($cat = db_fetch_object($qid)) {


    $output .=$indent."<br><a href=http://magda/backoffice/items/addkoppeling.php?id1=".$cat->id.">".$cat->name."</a>".$cat->id."<br>";
    if ($cat->id !== $parent) {
    build_category_tree_sitemap1($output, $cat->id, $indent."<br>&nbsp;&nbsp;");
    }
    }

    }

    $qid0 = db_query("SELECT id, name FROM folders WHERE parent_id = 0");
    while(list($id,$name)=mysql_fetch_row($qid0)){
    echo"<a href=http://magda/backoffice/items/addkoppeling.php?parent=$id>$name</a><br>";

    build_category_tree_sitemap1($category_options,$id1);



    echo"$category_options";}
    ?>

    but i thinks i have mistakes somewhere because the tree dont display my root ...


  •  

    Posting Permissions

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