select box
HI there.
Ive got a quick question.
How do I have a select box show a particular value.
Same as a previous post but I dont think it got answered
--------------previous post---------------------
I have a select box which gets values from a db.
The table is as follows
itemID
listID
item
How do a set the value of the select box to highlight the value as set
in parent id
Select query is
select item from table WHERE listid = $itemtid
Re: select box
> HI there.
>
> Ive got a quick question.
>
> How do I have a select box show a particular value.
>
> Same as a previous post but I dont think it got answered
>
I use the following function....
// $name = the field name
// $type = "selection" || "checkbox" || "radio", the type of field you want
to display the data in.
// $class = the class to use
// $listarray = array("id","value")
// $selected = Which item is selected within the list in the format
"int,int,int" for radio and in the format "int" for selection & checkbox
// So just by changing the $type in the calling function you can have the
data displayed in 3 ways.
function getFormInput($name,$type,$class,$listarray,$selected=""){
$res = "";
if($type == "selection"){
$res .= "<select name='".$name."' class='".$class."'>\n";
for ($i=0;$i <= count($listarray);$i++) {
$sel = ($selected == $listarray[$i]['id']? " selected" : "");
$res .= "<option
value='".$listarray[$i]['id']."'$sel>".$listarray[$i]['name' ]."</option>\n";
}
$res .= "</select>\n";
}elseif($type == "checkbox"){
$selsplit = explode(',',$selected);
for ($i=1;$i <= count($listarray);$i++) {
$sel = (in_array($listarray[$i]['id'],$selsplit)? " checked" : "");
$res .= "<input type='checkbox' name='".$name."[".$i."]'
value='".$listarray[$i]['id']."'$sel>".$listarray[$i]['name' ]."<br>\n";
}
}elseif($type == "radio"){
for ($i=1;$i <= count($listarray);$i++){
$sel = ($listarray[$i]['id'] == $selected) ? " checked" : "";
$res .= "<input type='radio' name='".$name."'
value='".$listarray[$i]['id']."'$sel>".$listarray[$i]['name' ]."<br>\n";
}
}
return $res;
}
Hope that helps
MS
Join MyClubWeb.co.uk - The Home of Club Websites and Medium to Small
Businesses.
Re: select box
On Thu, 11 Jan 2007 14:51:41 -0000, "MS" <MS [at] nospam.com> wrote:
>> HI there.
>>
>> Ive got a quick question.
>>
>> How do I have a select box show a particular value.
>>
>> Same as a previous post but I dont think it got answered
>>
>
>I use the following function....
>
>// $name = the field name
>// $type = "selection" || "checkbox" || "radio", the type of field you want
>to display the data in.
>// $class = the class to use
>// $listarray = array("id","value")
>// $selected = Which item is selected within the list in the format
>"int,int,int" for radio and in the format "int" for selection & checkbox
>
>// So just by changing the $type in the calling function you can have the
>data displayed in 3 ways.
>
>function getFormInput($name,$type,$class,$listarray,$selected=""){
> $res = "";
> if($type == "selection"){
> $res .= "<select name='".$name."' class='".$class."'>\n";
> for ($i=0;$i <= count($listarray);$i++) {
> $sel = ($selected == $listarray[$i]['id']? " selected" : "");
> $res .= "<option
>value='".$listarray[$i]['id']."'$sel>".$listarray[$i]['name ']."</option>\n";
> }
> $res .= "</select>\n";
> }elseif($type == "checkbox"){
> $selsplit = explode(',',$selected);
> for ($i=1;$i <= count($listarray);$i++) {
> $sel = (in_array($listarray[$i]['id'],$selsplit)? " checked" : "");
> $res .= "<input type='checkbox' name='".$name."[".$i."]'
>
>value='".$listarray[$i]['id']."'$sel>".$listarray[$i]['name ']."<br>\n";
> }
> }elseif($type == "radio"){
> for ($i=1;$i <= count($listarray);$i++){
> $sel = ($listarray[$i]['id'] == $selected) ? " checked" : "";
> $res .= "<input type='radio' name='".$name."'
>
>value='".$listarray[$i]['id']."'$sel>".$listarray[$i]['name ']."<br>\n";
> }
> }
> return $res;
>}
>
>
>Hope that helps
>
>MS
>Join MyClubWeb.co.uk - The Home of Club Websites and Medium to Small
>Businesses.
Thanks for the response.
Bit confused.
This is the code I currently have which gets me the info from the db.
$get = "SELECT * FROM categories";
$answer = mysql_query($get)
or die ("Couldn't execute query.");
echo "<select name=listid' class='gradientform'>\n";
while ($row = mysql_fetch_array($answer))
{
extract($row);
echo "<option value='$itemid'>$item\n";
}
echo "</select>\n";
But I would like to do is based on having the itemID value passed via
URL I would like to get the matching listid vallue selected.
Re: select box
Message-ID: <g6kcq211hinohaqm5kls2qve4ki50duab6 [at] 4ax.com> from Peter
revera contained the following:
>$get = "SELECT * FROM categories";
>$answer = mysql_query($get)
>or die ("Couldn't execute query.");
>
> echo "<select name=listid' class='gradientform'>\n";
> while ($row = mysql_fetch_array($answer))
> {
> extract($row);
> echo "<option value='$itemid'>$item\n";
> }
> echo "</select>\n";
$get = "SELECT * FROM categories";
$answer = mysql_query($get)
or die ("Couldn't execute query.");
echo "<select name=listid' class='gradientform'>\n";
while ($row = mysql_fetch_array($answer))
{
extract($row);
$selected=(isset($_GET['listid']) &&$_GET['listid']==$itemid)?"
selected":"";
echo "<option value='$itemid'$selected>$item</option>\n";
}
echo "</select>\n";
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Re: select box
On Thu, 11 Jan 2007 15:11:37 +0000, Geoff Berrow
<blthecat [at] ckdog.co.uk> wrote:
>Message-ID: <g6kcq211hinohaqm5kls2qve4ki50duab6 [at] 4ax.com> from Peter
>revera contained the following:
>
>>$get = "SELECT * FROM categories";
>>$answer = mysql_query($get)
>>or die ("Couldn't execute query.");
>>
>> echo "<select name=listid' class='gradientform'>\n";
>> while ($row = mysql_fetch_array($answer))
>> {
>> extract($row);
>> echo "<option value='$itemid'>$item\n";
>> }
>> echo "</select>\n";
>
>$get = "SELECT * FROM categories";
>$answer = mysql_query($get)
>or die ("Couldn't execute query.");
>
> echo "<select name=listid' class='gradientform'>\n";
> while ($row = mysql_fetch_array($answer))
> {
> extract($row);
>
> $selected=(isset($_GET['listid']) &&$_GET['listid']==$itemid)?"
>selected":"";
> echo "<option value='$itemid'$selected>$item</option>\n";
> }
> echo "</select>\n";
Ive added the code and when I view the form with the variable pased in
the url the select list hasnt changed. It just defaults to the normal
order.
Re: select box
Message-ID: <rhlcq29gimodffn7131cal3bc66splqi7t [at] 4ax.com> from Peter
revera contained the following:
>> $selected=(isset($_GET['listid']) &&$_GET['listid']==$itemid)?"
>>selected":"";
>> echo "<option value='$itemid'$selected>$item</option>\n";
>> }
>> echo "</select>\n";
>
>
>Ive added the code and when I view the form with the variable pased in
>the url the select list hasnt changed. It just defaults to the normal
>order.
How are you passing the variable?
Should be like this:
www.example.com?listid=someid
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
Re: select box
On Thu, 11 Jan 2007 15:29:26 +0000, Geoff Berrow
<blthecat [at] ckdog.co.uk> wrote:
>Message-ID: <rhlcq29gimodffn7131cal3bc66splqi7t [at] 4ax.com> from Peter
>revera contained the following:
>
>>> $selected=(isset($_GET['listid']) &&$_GET['listid']==$itemid)?"
>>>selected":"";
>>> echo "<option value='$itemid'$selected>$item</option>\n";
>>> }
>>> echo "</select>\n";
>>
>>
>>Ive added the code and when I view the form with the variable pased in
>>the url the select list hasnt changed. It just defaults to the normal
>>order.
>
>How are you passing the variable?
>Should be like this:
>www.example.com?listid=someid
Yep.
edit_item.php?listid=68
Re: select box
Message-ID: <oulcq2d6sqq1c2oovj6g4lp2jk9ep95c4o [at] 4ax.com> from Peter
revera contained the following:
>>How are you passing the variable?
>>Should be like this:
>>www.example.com?listid=someid
>
>Yep.
>
>edit_item.php?listid=68
Works for me
http://www.ckdog.co.uk/test/urlselect.php?listid=3
http://www.ckdog.co.uk/test/urlselect.php?listid=2
used an array to simulate your database call
$items=array(1=>"Item1",2=>"Item2",3=>"Item3",4=>"Item4");
echo "<select name=listid' class='gradientform'>\n";
foreach($items as $itemid=>$item){
$selected=(isset($_GET['listid']) &&$_GET['listid']==$itemid)?"
selected":"";
echo "<option value='$itemid'$selected>$item</option>\n";
}
echo "</select>\n";
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011