Dynamic Content thoughts

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello everyone,

                In my quest to build bigger and better dynamic content, I am
putting forth a concept to see what you all think.

Many times I come across customers who want drop down menus dynamically
built from database tables.

 

Old way Example:

Echo '<SELECT ID=personnel><option value='0'>--Please  Select--</option>';

$query = "SELECT * FROM personnel ORDER BY last_name";

$result = mysql_query($query);

If(mysql_num_rows($result) >= 1)

{

While($row = mysql_fetch_assoc($result))

{

Echo "<option value='".$row['ID']."'>".$row['first_name']."
".$row['last_name']."</option>";

                }

}

Echo '</select>';

*********************************************************

 

I am purposing a Method for this that has some flexibility.

 

Initialize Object:  $yourobject = new yourclass();

Call method: // The call design is just so you have a better understanding
of my concept

$dropdown = $yourobject-> dropmenu('personnell','ID',array(0 =>
'first_name', 1 => 'last_name'), 'last_name'); 

 

Function dropmenu($table,$fieldforvalue,$fieldstodisplay,$fieldorder)  //
Yes you could add some WHERE filters as well 

{

                $arraytoreturn = array();

                If(strlen($table) >= 3){   

                if(is_array($fieldstodisplay)){

                $count = 0;

                                foreach($fieldstodisplay as $key=>$values){

                                                if(strlen($values) >=3){

                                If($count == 0){

                                $fields = $values;

}else{

$fields . = ",".$values;

                                }                              

$count++;

                                                                }


}

                }else{

                                If(strlen(($fieldstodisplay) >= 1){

                                $fields = $fieldstodisplay;

                                $fieldstodisplay = array(0
=>$fieldstodisplay);

                                }else{

                                Return $arraytoreturn; // Return nothing
because no field was selected.

}

                }

}else{

Return $arraytoreturn; // Return nothing because no table was selected.

}              

                If(strlen($fieldorder) >= 3) {

$orderfilter = " ORDER BY ".$fieldorder." ";          

                }else{

                $orderfilter = "";

}

$query = "SELECT ".$fields." FROM ".$table." ".$orderfilter." ";

$result = mysql_query($query);

If(mysql_num_rows($result) >= 1)

{

$arraytoreturn[] = "<option value=0>--Please Select--</option>";

                                While($row = mysql_fetch_assoc($result))

{

$display_fields   = "";

                Foreach($fieldstodisplay as $key=>$values){

                $display_fields  .= $row[$values]." ";      

                }              

                If(strlen($fieldforvalue >= 3){

                $arraytoreturn[] = "<option
value='".$row[$fieldforvalue]."'>".$display_fields  ."</option>";

                }else{

                $arraytoreturn[] = "<option>".$display_fields  ."</option>";

                }

}

                Return $arraytoreturn;

                }else{

Return $arraytoreturn; // Nothing to return.

                }

}

 

Now I can call the drop downs driven by database tables dynamically and It
saves me a TON of time.

Echo '<SELECT ID=personnel>';

Foreach($dropdown as $key=>$values){

Echo $values;

}

Echo '</select>';

 

 

Richard L. Buskirk

"Some of the world's greatest feats were accomplished by people not smart
enough to know they were impossible"

 


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux