Re: [Phplib-users] Select Box!
Brought to you by:
nhruby,
richardarcher
From: Marylly A. S. <ima...@ya...> - 2005-04-29 17:51:49
|
Like you answered me, I can use the function make_select_list() to make select box. But how can I make more than one select box if i can't declare the function more than once? I'm wanting to do a pge using the PHPLIB Templates with two select boxes ,beind one of them depends on the other to generate the option list and after select the two select boxes will show a list from the database. if you didn't understand i can give more details please, help me thanks any way marylly **Mya** Andrew Crawford wrote: > Hi Mya, > > Are you using PHPLIB's database abstraction? Are you using PHPLIB > templates? > > If the answer to these questions is no, you have a general PHP > question and should look for another mailing list or forum to get the > answer. > > If the answer to both questions is yes, you should be able to do > something like this: > > function make_select_list($selected) { > $exampledb = new DB_Example; > $query = "SELECT label,value FROM table ORDER BY label"; > $exampledb->query($query); > > // Did we get any results? If not, fail appropriately ... > > // Loop template > $loop_t = new Template_Example; > > // our mini-template > $loop_t->set_var("option", "<option > value=\"{value}\"{selected}>{label}</option>\n"); > > // put our data into the template > while ($exampledb->next_record()) { > $loop_t->set_var(array( > "label" => $exampledb->f("label"), > "value" => $exampledb->f("value"), > "selected" => ((!is_null($selected) and > ($exampledb->f("value") == $selected)) ? " selected" : ""))); > $loop_t->parse("options", "option", true); > } > > // return the options list > return $loop_t->get_var("options"); > } > > The returned value will then contain a series of options: > > <option value="value1">Label 1</option> > <option value="value2">Label 2</option> > ... > <option value="valuen">Label n</option> > > ... and you can put it into your main page template, assigning it to a > template variable just like anything else. > > Template: > > <!-- top part of page goes here --> > <select name="example_select"> > {option_list} > </select> > <!-- bottom part of page goes here --> > > > Code: > > // Set selected to appropriate value > $selected = ""; > > // Set up your template > $t = new Template; > $t->set_file('content', 'example_template.ihtml'); > > // Generate the option list > $our_option_list = make_select_list($selected); > > // Fill in the variables > $t->set_var('option_list', $our_option_list); > > // Output the page > $t->parse('out', 'content'); > $t->p('out'); > > I hope that helps. > > Andrew Crawford > > Marylly Araujo Silva wrote: > >> hello guys >> >> Can I create a select box and create the options using a db query >> from the database? >> >> thanks >> >> Marylly >> **Mya** > > |