1

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!!

1
  • 2
    Storing multiple values in a single field is invariably a bad idea, and a bad design. You should normalize things and store those values in a separate table. As is, your "check" code in PHP will tick off a checkbox if a '2' appears in your field ANYWHERE. So a value of "12,22" would check off the '2' as well. Commented Aug 17, 2011 at 21:13

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent comment!!! Great stuff!! This really helps a lot!! I am such a visual guy, that this really did it for me. Thanks!
0

You can use it as shown below it could be helpful

if (in_array("2", $row_Recordset2['r_category'])) {echo "Checked";} 

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.