PHP Multilevel drop down generator class

Slothie

New Member
Messages
1,429
Reaction score
0
Points
0
PHP:
<?php
//sample usage
$drop = new dropper();
$drop->tab = ( '--' );
$drop->generate_drop();
echo $drop->output;
//end sample

class dropper
{
    public $dbhost;
    public $dbuser;
    public $dbpass;
    public $dbname;
    public $table;
    public $parent ;
    public $desc;
    public $id;
    public $tab;
    private $tablvl;
    public $output;

    function __construct()
    {
        $this->dbhost = "localhost";
        $this->dbuser = "root";
        $this->dbpass = "changeme";
        $this->dbname = "dbame"
        $this->table = "tablename";
        $this->parent = "parent";
        $this->desc = "desc";
        $this->id = "primarykey";
        $this->tablvl = 0;
        $this->output = '';
        $this->filler = "";
    } 

    function generate_drop()
    {
        $link = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpass );
        mysql_select_db( $this->dbname );
        $res = mysql_query( "select count(*)  FROM  " . $this->table );
        $numros = mysql_fetch_array( $res );
        $numrows = $numros[0];
        $this->output .= ( "<select  name=\"hierarchy\" >\n" );
        $this->output .= ( "<option  value=\"null\"  selected=\"selected\">" . $this->filler . "</option>\n" );
        $this->print_kids( '' );
        $this->output .= ( "</select>" );
        @mysql_close( $link );
    } 

    function print_kids( $pos = '' )
    {
        $link = mysql_connect( $this->dbhost, $this->dbuser, $this->dbpass );
        mysql_select_db( $this->dbname );
        $query = "Select " . $this->id . "," . $this->desc . "," . $this->parent . " from " . $this->table . " where " . $this->parent;
        if ( $pos == '' )
        {
            $query .= " IS NULL";
        } 
        else
        {
            $query .= " = $pos";
        } 

        $res = mysql_query( $query );

        while ( $row = mysql_fetch_array( $res ) )
        {
            $q = "Select count(*) from " . $this->table . " where " . $this->parent . "=" . $row[0] ;

            $x = mysql_query ( $q );

            $hk = mysql_fetch_array( $x );
            $this->output .= ( "<option  value=\"$row[0]\">" );
            for ( $i = 0; $i < $this->tablvl; $i++ ) $this->output .= ( $this->tab );
            $this->output .= ( "$row[1]</option>\n" );

            if ( $hk[0] > 0 )
            {
                $this->tablvl++; 
                // echo $row[0]."!!!!";
                $this->print_kids( $row[0] );
            } 
        } 
        $this->tablvl--;
        @mysql_close( $link2 );
    } 
} 

?>
Notes:
  • This is a PHP5-Only class (Although it should be easily modifiable to php4)
  • All public class variables are modifiable from outside
Just reply here if you have any questions, and as usual... rep me if ya find this useful.
 
Last edited:

tobyb

New Member
Messages
7
Reaction score
0
Points
0
thankx i have been trying to get this to work all night got it to work now:)
 

DarkDragonLord

New Member
Messages
782
Reaction score
0
Points
0
too bad i have no idea what this makes
im still at basics of PHP lol XD
would you mind adding a demo or explaining to a newbie what it makes?
 

php2lthands

New Member
Messages
1
Reaction score
0
Points
0
Am trying to get dropdown to work
public $dbhost; //ok
public $dbuser; // ok
public $dbpass; // ok
public $dbname; // ok
public $table; // ok
public $parent ; // is this a field from the table?
public $desc; // ok
public $id; // is the name of id field to put here - for reasons best known to me my primary key is PVMS
public $tab; // presume required by function leave empty
private $tablvl;// presume required by function leave empty
public $output; // presume required by function leave empty

help me sleep please.
 
Last edited:
Top