Radio buttons, php <-->mysql

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
jbrave
Forum Newbie
Posts: 4
Joined: Wed Oct 19, 2011 5:26 pm

Radio buttons, php <-->mysql

Post by jbrave »

I have a form where the user directly types in a value which gets stored in a mysql database, and a form that retrieves displays and allows editing the value. I want to change this to be a radio button. I'm not sure how to do this. Here is the current html & php stuff:

Code: Select all

Add:
<td width = "150"><div align="right"><label for="link_type">Link Type</label></div></td><td><input id="link_type" name="link_type" type="text" size="25" value="" maxlength="25"></td>

Update:
echo "<b>Link Type:</b> $link_type<br>";
Can someone guide me on how to modify these two forms to display radio buttons that reflect the state of link_type?

Thanks

- Joel
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Radio buttons, php <-->mysql

Post by twinedev »

Just to make sure I understand correctly, you want to give them a set of Link Types to choose from, completely removing them being able to actually edit the types?

-Greg
jbrave
Forum Newbie
Posts: 4
Joined: Wed Oct 19, 2011 5:26 pm

Re: Radio buttons, php <-->mysql

Post by jbrave »

Yes, exactly, link type should be either "direct" or "playlist"

-Joel
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Radio buttons, php <-->mysql

Post by twinedev »

For the initial form to add new and the editor to edit later:

Code: Select all

<?php  // This would be somewhere in your code or an included set of functions

/**
 * Outputs a list of radio buttons for a form
 *
 * @param string $strName Name of the input
 * @param array $aryItems Array of the options, in key=>label format
 * @param string $strCurrent Current set value to mark correct item checked (default to blank)
 * @param string $strSeparator A string of HTML code to place between items (default is <br>)
 */
function echoRadioList($strName,$aryItems,$strCurrent='',$strSeparator='<br>') {
	foreach($aryItems as $value=>$label) {
		echo '<input id="',$strName,'_',$value,'" name="',$strName,'" type="radio" value="',$value,'" ';
		if ($strCurrent==$value) { echo 'checked="checked"'; }
		echo '><label for="',$strName,'_',$value,'">',htmlspecialchars($label),'</label>',$strSeparator;
	}
}

?>

<td width="150">
	<div align="right">
		Link Type
	</div>
</td>
<td>
	<!-- Be sure that for adding new, that you had defaulted $link_type to be a blank string so you don't get a notice -->
	<?php echoRadioList('link_type',array('direct'=>'Direct','playlist'=>'Playlist'),$link_type); ?>
</td>
To display value that already exists:

Code: Select all

echo '<b>Link Type:</b> ';
switch ($link_type) {
	case 'direct':
		echo 'Direct';
		break;
	case 'playlist':
		echo 'Playlist';
		break;
	default:
		echo 'Not Specified';
}
echo '<br>';
The example of the form is using the default of a <BR> between the inputs. Say you needed more (ex, you wanted each one on it's own table row):

Code: Select all

<tr><td><?php echoRadioList('link_type',array('direct'=>'Direct','playlist'=>'Playlist'),$link_type,"</td></tr>\n<tr><td>"); ?></td></tr>
-Greg
jbrave
Forum Newbie
Posts: 4
Joined: Wed Oct 19, 2011 5:26 pm

Re: Radio buttons, php <-->mysql

Post by jbrave »

Hi Greg, thanks a whole bunch. This makes a lot of sense!

I hadn't gotten to the point in my php where I was doing more than a few in-line statements and functions with php to output XML, as-needed for various tiny projects, so didn't even know there was a for-each construct in php. But now that I do, I can see a whole bunch of places where I can use this type of iterative processing.

- Joel
Post Reply