Page 1 of 1

"Checking" appropriate radio button when displayin

Posted: Tue Sep 24, 2002 1:21 pm
by Crashin
I'm posting this in PHP initially, although it might pertain to DB's as well. Move it if you must. :)

What's a convenient method to "check" the appropriate radio button when displaying data pulled from a DB? More specifically, I've got a form that has several radio buttons with multiple selections, but obviously only one can/should be selected based on the value in the DB. The only working method I've come up with so far seems cumbersome to me, hence this post. It looks somewhat as follows:

Code: Select all

//set variables to check appropriate radio buttons
if ($rowїnews] == 1) {
	$news_rand = " checked";
}
else {
	$news_rand = "";
}

if ($rowїnews] == 2) {
	$news_last = "checked";
}
else {
	$news_last = "";
}
Then, to check the appropriate button:

Code: Select all

echo "<td><font class='small'>Display news items:</font></td>";
echo "<td><font class='small'>Random<input type='radio' name='news' value='1'$news_rand>&nbsp;Last Entered<input type='radio' name='news' value='2'$news_last></font></td>";
Any thoughts on improvements?

Posted: Tue Sep 24, 2002 4:36 pm
by mydimension
thats the way i do it, just go through each item one by one and see if that particualar item should be checked. can't think of any improvements but then again im not a perfect programmer either.

Posted: Tue Sep 24, 2002 4:41 pm
by Crashin
Yeah, I thought there might be some tricky loop trick that I was missing. You know how it is...if it's really long it doesn't seem efficient enough.

Posted: Tue Sep 24, 2002 4:52 pm
by nielsene
Well it depends, there are some tricks and some nice loops, but it depends on who your options are laid out.

One trick I've seen
$selected[$indexOfSelectedItem]="checked=\"checked\" ";
now use a loop to creat your checkboxes (or options in a select)
and
<input type=\"checkbox\" name=\"name\" value=\"{$value[$i]}\" {$selected[$i]} />

Very compact and nice, if you have your values in an appropriate array and can easiy get the index of the selected item in that array

I'm still undecided if I like this or not. It feels a little too tricky, for me to really consider it good practice, but you can't argue with its simplicity.

I tend to use the same loop but then use
<input type=\"checkbox\" name=\"name\" value=\"{$value[$i]}\" ". ($selectedValue==$value[$i] ? "checked=\"checked\" " : ""). />

Which is less efficient, uses the tertiary operator, but still feels better to me for some stupid reason.

Posted: Tue Sep 24, 2002 5:02 pm
by Crashin
That is neat and clean...I might mess around with that a bit. Thanks for the tip! :)

Posted: Tue Sep 24, 2002 6:14 pm
by Coco
*was gonna post something similar to that solutuion but went to pub instead*
call me a liar but thats the way i woulda done it to start with

Posted: Tue Sep 24, 2002 6:16 pm
by nielsene
::)
Were you the author of the earlier post I stole that from? If so I'm sorry for beating you to it, but I couldn't find the old thread to give proper credit.

Posted: Tue Sep 24, 2002 6:21 pm
by Coco
nah
just to me it seemed logical to do it that way

but thanks for the credit :)

i really dont deserve it im still a newbie really :(

sorta thing i was thinking is to have either 'selected' or NULL as the return for each checkbox...
meaning that it is selected or not