Jump to content

load mysql data into radio buttons


bossman

Recommended Posts

I am creating an admin for a project manager website. Each Project is a title and a link. The projects are organized by date, industry, and product. I am trying to make an add/edit/delete function for the projects. So far I have successfully gotten the add and delete functions to work. But now, I am stuck on the 'edit' page. I have gotten the page to pull in the title and link to the projects, but I am having a hard time getting the radio button to have automatically have what is in the database pre-selected.

 

Right now im working with date, the options are 2006, 2007, 2008, and 2009.

 

The database has a row called date, its TEXT(255). in the row it either has 2006, 2007, etc...

 

Can anybody help me with a script that will pull in whatever the date is in the database and have it pre-populate my radio button?

Link to comment
https://forums.phpfreaks.com/topic/172569-load-mysql-data-into-radio-buttons/
Share on other sites

<?php

//Set this variable to the value in the database
//I hard code it here for example purposes
$db_date = '2007';

//Create an array of all possible values
//Or, if all possible values are stored in a list table from DB, get them there
$dates = array('2006', '2007', '2008', '2009');


//Create the radio options
$radio_opts = "";
foreach ($dates as $date)
{
    $checked = ($date==$db_date) ? ' checked="checked"' : '';
    $radio_opts .= "<input type=\"radio\" name=\"groupname\" value=\"{$date}\"{$checked}> {$date}<br />\n";
}

//other code goes here as needed

?>


//Form
Select the date:
<?php echo $radio_opts; ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.