0

I have inserted some check box values in mysql database using PHP

And in the below image i have fetch the values:

alt text

Now i need the o/p like the below image: The values which i inserted in the database should be checked

alt text

Hope now its clear.

Thanks in advance..

3 Answers 3

2

You should have a table of available options (in this case, something like a cities table), and then a user-to-cities look-up table. Then you can loop over the cities, but also fetch which cities the user has checked.

A sample, without knowing your database structure, would be as follows:

$uid = $_SESSION['user']['id']; // your logged in user's ID

$cities = array();

// get an array of cities
$sql = "SELECT id, name FROM cities";
$res = mysql_query($sql);
while ($row = mysql_fetch_object($res)) {
    $cities[$row->id] = $row->name;
}

// get an array of cities user has checked
$sql = "SELECT DISTINCT city_id FROM users_cities WHERE user_id = '$uid'";
$res = mysql_query($sql);
while ($row = mysql_fetch_object($res)) {
    $checked[] = $row->city_id;
}

// this would be templated in a real world situation
foreach ($cities as $id => $name) {
    $checked = "";
    // check box if user has selected this city
    if (in_array($checked, $id)) {
        $checked = 'checked="checked" ';
    }
    echo '<input type="checkbox" name="city[]" value="'.$id.'" '.$checked.'/>';
}
Sign up to request clarification or add additional context in comments.

Comments

1

If I understand you question properly, the obvious and simplest approach is that you need to fetch records from database and when producing HTML [in a loop ot something similar] check if that value exists in array to results. You haven't given us any examples of your DB structure or code, so you must figure it our yourself how to do it.

Comments

0

Usually, you insert the values into the database. After inserting, you should have access to the same values again. It's not clear how you set up your script, so let's assume you redirect to another script.

What you need to do is retrieve the values for the checkboxes from your database again. Then you know which are selected. This can be used to determine if your checkbox need to be checked or not.

Note:

  • I assume here that the result of your query is an array with the selected Ids as a value.
  • I assume here that your fields are stored in the result of some query and is basically an array with Field Id as key and Field Name as Value.

E.g., something like this:

<?php
// Retrieve values from database.
$resultArray = ... some code ... ;

?>

<?php foreach ($field_types as $field_name => $field_value): ?>
<input type="checkbox" name="<?php echo $field_name; ?>" value="<?php echo $field_value ?>" <?php if (in_array($field_name, $resultArray)) { echo 'checked'; }/>
<?php endforeach; ?>

This results in a checkbox which is checked, if the field_name is inside the result array (with your already checked results). Otherwise they're just rendered as unchecked checkboxes. Hope this is clear enough.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.