You're better off using a pivot table here. You don't want more than one value in one column.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); }
pre fill checkboxes from database
Moderator: General Moderators
Re: pre fill checkboxes from database
Re: pre fill checkboxes from database
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
What about this?
Code: Select all
<input type="checkbox" name="absbrakes" <?php echo ($row['absbrakes'] == 'yes') ? 'checked="checked"' : ''; ?>">ABS BrakesRe: pre fill checkboxes from database
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
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.