php


CodeIgniter Marking checkboxes from database


I want to repopulate a page that contains checkboxes from values in the database. Currently, they are being stored as a string that gets split into an array when processing e.g 3,4,5.
I am able to check the boxes with the data from my database but another empty checkbox is created by its side. How do I only check the boxes with data from the database without having a duplicate empty box by its side?
<tbody>
<?php
if(!empty($datatable)){
foreach ($datatable as $data){
?>
<tr>
<td>
<?php foreach ($event_contacts as $checked){?>
<?php if($checked==$data->id){?>
<input type="checkbox" name="id[]" id="id" checked value="<?php echo $data->id; ?>"/>
<?php }else{ ?>
<input type="checkbox" name="id[]" id="id" value="<?php echo $data->id; ?>"/>
<?php }}?>
</td>
<td><?php echo $data->first_name." ".$data->last_name; ?></td>
<td><?php echo $data->email; ?></td>
<td><?php echo $data->phone_number; ?></td>
<td><?php echo $data->address;?></td>
</tr>
<?php
}
}
?>
</tbody>
Hope someone can help. Thanks
EDIT: Added photo for clarity
just change your code like this
<tbody>
<?php
if(!empty($datatable)){
foreach ($datatable as $data){
?>
<tr>
<td>
<?php
if (in_array($data->id, $event_contacts)) {
?>
<input type="checkbox" name="id[]" id="id" checked value="<?php echo $data->id; ?>"/>
<?php
}else{
?>
<input type="checkbox" name="id[]" id="id" value="<?php echo $data->id; ?>"/>
<?php
}
?>
</td>
<td><?php echo $data->first_name." ".$data->last_name; ?></td>
<td><?php echo $data->email; ?></td>
<td><?php echo $data->phone_number; ?></td>
<td><?php echo $data->address;?></td>
</tr>
<?php
}
}
?>
</tbody>

Related Links

Including files from different folders include (“./”.$_GET['id'].“.php”);
How to resolve action queues and simulate real time?
Catching Different Number Types in Strings by Regex in PHP
Generate a file by PHP
Wordpress - Pass parameter to a wordpress page with ajax
PDO <-> SQL Server exec (in Laravel) returns empty array
Automatically create a Wordpress gallery on a specific post from a Dropbox account
Displaying Notifications Logic
Common view template files with Wiredesignz HMVC & CodeIgniter
Define Laravel 5 route inside subfolder and display it via controller
How to delete a particular message from openfire using PHP
Redirect form data in encrypted form to another domain
How to upload to YouTube in PHP as Server to Server transaction?
Php Soap Client equivalent in NodeJS
How to return and use variable from function? [duplicate]
How to get the image path from data URL

Categories

HOME
ionic2
lambda
clearcase
paypal-ipn
redmine
concurrency
swi-prolog
magento-2.0
telegraf
timber
game-maker
smooks
xbap
pushdown-automaton
sonata
trace32
exec-maven-plugin
xsl-fo
home
software-packaging
dragula
python-appium
jackrabbit-oak
onchange
python-imageio
spring-annotations
monitor
winsock
objectmapper
gsp
ms-media-foundation
apptentive
spring-rabbitmq
busboy
nppexec
pdfnet
fastlane
swfupload
contactless-smartcard
jenkins-slave
return-type
embedded-v8
freshdesk
popen
word-2016
ltrace
instafeedjs
http4s
qtextedit
livecycle
maven-release-plugin
right-to-left
bbc-micro
noclassdeffounderror
xcopy
preferenceactivity
ibm-was
astropy
supersocket.net
ngcordova
swagger-maven-plugin
google-gdk
mobilefirst-server
encryption-symmetric
dc
global-scope
multivariate-testing
visual-c++-2012
commenting
sqlbindparameter
spring-social-linkedin
autonumber
application-server
mp4parser
jacob
bcdedit
jquery-dialog
spark-view-engine
arrow
websphere-6.1
nsfont
explicit
principles
self-tracking-entities
spring-modules
wmd-editor
gtktextview
ntruencrypt
downloadfile
rootkit

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App