Forum Moderators: coopster
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
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