Re: Is there a way to display data from database for Select Options in a form?

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

 



You mean like...

$Link = mysql_connect("Host", "User", "Pass");
mysql_select_db("MyDB");
$Datefield = "Dates";
$Select = mysql_query("SELECT * FROM `Table` WHERE `".$Dates."` LIKE *");
$MySelect = "<select onChange='WhateverJavascript()' id='whateverId'>"
While($Row = @mysql_fetch_assoc($Select)) {
  //Adds each date to the select where the value is also equal to the date
  //You can use an increment as well for the value, or whatever you want
  $MySelect .= "<option
value='".$Row[$Datefield]."'>".$Row[$Datefield]."</option>";
}
$MySelect .= "</select>";
mysql_close($Link);

On 1/10/06, Jochem Maas <jochem@xxxxxxxxxxxxx> wrote:
>
> Sue wrote:
> > Hello,
> >
> > We have a form that contains a Select option for a listing of available
> > dates for the user to choose from.  Right now we have to manually change
> the
> > dates within the form's Selection list as new dates become available to
> > choose from.  We currently store these available dates in our database,
> and
> > am wondering if we can somehow extract these dates from the table to
> display
> > in our form?  This would eliminate alot of maintenance needed to this
> form.
> > If anyone has any ideas as to reference material etc. that I may look
> which
> > would give me an idea how to do this using PHP, I'd appreciate it!
>
> I am guessing that your php skills are not that hot. filling a select box
> is php 101.... here is function that generates a selectbox based on the
> given
> 'data' - now you have to go and figure out how to build the array $items
> (which is passed as the first arg to this function) from the data in your
> table:
>
> /** selectTag()
> *  generate an html select list using the given $items as options.
> *
> * @param   array   $items      - list of items to display as options
> * @param   array   $selected   - value of select item in $items
> * @param   string  $selectname - value of select tag's name attribute
> * @param   numeric $addnone    - add a 'blank' option to the top of the
> list
> *                               (different types depending on valuepassed)
> * @param   string  $onchange   - value of select tag's onchange attribute
> * @param   string  $selectid   - value of select tag's id attribute
> * @param   boolean $readonly   - whether the select tag is readsonly or
> not
> * @param   array   $readonly   - whether the select tag is readsonly or
> not
> * @param   boolean $option_class_arr   - optional CSS class names for
> individual option tags
> * @param   string  $selectHTMLattribs  - passthru HTML (attributes for the
> select tag)
> *
> * @return  string
> */
> function selectTag($items, $selected = '', $selectname = '',
>                   $addnone = 0, $onchange = '', $selectid = '',
>                   $readonly = 0, $option_class_arr = array(),
> $selectHTMLattribs = '')
> {
>     // must have an array of items
>     if (!is_array($items)) {
>         return '';
>     }
>
>     // determine the value of the selected item
>     $selected = strtolower(trim($selected));
>
>     // list of options we will generate
>     $opts     = array();
>
>     // add item with value 0 ?
>     $add_none_value = 0;
>     switch ( $addnone )  {
>         case '1':   // item display = 'none'
>             $zero_id_item = getStr('None');
>             break;
>         case '3':   // item display = '0'
>             $zero_id_item = getStr('zero_digit');
>             break;
>         case '2':   // item display = 'all'
>         case '4':   // item display = 'all <digit>' e.g. 'all 3'
>             $zero_id_item = getStr('All');
>             break;
>         default:
>             if(!empty($addnone) && $addnone != '0') {
>                 if(is_array($addnone)) {
>                     list($add_none_value, $zero_id_item) =
> @each($addnone);
>                 } else {
>                     // here we can drop in a custom 'blank' item:
>                     $zero_id_item = $addnone;
>                 }
>             }
>             break;
>     }
>     if (isset($zero_id_item)) {
>         $thisselected = ($selected > '')  ? '' : ' selected="selected"';
>         // FIX THE NEXT LINE - BUT NOT IMPORTANT
>         $opts[]     = '<option value="'.htmlentities($add_none_value,
> ENT_QUOTES).'"'
>                      .$thisselected'>'.ucwords($zero_id_item).'</option>';
>     }
>
>     $thisselected = '';
>     foreach ($items as  $value => $displayname) {
>         $thisselected = ($selected > '' && $selected ==
> strtolower($value)) ? ' selected="selected"' : '';
>         $class_var    = (isset($option_class_arr[$value])) ? ' class="' .
> $option_class_arr[$value] . '" ' : '';
>         $opts[]       = '<option value="'.htmlentities($value, ENT_QUOTES)
>
> .'"'.$thisselected.$class_var.'>'.ucwords($displayname).'</option>';
>     }
>
>     // various select tag attribute values
>     $onchange = (($onchange = trim($onchange)) > '')
>               ? " onchange=\"$onchange\""
>               : ''
>               ;
>     $id       = ($selectid = strval($selectid))
>               ? " id=\"{$selectid}\""
>               : ''
>               ;
>     $readonly = ($readonly == 1)
>               ? ' readonly="readonly"'
>               : ''
>               ;
>
>     return "<select name=\"{$selectname}\"
> {$selectHTMLattribs}{$id}{$onchange}{$readonly}>".
>            join('', $opts).
>            '</select>';
> }
>
>
> >
> > Thanks!
> > Sue
> >
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
-----BEGIN PGP MESSAGE-----
The One and Only DarkX
-----END PGP MESSAGE-----
-----BEGIN PGP SIGNATURE----
owGbwMvMwCRoO3M+/7fM0iLG07JJDE4vrReHZKQq+OelKiTmpQDpnEoFl8Si7Aiu
DntmVpD8BpgGQabIGob5ToUFGnFlWgz8cj6zPulrPMrfdI2ZYX7wx/++aeWli6aY
7ZqsahKeKc789DAA
=k1EI
-----END PGP SIGNATURE-----

[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