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

CodeIgniter: “Cannot redeclare class”
How to check if any number exists in a word
Retrieving $_GET gives error unexpected T_VARIABLE, expecting ',' or ';'
Symfony2 How to pass in $year, $month variables to Twig without hardcoding first?
How can I change this Html Form to Yii framework
How to get log() of a very big number (PHP)?
command line data curl convert to array
Display content based on date and time
How to give a custom name to the PDF being downloaded using FPDF library?
DNS with subdomain doesn't work properly
SMPP: the UDH (User Data Header) appears in the body of sms
Compare 2 arrays and retrieve the differences using PHP
wordpress making a static and dynamic link
for each if true fetch mysqli result
PHP, CodeIgniter, MySQL - SELECT - overloading database
How do I enable member login to a specific directory including sub-directories in wordpress

Categories

HOME
keyboard
haproxy
filesize
odata
twitter-fabric
yql
liferay-7
qooxdoo
facebook-android-sdk
sentry
sympy
static-analysis
polymer-2.x
reportviewer
erd
responsivevoice
fresco
phonegap-build
uima
user-defined-types
robolectric
http-authentication
parceler
pfobject
candlestick-chart
websphere-portal
tortoisemerge
webviewclient
ccavenue
react-bootstrap-table
miniprofiler
sendmessage
gsp
nppexec
formsauthenticationticket
python-sounddevice
resampling
jqgrid-asp.net
latexml
ipp-protocol
liteide
qvtkwidget
sonarqube5.2
webalizer
return-type
brightscript
http-basic-authentication
gapi
controllers
dmarc
static-methods
flipclock
outlook.com
decidable
wso2ml
postal-code
modern.ie
karma-coverage
directorysearcher
communicate
coding-efficiency
screwturn
stripe.net
incognito-mode
fileshare
meteor-collections
air-native-extension
jms-serializer
profiles
quickgraph
ember-table
ax
image-formats
algol68
rtd
ognl
servlet-container
qcar-sdk
odac
self-tracking-entities
.net-services
mysqli-multi-query
dm
downcasting
accumulator
rakudo
watchpoint
isapi-redirect
remote-working
downloadfile
community-server

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