Re: [Phplib-users] Select Box!
Brought to you by:
nhruby,
richardarcher
|
From: Marylly A. S. <ima...@ya...> - 2005-04-26 17:31:05
|
Andrew,
I've been using the PHPLIB's database abstraction I guess.
But I doesn't matter if it is PHPLIB's data abstraction or not. I hope a
combobox which works.
I will try the first example that you have writen and later I write the
results.
see ya
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**
>
>
|