Jump to content

Possible to populate a "checkbox from a mysql database?


suttercain

Recommended Posts

boo_lolly's suggestion should work well.

 

Another option is that you could use your "vin" field and set it as UNIQUE in your database, as I'm assuming that you'll not have two rows in your db with the same vin number. This could be used instead of a "record_id" field and would provide a quick way to distinguish rows.

 

Also, when updating your db to reflect which "letter_sent" checkboxes are checked, you can save quite a few db calls by consildating boo_lolly's code into a single query without the need for a loop.

 

Try changing:

 

<?php
foreach($_POST['checkboxes'] as $key => $val){
	$sql = "UPDATE canada SET letter_sent = '1' WHERE id = '$val'";
}
?>

 

to read:

 

<?php
$sql = "UPDATE canada SET letter_sent = '1' WHERE id IN ('" . implode(",", $_POST['checkboxes']). "')";
?>

Link to comment
Share on other sites

Hey Boo,

 

I am back at work today and I am still trying to tackle this issue.

 

I have made the above suggested change but for some reason the check boxes do not load checked even if it is !NULL in the database.

 

The code I tried this morning:

<?php
while($row = mysql_fetch_array($result)) {
	echo "
		<tr>
		<td>" . $row['first_name'] . " " . $row['last_name'] . "</td>
		<td>" . $row['vehicle_year'] . "</td>
		<td>" . $row['vehicle_make'] . "</td>
		<td>" . $row['vehicle_model'] . "</td>
		<td>" . $row['vin'] . "</td>
		<td>" . $row['date'] . "</td>
		<td><input name=\"checkboxes[]\" type=\"checkbox\" value=\"{$row['record_id']}\"";
                        (($row['letter_sent'] == 1) ? (" checked") : (""));
                echo "></td>
            </tr>
	";
        }
?>

 

I'll try to illustrate the set up also, right now I only have 4 rows in the database for testing:

Browser Side:

NAME                    LETTER SENT (CHECKBOX COLUMN)

a                          [ ] (Unchecked)

b                          [ ] (Unchecked)

c                          [ ] (Unchecked)

d                          [ ] (Unchecked)

 

MySQL DATABSE

name                    letter_sent

a                          NULL

b                          1

c                          NULL

d                          1

 

So it is my understanding that when I view the data through the browser, rows b and d should be checked. But For some reason it's not doing it.

 

Thanks for any help or suggestions.

 

Also thank you Hey Rey for the UPDATE query.

 

 

 

 

Link to comment
Share on other sites

you know, i've been having some problems with that, as well. if you look online for some HTML checkbox tutorials, you'll find that there are two ways of doing it:

<input type="checkbox" name="checked[]" checked="checked">

 

and:

<input type="checkbox" name="checked[]" CHECKED>

 

try the first way... i don't understand why HTML would have changed this standard...

Link to comment
Share on other sites

Hi Boo, thanks for such the quick reply,

 

I just tried:

<td><input name=\"checkboxes[]\" checked=\"checked\" type=\"checkbox\" value=\"{$row['record_id']}\"";
                        (($row['letter_sent'] == 1) ? (" checked") : (""));
                echo "></td>

And it echoed all my checkboxes as checked, even the null fields from MySQL. Same with the second suggestion you made.

 

I am starting to wonder if this is possible to do with multiple check boxes. It's kind of like an issue I had with the dropdowns and sessions. When using sessions I can never have the dropdown stay selected, it always resets.

 

Thank you for your help Boo.

 

 

Link to comment
Share on other sites

sorry i didn't clarify. this is what i mean:

         echo "<td><input name=\"checkboxes[]\" type=\"checkbox\" value=\"{$row['record_id']}\"";
                (($row['letter_sent'] == 1) ? (" checked=\"checked\"") : (""));
         echo "></td>

 

that should work.

Link to comment
Share on other sites

Hi Boo,

 

Sorry my bad. I tried:

 

<?php
while($row = mysql_fetch_array($result)) {
	echo "
		<tr>
		<td>" . $row['first_name'] . " " . $row['last_name'] . "</td>
		<td>" . $row['vehicle_year'] . "</td>
		<td>" . $row['vehicle_make'] . "</td>
		<td>" . $row['vehicle_model'] . "</td>
		<td>" . $row['vin'] . "</td>
		<td>" . $row['date'] . "</td>
		<td><input name=\"checkboxes[]\" type=\"checkbox\" value=\"{$row['record_id']}\"";
		(($row['letter_sent'] == 1) ? (" checked=\"checked\"") : (""));
            echo "</tr>
	";
        }
?>

 

and also with:

 

(($row['letter_sent'] == 1) ? (" CHECKBOX") : (""));

 

It echoed all the results as unchecked.

 

Link to comment
Share on other sites

<?php

while($row = mysql_fetch_array($result)) {
    $id = $row['record_id'];
    $chk =  $row['letter_sent'] == 1 ? 'checked' : '';
    echo "<tr>
        <td>" . $row['first_name'] . " " . $row['$last_name'] . "</td>
        <td>" . $row['vehicle_year'] . "</td>
        <td>" . $row['vehicle_make'] . "</td>
        <td>" . $row['vehicle_model'] . "</td>
        <td>" . $row['vin'] . "</td>
<td><input name='letter_sent[]' type='checkbox' value='$id' $chk /></td>
      </tr>";

}


?>

Link to comment
Share on other sites

I find

 

<td><input name='letter_sent[]' type='checkbox' value='$id' $chk /></td>

 

a lot easier to read than

 

<td><input name=\"checkboxes[]\" type=\"checkbox\" value=\"{$row['record_id']}\"";
		(($row['letter_sent'] == 1) ? (" checked=\"checked\"") : (""));

 

and

 

name='letter_sent[]'

 

but maybe it should be "checkboxes[]"

 

I've been reading through the previous posts but hard to follow as I don't think two consecutive posts have consistent names. But as a rule it's easier to keep a handle on things if you keep form field names aligned with table column names

Link to comment
Share on other sites

Hi Barrand and Boo,

 

That seems to be working. I ran:

while($row = mysql_fetch_array($result)) {
    $id = $row['record_id'];
    $chk =  $row['letter_sent'] == 1 ? 'checked' : '';
    echo "<tr>
        <td>" . $row['first_name'] . " " . $row['$last_name'] . "</td>
        <td>" . $row['vehicle_year'] . "</td>
        <td>" . $row['vehicle_make'] . "</td>
        <td>" . $row['vehicle_model'] . "</td>
        <td>" . $row['vin'] . "</td>
<td><input name='letter_sent[]' type='checkbox' value='$id' $chk /></td>
      </tr>";

}

 

and it displays all non null as checked and null as unchecked.

 

I will try to attempt the update in a short while.

 

Thank you both very much for your help and time.

 

SC

Link to comment
Share on other sites

@Boo

 

Re reading your code again, the second bit doesn't output anything

<td><input name=\"checkboxes[]\" type=\"checkbox\" value=\"{$row['record_id']}\""; // echo ends here
		(($row['letter_sent'] == 1) ? (" checked=\"checked\"") : (""));         // does nothing

 

that's what i was afraid of... so, i asked suttercain to make sure that the column 'letter_sent' was TINYINT. this could be the cause of the condition not working the way we want, but i'm pretty sure he's confirmed that it's set to an INT. if this is the case, what else could be the source of the condition not working the way we want?

Link to comment
Share on other sites

I think what barand was saying is that your logic is incorrect. if you want it to be the way you have it either barands method (my preference) is the best way to go but maybe this will clear it up for you:

 

<td><input name=\"checkboxes[]\" type=\"checkbox\" value=\"{$row['record_id']}\""; // echo ends here
		echo ($row['letter_sent'] == 1) ? " checked=\"checked\"" : "";         // needs to echo statement on this line also to echo the statement
echo "/>"; // close input

 

But I prefer barands method, much cleaner and easier.

Link to comment
Share on other sites

that's what i was afraid of... so, i asked suttercain to make sure that the column 'letter_sent' was TINYINT. this could be the cause of the condition not working the way we want, but i'm pretty sure he's confirmed that it's set to an INT.

 

Yes, it is a tinyint.

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.