wikedawsum Posted February 5, 2008 Share Posted February 5, 2008 Hoping someone can help me out here. I've tried searching several different places and cannot figure out how to do this. I'm trying to create an update page where a user can update their profile information. When they register, they can select different topics that they are interested in by checking the different checkboxes I have setup. These topics are inserted into my database using " . implode(',', $_POST['topics']) .". This works great. Now what I'd like to do is on the update page, return all the checkboxes, and set them to checked if that particular topic was selected at registration. I assume that I would need to do this using some sort of array, but I have never used arrays before. After searching, I found this code that I thought I could try to modify. Here's what I have so far. <?php $topics=array('Networking','Power Lunches','Entrepreneur Workshop','Financial and Retirement Planning','Executive Coaching','Mentoring Program','Charity Involvement','Encouragement','Career Change/Advice'); // the checkboxes $query = mysql_query("SELECT topics FROM users WHERE username='{$_SESSION['auth_user']}'"); $topics_check=mysql_query($query); if ($topics_check){ $row=mysql_result($topics_check); $dbchecks=explode(',',$row); foreach($topics_check as $key => $value) { if(in_array($key, $dbchecks)) { echo '<input type="checkbox" name="topics[]" value="' .$key .'" checked>' .$value .'<br>'; } else { echo '<input type="checkbox" name="topics[]" value="' .$key .'">' .$value .'<br>'; } } } else { echo 'No matches found...'; } ?> When I use that code, I get "No matches found...". I may be WAY off in even trying to modify this. Can someone point me in the right direction? Also, I understand where everything is coming from except for this part here: foreach($topics_check as $key => $value). Where do $key and $value come in? I hope I explained this clearly enough. Thanks for any input you may have. - wikedawsum Quote Link to comment https://forums.phpfreaks.com/topic/89588-arrays-and-checkboxes/ Share on other sites More sharing options...
sasa Posted February 5, 2008 Share Posted February 5, 2008 change line $query = mysql_query("SELECT topics FROM users WHERE username='{$_SESSION['auth_user']}'"); to $query = "SELECT topics FROM users WHERE username='{$_SESSION['auth_user']}'"; Quote Link to comment https://forums.phpfreaks.com/topic/89588-arrays-and-checkboxes/#findComment-458966 Share on other sites More sharing options...
wikedawsum Posted February 5, 2008 Author Share Posted February 5, 2008 Hello, sasa. Thanks for replying. That got rid of the "No matches found...", but now that portion of the page is just blank. Any thoughts? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/89588-arrays-and-checkboxes/#findComment-458974 Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 try <?php $topics=array('Networking','Power Lunches','Entrepreneur Workshop','Financial and Retirement Planning','Executive Coaching','Mentoring Program','Charity Involvement','Encouragement','Career Change/Advice'); // the checkboxes $query = "SELECT topics FROM users WHERE username='{$_SESSION['auth_user']}'"; $topics_check=mysql_query($query); if ($topics_check){ $row=mysql_fetch_array($topics_check); $dbchecks=explode(',',$row['topics']); foreach($topcs as $value) { if(in_array($value, $dbchecks)) { echo '<input type="checkbox" name="topics[]" value="' .$value .'" checked>' .$value .'<br>'; } else { echo '<input type="checkbox" name="topics[]" value="' .$value .'">' .$value .'<br>'; } } } else { echo 'No matches found...'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89588-arrays-and-checkboxes/#findComment-458976 Share on other sites More sharing options...
wikedawsum Posted February 5, 2008 Author Share Posted February 5, 2008 That worked, rhodesa. Thanks so much. I think was getting confused by the $key variable. Appreciate you both for the help! Quote Link to comment https://forums.phpfreaks.com/topic/89588-arrays-and-checkboxes/#findComment-458983 Share on other sites More sharing options...
laffin Posted February 5, 2008 Share Posted February 5, 2008 after the query the code gets very confusing why 2 mysql_results? and mysql_result requires 2 parameters $dbchecks=array(); if($topics_check=mysql_query($query,0)) $dbchecks=explode(',',$row); foreach($topics as $key => $value) { $checked= (in_array($key, $dbchecks))?" checked":""; echo "<input type=\"checkbox\" name=\"topics[]\" value=\"$key\" $checked>$value<br>"; } shud work Quote Link to comment https://forums.phpfreaks.com/topic/89588-arrays-and-checkboxes/#findComment-458988 Share on other sites More sharing options...
wikedawsum Posted February 5, 2008 Author Share Posted February 5, 2008 Two more questions regarding this. Question 1: As I said, the code that rhodesa supplied works for me. I have applied it to my other checkboxes and they all work great except for this one. <?php $income=array('< 50,000','50,000 - 75,000','75,000 - 100,000','> 100,000'); // the checkboxes $query = "SELECT income FROM users WHERE username='{$_SESSION['auth_user']}'"; $income_check=mysql_query($query); if ($income_check){ $row=mysql_fetch_array($income_check); $dbchecks=explode(',',$row['income']); foreach($income as $value) { if(in_array($value, $dbchecks)) { echo '<input type="checkbox" name="income" value="' .$value .'" checked>' .$value .'<br>'; } else { echo '<input type="checkbox" name="income" value="' .$value .'">' .$value .'<br>'; } } } else { echo 'No matches found...'; } ?> Can anyone tell me why that would not bring back the selected checkbox when I know that < 50,000 should be checked? Question 2: I should have mentioned this previously. In my registration form, I also have a text box where the user can supply other topics of interest. Is there any way to get this information to display in a checkbox on the update form? I tried the following, but it shows all of the topics of interest. It may not be possible, but thought I would ask. <?php $topics=array('Networking','Power Lunches','Entrepreneur Workshop','Financial and Retirement Planning','Executive Coaching','Mentoring Program','Charity Involvement','Encouragement','Career Change/Advice'); // the checkboxes $query = "SELECT topics FROM users WHERE username='{$_SESSION['auth_user']}'"; $topics_check=mysql_query($query); if ($topics_check){ $row=mysql_fetch_array($topics_check); $dbchecks=explode(',',$row['topics']); foreach($topics as $value) { if(in_array($value, $dbchecks)) { echo '<input type="checkbox" name="topics[]" value="' .$value .'" checked>' .$value .'<br>'; } else { echo '<input type="checkbox" name="topics[]" value="' .$value .'">' .$value .'<br>'; } } echo "Other <input type='text' name='topics[]' value='{$row['topics']}' size='25'>"; } else { echo 'No matches found...'; } ?> Thanks again for your help. Quote Link to comment https://forums.phpfreaks.com/topic/89588-arrays-and-checkboxes/#findComment-459084 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.