Forum Moderators: coopster

Message Too Old, No Replies

Form Checkboxes

Checking certain boxes based on DB

         

createErrorMsg

10:40 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm working on an edit page for a cms script, and am having a bit of trouble figuring out a way to check checkboxes.

One set of information on the entry form involves checking checkboxes for categories that apply to the entry. gathering and storing that information is no problem, but calling it up and using it to rebuild the filled in form is.

Here's the set up:

For any checkbox checked, an integer value is entered into the database. I want to pull those integer values from the database and use them to check the same checkboxes on the edit form. This way users can see what categories were selected and change them if need be.

Here's the code for the checkboxes...

<fieldset class="checks">
<legend>Select categories for this widget...</legend>
<label>Widget One<input type="checkbox" name="cats[]" value="1" /></label>
<label>Widget Two<input type="checkbox" name="cats[]" value="2" /></label>
<label>Widget Three<input type="checkbox" name="cats[]" value="3" /></label>
<label>Widget Four<input type="checkbox" name="cats[]" value="4" /></label>
</fieldset>

So if the DB record said that Widget A was in categories 1 and 3, I need to pull those values and somehow use them to add a checked="checked" to the Widget One and Widget Three <input> tags before outputting them to the page, but also output the other <input> tags check-less.

I thought I could run a loop a number of times equal to the number of categories, outputting an <input> tag each time with a value equal to the iteration, checking the DB results against the iteration value to decide whether or not to check the box, but I can't make it compare against several numbers on each loop (i.e., on loop one, check three numbers from the database against the interation of "1" to see if one of the three DB numbers is a 1). If it's just one category, fine, but most of the widgets are multi-categorical, so this solution leads to a dead end.

Does anyone know of a way to loop through and output <input> tags as either checked or not checked based on a DB query result. Again, it's not the query part I need help with, just turning the results into a series of checked or unchecked boxes based on the results.

Any responses would be greatly appreciated.

cEM

coopster

12:55 am on Dec 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hi cEM, how about loading an array with the database values, then using PHP's in_array() [php.net] function to determine whether or not to check the box? Pseudocode:
  1. Query the database, get result set of checkboxes
  2. loop through result set, building an array of values
  3. Execute loop, building checkboxes, and for each item that has a value in the array, check the box.

createErrorMsg

2:35 am on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



coopster, that's perfect!

I had just worked out a function to seperately loop through the db array values and return a 'true' if it was there, but it was stopping after it hit the first array value and not checking the others.

Your in_array() solution worked perfectly the first time.

I keep finding myself amazed at the seemingly complex things that PHP has a simple function for. Thanks a billion for pointing it out.

cEM

coopster

2:49 am on Dec 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Good for you. It's beautiful, isn't it ;)