Jump to content

Pulling MySQL data for check boxes and radio buttons.


zachatk1

Recommended Posts

Hey!

 

I have it so users can submit info to my site. They have to check some check boxes and radio buttons. I want them to be able to edit this information, so I want to have the data pulled from the MySQL database and have the correct radio button selected (and the text bolded). So I'm thinking some sort of array to check for each value in the database... but I don't really know.

 

There are a few groups of radio boxes and check boxes but here is an example of one.

 

<input type="radio" name="stage" value="Stage 1" /> Stage 1
<input type="radio" name="stage" value="Stage 2" /> Stage 2
<input type="radio" name="stage" value="Stage 3" /> Stage 3

 

The information is pulled from the database like this:

 

$res=mysql_query("SELECT * FROM ACTIVE WHERE INDEX_ID=$id");
if(mysql_num_rows($res)==0) echo "There is no data in the table <br /> <br />";
else
{
for($i=0;$i<mysql_num_rows($res);$i++) 
{
  $row=mysql_fetch_assoc($res);
}
}

 

So when I pull information (lets say I want to echo it) it'd look like this:

 

$row[sTAGE]

 

The value for the stage selection can only be Stage 1, Stage 2 or Stage 3 so I need it to be selected when the page loads.

 

Thanks!

Link to comment
Share on other sites

Add all possible checkbox values into an array

$checkbox_values = array('Stage 1', 'Stage 2', 'Stage 3', 'Stage 4');

 

Next get all the db values into an array

$res=mysql_query("SELECT stage FROM ACTIVE WHERE INDEX_ID=$id");
if(mysql_num_rows($res)==0) 
    echo "There is no data in the table <br /> <br />";
else
{
    while($row=mysql_fetch_assoc($res))
        $db_values[] = $row['stage']; // add value into array
}

 

No that we have the necessary data we can generate the checkboxes.

foreach($checkbox_values as $cbox_value)
{
    $selected = if(in_array($cbox_value, $db_values)) ? ' checked="checked"' : null;
    echo '<input type="radio" name="stage" value="' . $cbox_value . '"' . $selected . ' />' . $cbox_value . '<br />';
}

 

Link to comment
Share on other sites

Yea, it's like if you went to go edit your profile on this forum on some radio buttons. Your previous selection would be the checked bullet when you first filled it out so you know what you previously put.

 

Anyway, I tried wildteens script and am getting an error on the 3rd line in the 3rd script (if statement).

 

Parse error: syntax error, unexpected T_IF

Link to comment
Share on other sites

Change the foreach loop to

foreach($checkbox_values as $cbox_value)
{
    $selected = null;
    $cbox_label = $cbox_value;

    if(in_array($cbox_value, $db_values))
    {
        $selected = ' checked="checked"';
        $cbox_label = "<b>$cbox_value</b>";
    }

    echo '<input type="radio" name="stage" value="' . $cbox_value . '"' . $selected . ' />' . $cbox_label . '<br />';
}

Link to comment
Share on other sites

Thanks!! It works great... except I think I have one problem.

 

There is one input that has like 12 checkboxes... so more than one checkbox can be selected. When one is selected, it works fine. But when there is more than one, nothing is selected or bolded.

Link to comment
Share on other sites

Ok so here is everything used for the check boxes:

 

$connect = mysql_connect("$mysql_host", "$mysql_user", "$mysql_password") or die(mysql_error());
mysql_select_db("$mysql_database") or die(mysql_error());

$res=mysql_query("SELECT * FROM ACTIVE WHERE INDEX_ID=$id");
if(mysql_num_rows($res)==0) echo "There is no data in the table <br /> <br />";
else
{
for($i=0;$i<mysql_num_rows($res);$i++) 
{
  $row=mysql_fetch_assoc($res);
  $db_year[] = $row[YEAR];
}
}

$year_values = array('Any', 'N/A', '2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009', '2010', '2011', '2012');

<?php
foreach($year_values as $year_value)
{
    $selected_year = null;
    $year_label = $year_value;

    if(in_array($year_value, $db_year))
    {
        $selected_year = ' checked="checked"';
        $year_label = "<b>$year_value</b>";
    }

    echo '<input type="checkbox" name="year" value="' . $year_value . '"' . $selected_year . ' />' . $year_label . ' ';
}
?>

 

Yea like I said, it doesn't work when more than one box is selected (but does work with only one selected).

Link to comment
Share on other sites

Check the contents of the $db_year array. Add the following after the while loop:

echo "<pre>".print_r( $db_year, true)."</pre>";

Post the output here.

 

I have tested the code by setting $db_year with hard coded values, eg add this after your while loop.

$db_year = array('2001', '2006', '2012');

And the script does select the correct checkboxes. Maybe the $db_year is not being populated correctly?

 

Also one problem you'll find when you submit your form is only the last checkbox value will be submitted. This is due to how the checkboxs are named

echo '<input type="checkbox" name="year"....

Adding [] after the name will allow for all selected checkbox values to be submitted. If you do not add the square brackets at the end of the name only the last selected checkbox value will be submitted.

Link to comment
Share on other sites

Ok so for 2 things in the years array, this is what I got.

 

Array
(
    [0] => N/A, 2004
)

 

Here's what the form looks like:

 

  <input type="checkbox" name="year[0]" value="Any" /> Any
  <input type="checkbox" name="year[1]" value="N/A" /> N/A 
<br /><br />
  <input type="checkbox" name="year[2]" value="2000" /> 2000 
  <input type="checkbox" name="year[3]" value="2001" /> 2001
  <input type="checkbox" name="year[4]" value="2002" /> 2002 
  <input type="checkbox" name="year[5]" value="2003" /> 2003 
  <input type="checkbox" name="year[6]" value="2004" /> 2004 
  <input type="checkbox" name="year[7]" value="2005" /> 2005 
  <input type="checkbox" name="year[8]" value="2006" /> 2006 
  <input type="checkbox" name="year[9]" value="2007" /> 2007 
  <input type="checkbox" name="year[10]" value="2008" /> 2008 
  <input type="checkbox" name="year[11]" value="2009" /> 2009 
  <input type="checkbox" name="year[12]" value="2010" /> 2010 
  <input type="checkbox" name="year[13]" value="2011" /> 2011 
  <input type="checkbox" name="year[14]" value="2012" /> 2012 

 

Now here is where it is processed.

 

$year = $_POST["year"];

//now checks if it was filled out

//YEAR
if(count($_POST['year'])==0)
{
echo "<div class=\"red\"><font color=\"#ff0000\">Please go back and check a Year Box</font></div>"; 
echo "<div class=\"space\"></div>";
$flag[] = 1;
} 
else 
{ 
$year_s=implode($year,", ");
echo "Year(s): <font color=\"#3333FF\">$year_s </font><br /><br />";
}

 

If $flag is 1, it won't submit the data to the database. If it's ok, it will submit the info, and redirect the user to the topic they just created.

 

$year_s is the variable submitted to the base (the one that is imploded). I have a feeling that the implode has something to do with it not working.

Link to comment
Share on other sites

Your dates seem to be stored in the database as a coma delimited list. You will need to use explode to convert the list back into an array in order for the correct checkboxes to be selected.

 

To convert the dates back into an array change

$db_year[] = $row[YEAR];

to

$db_year = explode(', ', $row['YEAR']);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.