Page 1 of 1
Re: pre fill checkboxes from database
Posted: Thu Oct 09, 2014 4:25 pm
by Celauran
Code: Select all
// Loop through the array of checked box values ...
$features="";
$flag=0;
foreach($checkboxl as $entry){
$features .= $entry."|";
$flag=1;
}
if($flag==1){
$features=rtrim($features);
}
You're better off using a pivot table here. You don't want more than one value in one column.
Re: pre fill checkboxes from database
Posted: Thu Oct 09, 2014 5:01 pm
by Celauran
Depends on the number of features we're talking about. If it's relatively small, you could have one column per feature with a binary value. If it's many, you could list features in their own table and use a pivot to join listings and features. For example:
Code: Select all
mysql> SELECT id, title, slug FROM products LIMIT 1;
+--------------------------------------+-------------------------------------+------------------------------+
| id | title | slug |
+--------------------------------------+-------------------------------------+------------------------------+
| 292abfce-764d-498c-9515-c84cfb46c23c | Some product title | some-product |
+--------------------------------------+-------------------------------------+------------------------------+
mysql> SELECT id, title, path FROM media LIMIT 1;
+--------------------------------------+-------------------------+--------------------------------------------------+
| id | title | path |
+--------------------------------------+-------------------------+--------------------------------------------------+
| 298607e3-ce63-463e-ae95-4f49d930353d | Some media file | files/media/4719eb7fb7d634b921ac5517f7fecc1a.jpg |
+--------------------------------------+-------------------------+--------------------------------------------------+
mysql> SELECT * FROM media_products;
+--------------------------------------+--------------------------------------+--------------------------------------+
| id | media_id | product_id |
+--------------------------------------+--------------------------------------+--------------------------------------+
| 040f9477-f71a-4fa1-96a0-fb70669bc910 | 298607e3-ce63-463e-ae95-4f49d930353d | 292abfce-764d-498c-9515-c84cfb46c23c |
+--------------------------------------+--------------------------------------+--------------------------------------+
Re: pre fill checkboxes from database
Posted: Thu Oct 09, 2014 5:25 pm
by Celauran
What about this?
Code: Select all
<input type="checkbox" name="absbrakes" <?php echo ($row['absbrakes'] == 'yes') ? 'checked="checked"' : ''; ?>">ABS Brakes
Re: pre fill checkboxes from database
Posted: Fri Oct 10, 2014 7:16 am
by Celauran
Pretty much. You've already retrieved the row from the DB and you mentioned you were using yes/no, so it checks if the value of that column is 'yes' and checks the box if so. Otherwise, do nothing.
Re: pre fill checkboxes from database
Posted: Fri Oct 10, 2014 9:53 am
by Celauran
That's the default value for checkboxes. You'll need to apply some logic to set yes/no prior to updating your database. You can also add value="yes" to your checkboxes to have half of that handled for you. You'll still need to logic for 'no', though.