Reputation: 1224
I have a form that has multiple categories. They are separated by checkboxes, so one could check any number of categories that their post/story will be in.
The values are stored inside a db row separating them by commas while INSERT
GetSQLValueString(implode($_POST['r_category'],", "), "text"),
So in the database I could have an entry that looks like this:
2,3,6,12
Which those are category numbers.
I am now building an edit form feature to use to Edit and UPDATE these records. I can not find a way to "EXPLODE" the values into checkboxes on the edit form??
So if you log in and go to the ENTRY that you want to edit for example, I have Checkboxes separating the Categories, so it will be easy to check and uncheck to add and not add... I have tried everything I could to get the values to POST and be checked when viewing this Edit Form.
Here is an example of one of my Category Check boxes on my edit form page, but it is not working when I have multiple Categories, like the example above.. 2,3,6,12 ?? :
<input type="checkbox" name="r_category[]"
<?php if (!(strcmp("2", $row_Recordset2['r_category']))) {
echo "checked=\"checked\"";} ?> value="2" />Cat 2
Thanks in advance NINJAS!!
Upvotes: 1
Views: 4585
Reputation: 719
That in_array conditional never did work for me. I devised a totally front-end solution instead that works like a charm and automatically updates itself based on changes to the database. If you're interested, check it out here. https://stackoverflow.com/a/26433841/1896348
Upvotes: 0
Reputation: 1
You can use it as shown below it could be helpful
if (in_array("2", $row_Recordset2['r_category'])) {echo "Checked";}
Upvotes: 0
Reputation:
I can not find a way to "EXPLODE" the values into checkboxes on the edit form
This line is so close to the answer that I'm seriously wondering if this is a joke post.
Given your database structure as it is, you can use the explode()
function to break the value into an array based on a delimiter. For example:
$values = "1,2,3,4";
$array_of_values = explode(",", $values);
After this, $array_of_values
will contain an array of four elements with the values of 1
, 2
, 3
, and 4
respectively.
When deciding whether or not to display the checkbox as checked, you can use the in_array()
function to make that call:
if (in_array("2", $array_of_values)) {
echo 'checked="checked"';
}
Beyond that, let me take this opportunity to second what @MarkB said in the comments on this question: it's a bad idea to hold multiple values in a single database field. It makes searching for things harder, it makes adding values harder... it just makes everything more complex than it needs to be.
The best way to handle this would be to have a many-to-many table which stores rows of items and categories, with one row for each category that an item belongs to.
In other words, where you currently have this:
Item | Category
---------------
123 | 1,2,3,4
You would instead have a table that looks like this:
Item | Category
---------------
123 | 1
123 | 2
123 | 3
123 | 4
Upvotes: 5