pre fill checkboxes from database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: pre fill checkboxes from database

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: pre fill checkboxes from database

Post 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 |
+--------------------------------------+--------------------------------------+--------------------------------------+
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: pre fill checkboxes from database

Post by Celauran »

What about this?

Code: Select all

<input type="checkbox" name="absbrakes" <?php echo ($row['absbrakes'] == 'yes') ? 'checked="checked"' : ''; ?>">ABS Brakes
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: pre fill checkboxes from database

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: pre fill checkboxes from database

Post 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.
Post Reply