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 );
}
}
?>
- This is a PHP5-Only class (Although it should be easily modifiable to php4)
- All public class variables are modifiable from outside
Last edited: