Listing categories from MySQL in dropdown menu

Discussion in 'PHP' started by Exa, Sep 25, 2008.

  1. #1
    Hey :p

    Having a problem with PHP/MySQL.

    My MySQL database table:

    The category hierarchy will be as follows:
    • cat1
      • sub3
    • cat2
      • sub1
        • sub4
      • sub2
    • cat3
    --

    I want to show all the categories in a dropdown menu in the following way:

    How should I go about doing this, thanks.

    --

    Sorry if this sounds confusing, and thanks in advance to any help :D
     
    Exa, Sep 25, 2008 IP
  2. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Use this as a start. just wrote for you. need some edits tho.
    edit $the_table before start. and edit the form to do what u want to do eg: when user click on it

    
    // after connected to sql
    
    print '<select name="">';
    $the_table = ''; // the table name of the category
    
    $get_parent = mysql_query("SELECT * FROM $the_table WHERE parentid=0");
    if (mysql_num_rows($get_parent)>0) {
    
        while ($parent = mysql_fetch_assoc($get_parent)) {
        print '<option value="">'.$parent['name'].'</option>';
            
            $get_child = mysql_query("SELECT * FROM $the_table WHERE parentid={$parent['id']}");
            if (mysql_num_rows($get_child)>0) {
                while ($child = mysql_fetch_assoc($get_child)) {
                  print '<option value="">&nbsp;&nbsp;|__ '.$child['name'].'</option>';
                              $get_grandson = mysql_query("SELECT * FROM $the_table WHERE parentid={$child['id']}");
                              if (mysql_num_rows($get_grandson)>0) {
                              while ($grandson = mysql_fetch_assoc($get_grandson)) {
                              print '<option value="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|__ '.$grandson['name'].'</option>';
                      
                              } // end while
                              }
                } // end while
            }
        } // end while
    }
    print '</select>';
    
    PHP:
     
    ads2help, Sep 25, 2008 IP
    Exa likes this.
  3. Exa

    Exa Active Member

    Messages:
    471
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    85
    #3
    Thanks!

    Now say I get these info from MySQL, and I want to echo it in a HTML table. But this table has two columns, how do I split the results into a table with two columns? Like

    <tr>
    <td>results</td>
    <td>results2</td>
    </tr>
    <tr>
    <td>results</td>
    <td>results2</td>
    </tr>

    Instead of simply

    results <br />
    results <br />

    THANKS!
     
    Exa, Sep 26, 2008 IP
  4. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #4
    show me the database table?
     
    ads2help, Sep 26, 2008 IP
  5. Exa

    Exa Active Member

    Messages:
    471
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    85
    #5
    It's like the same as above, but ignoring the hierarchy.

    So say I want to echo the above categories in a table,

    print '<table width="100%" cellpadding="3">
    <tbody>';
    while ($cat = mysql_fetch_assoc($get_cat)) {
    print '<tr><td>'.$cat[name].'</td></tr>';
    }
    PHP:
    That will print:

    But how do I go about doing it, such that I can get something like:

    <table width="100%" cellpadding="3">
    <tbody>
    <tr><td>cat1</td><td>cat2</td></tr>
    <tr><td>cat3</td><td>sub1</td></tr>
    HTML:
    and so on.

    Thanks! :D:D
     
    Exa, Sep 26, 2008 IP
  6. iKiller

    iKiller Guest

    Messages:
    52
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    maybe something like this :


    print '<table width="100%" cellpadding="3">
    <tbody>';
    $counter = 0;
    while ($cat = mysql_fetch_assoc($get_cat)) {
    if($counter % 2 == 0)
    	{
    		print("<tr>");		
    	}
    	$counter += 1;
    print '<td>'.$cat[name].'</td>';
    
    
     	if($counter % 2 == 0)
    	{
    		print("</tr>");
    	}
    }
    PHP:
     
    iKiller, Sep 26, 2008 IP
  7. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #7
    So based on your HTML it will be

    
    $limit_per_row = 2; // since you want two only
    print '<table width="100%" cellpadding="3">
    <tbody>';
    while ($cat = mysql_fetch_assoc($get_cat)) {
    if (!isset($loop)) { $loop = 1; } else {}
    if ($loop == 1) { print "<tr>\r\n"; }
    
    print '<td>'.$cat['name'].'</td>'."\r\n";
    
    if ($loop == $limit_per_row) { 
    print "</tr>\r\n";
    unset($loop);
    }
    
    $loop++;
    }
    
    // remember to close the table
    
    PHP:
     
    ads2help, Sep 26, 2008 IP
  8. Mahmoud Jbarin

    Mahmoud Jbarin Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #8
    ads2help ... Thank You ,But the database us ( in the table ) ... id-name-paretn_id ??
     
    Mahmoud Jbarin, Jul 27, 2013 IP