Re: Multiple 'similar' dropdowns -> 1 query



I noticed that Message-ID:
<1122490398.862551.269070@xxxxxxxxxxxxxxxxxxxxxxxxxxxx> from frizzle
contained the following:

>Hmm, could you maybe explain a little bit more?
>I'm not trying to find a better way for the '$selected'-part,
>but i'm not able (yet) to create the same dropdown three
>times from 1 query ...

If you are doing something more than once you need to be thinking
'function'
Let is say you query the database and do a loop to put the values in an
array. In the following function, $name is the name of the select box,
$array is the array from the database and $sel is the selection you are
testing.

function mk_select($name,$array,$sel){
$string = "<select name=\"$name\"class=\"input\">";
foreach ($array AS $key=>$value) {
if($key==$sel){$selected=" selected";}
else{$selected="";}
$string .= "<option
value=\"$key\"$selected>$value</option>\n";
}
$string.="</select>";
return $string;
}
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
.



Relevant Pages

  • Re: form - request question
    ... Name your desired variables, loop through the $_POST/$_GET array, ... these names are used to build the query. ... column names in de database. ... you''d change the location or surrounding code of the script it could break. ...
    (alt.php)
  • Re: Can someone tell me what is wrong with this?
    ... > in checkboxes that if selected, will be then be used to select specific ... you're looping through each item in an array. ... If it is the result of a database ... query, is it perhaps an array of arrayrefs? ...
    (comp.lang.perl.misc)
  • Re: Count rows in dataset WHERE ...
    ... This will return an array of rows matching the ... things you can do in a database query, and it is certainly going to be far ... less efficient then a SQL engine that has query optimization, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Algorithm isnt working
    ... > supposed to connect to a database and perform a query to get all the ... What makes you so certain that the order in the $files array corresponds ... For each loop you should check for its presence in the ...
    (comp.lang.php)
  • Re: making database
    ... Hmm, why? ... If you have .mdf/,ldf then you might attach them, but it is ... create database. ...
    (microsoft.public.sqlserver.programming)