Jump to content

Arrays and checkboxes


wikedawsum

Recommended Posts

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

Link to comment
Share on other sites

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...';
  }
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.