zachatk1 Posted April 29, 2011 Share Posted April 29, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/235116-pulling-mysql-data-for-check-boxes-and-radio-buttons/ Share on other sites More sharing options...
fugix Posted April 29, 2011 Share Posted April 29, 2011 do you mean that when a user selects a certain radio button and saves...that button will be filled in when they load the page again? Quote Link to comment https://forums.phpfreaks.com/topic/235116-pulling-mysql-data-for-check-boxes-and-radio-buttons/#findComment-1208340 Share on other sites More sharing options...
wildteen88 Posted April 29, 2011 Share Posted April 29, 2011 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 />'; } Quote Link to comment https://forums.phpfreaks.com/topic/235116-pulling-mysql-data-for-check-boxes-and-radio-buttons/#findComment-1208353 Share on other sites More sharing options...
zachatk1 Posted April 30, 2011 Author Share Posted April 30, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235116-pulling-mysql-data-for-check-boxes-and-radio-buttons/#findComment-1208478 Share on other sites More sharing options...
Pikachu2000 Posted April 30, 2011 Share Posted April 30, 2011 Looks like a typo (or a moment of indecision) to me; if() is not used with ternary syntax. $selected = in_array($cbox_value, $db_values) ? ' checked="checked"' : null; Quote Link to comment https://forums.phpfreaks.com/topic/235116-pulling-mysql-data-for-check-boxes-and-radio-buttons/#findComment-1208515 Share on other sites More sharing options...
zachatk1 Posted April 30, 2011 Author Share Posted April 30, 2011 Great it works!! Now only thing, how would you have the check box be bolded (so if the user clicked a different radio box but wasn't sure what they had)? Quote Link to comment https://forums.phpfreaks.com/topic/235116-pulling-mysql-data-for-check-boxes-and-radio-buttons/#findComment-1208613 Share on other sites More sharing options...
wildteen88 Posted April 30, 2011 Share Posted April 30, 2011 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 />'; } Quote Link to comment https://forums.phpfreaks.com/topic/235116-pulling-mysql-data-for-check-boxes-and-radio-buttons/#findComment-1208616 Share on other sites More sharing options...
zachatk1 Posted May 1, 2011 Author Share Posted May 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235116-pulling-mysql-data-for-check-boxes-and-radio-buttons/#findComment-1208896 Share on other sites More sharing options...
wildteen88 Posted May 1, 2011 Share Posted May 1, 2011 Post your code here. Quote Link to comment https://forums.phpfreaks.com/topic/235116-pulling-mysql-data-for-check-boxes-and-radio-buttons/#findComment-1208965 Share on other sites More sharing options...
zachatk1 Posted May 1, 2011 Author Share Posted May 1, 2011 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). Quote Link to comment https://forums.phpfreaks.com/topic/235116-pulling-mysql-data-for-check-boxes-and-radio-buttons/#findComment-1209233 Share on other sites More sharing options...
wildteen88 Posted May 2, 2011 Share Posted May 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235116-pulling-mysql-data-for-check-boxes-and-radio-buttons/#findComment-1209365 Share on other sites More sharing options...
zachatk1 Posted May 3, 2011 Author Share Posted May 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235116-pulling-mysql-data-for-check-boxes-and-radio-buttons/#findComment-1209704 Share on other sites More sharing options...
wildteen88 Posted May 3, 2011 Share Posted May 3, 2011 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']); Quote Link to comment https://forums.phpfreaks.com/topic/235116-pulling-mysql-data-for-check-boxes-and-radio-buttons/#findComment-1210022 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.