"Checking" appropriate radio button when displayin

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

"Checking" appropriate radio button when displayin

Post 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?
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post 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.
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post 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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post by Crashin »

That is neat and clean...I might mess around with that a bit. Thanks for the tip! :)
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post 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
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
Coco
Forum Contributor
Posts: 339
Joined: Sat Sep 07, 2002 5:28 am
Location: Leeds, UK
Contact:

Post 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
Post Reply