1

I`m using codeigniter to build small website. I am curious how to populate checkbox from database ?

Let`s say I have the following query;

$this->db->select('status');
$this->db->where('id', 3');
$this->db->get('table);

How to make checkbox to be checked if the result of the query above is 1 ?

1
  • 1
    try <input type='checkbox' checked='checked' /> i think you are asking how to make the checkbox checked based on the db value. Commented May 3, 2011 at 16:34

4 Answers 4

4

You can do something like this. It's just an idea since I can't really see EXACTLY what you're returning. $entry['status'] is the result from your query.

if($entry['status'] == 1){
                echo '<input type="checkbox" checked="checked" disabled="disabled"/>';
                    }
else {
                echo '<input type="checkbox" unchecked="unchecked" disabled="disabled"/>';
            }
Sign up to request clarification or add additional context in comments.

Comments

1

Take a look at the form_checkbox() section of the codeigniter form helper documentation

Comments

0

If you want to have the select box checked if the status value is greater than 1 you could do something like this.

$result = $this->db->select('status')->get_where('table', array('id'=> 3))->row();
if($result->status > 1)
{
print '<input type="checkbox" name="checkbox" checked="checked" value="'.$result->status.'" />';
}else{
print '<input type="checkbox" name="checkbox" value="'.$result->status.'" />';
}

You'll want to check that the row actually returns values or you'll get a var on a non object error. This is just an example.

6 Comments

Why would it ever even need to be greater than 1?
His question was - "How to make checkbox to be checked if the result of the query above is 1?"
@mhiggins Well I couldnt see why you would have it greater than 1 since they are either 1 or 0 (on or off). no?
No. Checkboxes can be any value... it's true they have an off and on state but at the on state you could assign anything to its value. Additionally, he wants to read the value of "status" out of the database. That could hold anything as well so we're assuming it's numeric and has values greater than 1.
@mhiggins Yeah any value, but you would store their value in a column in a database table. The actual cell value, what you're returning is a boolean value.
|
0

This works for me and is simple:

    <?php foreach($tb as $table_row){ ?>
    <input type="checkbox" <?php if ($table_row['reconciled'] == "1" {echo "checked = checked";} ?>

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.