suttercain Posted April 12, 2007 Author Share Posted April 12, 2007 Thanks Boo, I won't be able to check until Monday at work. But I will update this post then. Have a great weekend! Shannon Link to comment https://forums.phpfreaks.com/topic/46730-possible-to-populate-a-checkbox-from-a-mysql-database/page/2/#findComment-228052 Share on other sites More sharing options...
HeyRay2 Posted April 13, 2007 Share Posted April 13, 2007 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 https://forums.phpfreaks.com/topic/46730-possible-to-populate-a-checkbox-from-a-mysql-database/page/2/#findComment-228065 Share on other sites More sharing options...
boo_lolly Posted April 13, 2007 Share Posted April 13, 2007 great suggestion HeyRay. Link to comment https://forums.phpfreaks.com/topic/46730-possible-to-populate-a-checkbox-from-a-mysql-database/page/2/#findComment-228217 Share on other sites More sharing options...
suttercain Posted April 16, 2007 Author Share Posted April 16, 2007 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 https://forums.phpfreaks.com/topic/46730-possible-to-populate-a-checkbox-from-a-mysql-database/page/2/#findComment-230580 Share on other sites More sharing options...
boo_lolly Posted April 16, 2007 Share Posted April 16, 2007 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 https://forums.phpfreaks.com/topic/46730-possible-to-populate-a-checkbox-from-a-mysql-database/page/2/#findComment-230589 Share on other sites More sharing options...
suttercain Posted April 16, 2007 Author Share Posted April 16, 2007 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 https://forums.phpfreaks.com/topic/46730-possible-to-populate-a-checkbox-from-a-mysql-database/page/2/#findComment-230602 Share on other sites More sharing options...
boo_lolly Posted April 16, 2007 Share Posted April 16, 2007 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 https://forums.phpfreaks.com/topic/46730-possible-to-populate-a-checkbox-from-a-mysql-database/page/2/#findComment-230608 Share on other sites More sharing options...
suttercain Posted April 16, 2007 Author Share Posted April 16, 2007 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 https://forums.phpfreaks.com/topic/46730-possible-to-populate-a-checkbox-from-a-mysql-database/page/2/#findComment-230622 Share on other sites More sharing options...
Barand Posted April 16, 2007 Share Posted April 16, 2007 <?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 https://forums.phpfreaks.com/topic/46730-possible-to-populate-a-checkbox-from-a-mysql-database/page/2/#findComment-230629 Share on other sites More sharing options...
boo_lolly Posted April 16, 2007 Share Posted April 16, 2007 Barand... what's the difference? Link to comment https://forums.phpfreaks.com/topic/46730-possible-to-populate-a-checkbox-from-a-mysql-database/page/2/#findComment-230638 Share on other sites More sharing options...
Barand Posted April 16, 2007 Share Posted April 16, 2007 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 https://forums.phpfreaks.com/topic/46730-possible-to-populate-a-checkbox-from-a-mysql-database/page/2/#findComment-230651 Share on other sites More sharing options...
suttercain Posted April 16, 2007 Author Share Posted April 16, 2007 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 https://forums.phpfreaks.com/topic/46730-possible-to-populate-a-checkbox-from-a-mysql-database/page/2/#findComment-230653 Share on other sites More sharing options...
Barand Posted April 16, 2007 Share Posted April 16, 2007 @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 Link to comment https://forums.phpfreaks.com/topic/46730-possible-to-populate-a-checkbox-from-a-mysql-database/page/2/#findComment-230658 Share on other sites More sharing options...
boo_lolly Posted April 16, 2007 Share Posted April 16, 2007 @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 https://forums.phpfreaks.com/topic/46730-possible-to-populate-a-checkbox-from-a-mysql-database/page/2/#findComment-230670 Share on other sites More sharing options...
per1os Posted April 16, 2007 Share Posted April 16, 2007 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 https://forums.phpfreaks.com/topic/46730-possible-to-populate-a-checkbox-from-a-mysql-database/page/2/#findComment-230681 Share on other sites More sharing options...
suttercain Posted April 16, 2007 Author Share Posted April 16, 2007 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 https://forums.phpfreaks.com/topic/46730-possible-to-populate-a-checkbox-from-a-mysql-database/page/2/#findComment-230685 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.