Re: Dynamic Content thoughts

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

 



On Thu, Aug 23, 2012 at 2:51 PM, admin <admin@xxxxxxxxxxxxxxxxxxx> wrote:
> 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"

Some things you may want to consider in extending this:

* The separation of code and data is important to consider; this sort
of function, for me, falls into the grey area in between; I often have
built up the array of options and other elements of the <select> and
then have passed it onto the portion of the app that handles views,
keeping any sort of HTML out of the way of the database queries, etc.
Encapsulating this in a single class brings up the issues of data base
agnosticism as well.

* Often times, on a form, one may want to include a <label> for the
select statement.

* At some point, you may want to use the "selected" attribute in one
of the options.

My HTML selects typically have the following form:

<label for="dyn_select">Select the appropriate item:</label>
<select name="dyn_select" id="dyn_select">
  <option value="opt1">Item One</option>
  <option value="opt2" selected="selected">Item Two</option>
</select>

The code I use that generates this is at http://pastebin.com/SZYN5qgv

The call would be something like:

echo dynamic_select(array('opt1'=>'Item One','opt2'=>'Item
Two'),'dyn_select','Select the appropriate item:','opt2');

The first parameter can obviously be an array created from a database
query using whatever database methods you'd like: mysql, mysqli, pg_*,
PDO, etc.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[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