PDA

View Full Version : Checkboxes and one field in the DB


kona72
11-21-2006, 05:15 PM
Hi All,

I am developing a site where businesses can sign up and be associated with a number of different categories. The DB has one field called 'category' and there fore I thought I could just do something like this....

<td class="main_text"><p> <input name="category" type="checkbox" id="category" value="appraisers">
Appraisers<br>
<input name="category" type="checkbox" id="category" value="architects">
Architects<br>
<input name="category" type="checkbox" id="category" value="basement contractors">
Basement Contractors<br>
<input name="category" type="checkbox" id="category" value="bathroom remodeling">
Bathroom Remodeling<br>
<input name="category" type="checkbox" id="category" value="building supplies retail">
Building Supplies Retail<br>
<input name="category" type="checkbox" id="category" value="building inspections">
Building Inspections<br>
<input name="category" type="checkbox" id="category" value="carpets rugs retail">
Carpets Rugs Retail<br>
<input name="category" type="checkbox" id="category" value="ceramic tile">
Ceramic Tile<br>
<input name="category" type="checkbox" id="category" value="concrete">
Concrete<br>
<input name="category" type="checkbox" id="category" value="contractors">
Contractors<br>
<input name="category" type="checkbox" id="category" value="decks fences">
Decks Fences<br>
<input name="category" type="checkbox" id="category" value="draperies">
Draperies<br>
<input name="category" type="checkbox" id="category" value="eavestroughing">
Eavestroughing<br>
<input name="category" type="checkbox" id="category" value="electrical">
Electrical<br>
<input name="category" type="checkbox" id="category" value="extermination">
Extermination<br>
<input name="category" type="checkbox" id="category" value="fireplaces">
Fireplaces<br>
<input name="category" type="checkbox" id="category" value="floor covering">
Floor Covering<br>
<input name="category2" type="checkbox" id="category" value="foundations">
Foundations<br>
<input name="category" type="checkbox" id="category" value="furnaces air conditioning plumbing">
Furnaces,Air Conditioning,Plumbing<br>
<input name="category" type="checkbox" id="category" value="garage door">
Garage Door<br>
<input name="category" type="checkbox" id="category" value="glass">
Glass<br>
<input name="category" type="checkbox" id="category" value="hauling service">
Hauling Service<br>
<input name="category" type="checkbox" id="category" value="heating contractors">
Heating Contractors<br>
<input name="category" type="checkbox" id="category" value="home inspectors">
Home Inspectors<br>
<input name="category" type="checkbox" id="category" value="home builders">
Home Builders<br>
<input name="category" type="checkbox" id="category" value="interior design">
Interior Design<br>
<input name="category" type="checkbox" id="category" value="kitchen cabinets">
Kitchen Cabinets<br>
<input name="category" type="checkbox" id="category" value="landscaping">
Landscaping<br>
<input name="category" type="checkbox" id="category" value="lawn and garden sprinklers">
Lawn and Garden Sprinklers<br>
<input name="category" type="checkbox" id="category" value="lawn maintenance">
Lawn Maintenance<br>
<input name="category" type="checkbox" id="category" value="lumber">
Lumber<br>
<input name="category" type="checkbox" id="category" value="mechanical">
Mechanical<br>
<input name="category" type="checkbox" id="category" value="mortgages">
Mortgages<br>
<input name="category" type="checkbox" id="category" value="painting">
Painting<br>
<input name="category" type="checkbox" id="category" value="plumbing">
Plumbing<br>
<input name="category" type="checkbox" id="category" value="railings">
Railings<br>
<input name="category" type="checkbox" id="category" value="renovations">
Renovations<br>
<input name="category" type="checkbox" id="category" value="roofing">
Roofing<br>
<input name="category" type="checkbox" id="category" value="security systems">
Security Systems<br>
<input name="category" type="checkbox" id="category" value="siding">
Siding<br>
<input name="category" type="checkbox" id="category" value="stucco">
Stucco<br>
<input name="category" type="checkbox" id="category" value="tree service">
Tree Service<br>
<input name="category" type="checkbox" id="category" value="video surveillance">
Video Surveillance<br>
<input name="category" type="checkbox" id="category" value="windows">
Windows<br>
<input name="category" type="checkbox" id="category" value="wood workers">
Wood Workers<br>
</td>

Alas when a new user signs up it only records the last check box checked. Is there anyway i can have all the checked boxes populate the 'category' field?

Thanks!!

lux
11-21-2006, 06:21 PM
If i understand you want to insert more than one value into a single field. This won't be possible.

I would suggest that you create a field for each of the options in a table and insert the value of the checkbox into that field, i.e. true (1) if the checkbox is selected or false (0) if it is not selected.

I can give you some more help with inserting the values of the checkboxes into the database if you would like to follow my suggestion?

davidj
11-22-2006, 06:35 PM
ok

If i understand correctly you could do something like this


<input name="category[]" type="checkbox" id=="category[]"
<input name="category[]" type="checkbox" id=="category[]"
<input name="category[]" type="checkbox" id=="category[]"


This will force an array to be initialised

so if you have 20 <input fields you can catch all of them using 1 POST

$category = $_POST['catagory']; // << this will catch every checkbox value thats checked and store the array in $category

when you want to target a value from the 1 $category var you just use arraykeys

so to get the first value just use > $category[0];
and the 5th > $category[4];

kona72
11-24-2006, 10:33 PM
ok

If i understand correctly you could do something like this


<input name="category[]" type="checkbox" id=="category[]"
<input name="category[]" type="checkbox" id=="category[]"
<input name="category[]" type="checkbox" id=="category[]"


This will force an array to be initialised

so if you have 20 <input fields you can catch all of them using 1 POST

$category = $_POST['catagory']; // << this will catch every checkbox value thats checked and store the array in $category

when you want to target a value from the 1 $category var you just use arraykeys

so to get the first value just use > $category[0];
and the 5th > $category[4];

So far so good.. The only problem now is that the field just has the actual word Array in it rather than having all the categories i checked off.... Maybe i should be using a join. Too bad i don't know how to .....

Here is what i have now....

<td class="main_text"><p> <input name="category[]" type="checkbox" id=="category[]" value="appraisers">
Appraisers<br>
<input name="category[]" type="checkbox" id=="category[]" value="architects">
Architects<br>
<input name="category[]" type="checkbox" id=="category[]" value="basement contractors">
Basement Contractors<br>
<input name="category[]" type="checkbox" id=="category[]" value="bathroom remodeling">
Bathroom Remodeling<br>
<input name="category[]" type="checkbox" id=="category[]" value="building supplies retail">
Building Supplies Retail<br>
<input name="category[]" type="checkbox" id=="category[]" value="building inspections">
Building Inspections<br>
<input name="category[]" type="checkbox" id=="category[]" value="carpets rugs retail">
Carpets Rugs Retail<br>
<input name="category[]" type="checkbox" id=="category[]" value="ceramic tile">
Ceramic Tile<br>
<input name="category[]" type="checkbox" id=="category[]" value="concrete">
Concrete<br>
<input name="category[]" type="checkbox" id=="category[]" value="contractors">
Contractors<br>
<input name="category[]" type="checkbox" id=="category[]" value="decks fences">
Decks Fences<br>
<input name="category[]" type="checkbox" id=="category[]" value="draperies">
Draperies<br>
<input name="category[]" type="checkbox" id=="category[]" value="eavestroughing">
Eavestroughing<br>
<input name="category[]" type="checkbox" id=="category[]" value="electrical">
Electrical<br>
<input name="category[]" type="checkbox" id=="category[]" value="extermination">
Extermination<br>
<input name="category[]" type="checkbox" id=="category[]" value="fireplaces">
Fireplaces<br>
<input name="category[]" type="checkbox" id=="category[]" value="floor covering">
Floor Covering<br>
<input name="category2" type="checkbox" id="category" value="foundations">
Foundations<br>
<input name="category[]" type="checkbox" id=="category[]" value="furnaces air conditioning plumbing">
Furnaces,Air Conditioning,Plumbing<br>
<input name="category[]" type="checkbox" id=="category[]" value="garage door">
Garage Door<br>
<input name="category[]" type="checkbox" id=="category[]" value="glass">
Glass<br>
<input name="category[]" type="checkbox" id=="category[]" value="hauling service">
Hauling Service<br>
<input name="category[]" type="checkbox" id=="category[]" value="heating contractors">
Heating Contractors<br>
<input name="category[]" type="checkbox" id=="category[]" value="home inspectors">
Home Inspectors<br>
<input name="category[]" type="checkbox" id=="category[]" value="home builders">
Home Builders<br>
<input name="category[]" type="checkbox" id=="category[]" value="interior design">
Interior Design<br>
<input name="category[]" type="checkbox" id=="category[]" value="kitchen cabinets">
Kitchen Cabinets<br>
<input name="category[]" type="checkbox" id=="category[]" value="landscaping">
Landscaping<br>
<input name="category[]" type="checkbox" id=="category[]" value="lawn and garden sprinklers">
Lawn and Garden Sprinklers<br>
<input name="category[]" type="checkbox" id=="category[]" value="lawn maintenance">
Lawn Maintenance<br>
<input name="category[]" type="checkbox" id=="category[]" value="lumber">
Lumber<br>
<input name="category[]" type="checkbox" id=="category[]" value="mechanical">
Mechanical<br>
<input name="category[]" type="checkbox" id=="category[]" value="mortgages">
Mortgages<br>
<input name="category[]" type="checkbox" id=="category[]" value="painting">
Painting<br>
<input name="category[]" type="checkbox" id=="category[]" value="plumbing">
Plumbing<br>
<input name="category[]" type="checkbox" id=="category[]" value="railings">
Railings<br>
<input name="category[]" type="checkbox" id=="category[]" value="renovations">
Renovations<br>
<input name="category[]" type="checkbox" id=="category[]" value="roofing">
Roofing<br>
<input name="category[]" type="checkbox" id=="category[]" value="security systems">
Security Systems<br>
<input name="category[]" type="checkbox" id=="category[]" value="siding">
Siding<br>
<input name="category[]" type="checkbox" id=="category[]" value="stucco">
Stucco<br>
<input name="category[]" type="checkbox" id=="category[]" value="tree service">
Tree Service<br>
<input name="category[]" type="checkbox" id=="category[]" value="video surveillance">
Video Surveillance<br>
<input name="category[]" type="checkbox" id=="category[]" value="windows">
Windows<br>
<input name="category[]" type="checkbox" id=="category[]" value="wood workers">
Wood Workers<br>
</p> </td></tr>

davidj
11-25-2006, 10:36 AM
you are on the right lines

to examine the contents of $catagory just use the print_r() function

print_r($catagory)

ok so that will print the array contents to the screen which is a good test that its working

i take it you want to have this array inserted into 1 field inyour db???

davidj
11-25-2006, 12:01 PM
ok

we are going to use an implode() function

//string implode ( string glue, array pieces )

you can use anything as glue. I like to use pipe

$impval = implode('|', $catagory);

ok so implode converts the Array into a string delimited by a pipe but you can use anything to delimit it like a comma

so now you can use $impval in your insert statement

the reason why we have added a delimiter is that we may need to reconstitue the string back into an array if we want to display that data row and re-check the checkboxes with the right values

kona72
11-25-2006, 01:47 PM
wow....
i am sooooo lost. I am going to finish watching you tutorials right now and come back and hopefully get this.

You are right about this....

i take it you want to have this array inserted into 1 field inyour db???

I have my insert statement (i think!)

GetSQLValueString($_POST['category'], "text"));

but i have no idea how i am supposed to modify it. I need to figure out where a function will go and where in my insert statement the $impval will go.

Man I wish I had your brains!!!

Thanks again!!!

davidj
11-25-2006, 03:31 PM
follow the vids and come back if you have any questions

you will benefit more from the vids than the code DW write for you

My knowledge has been obtained by doing this stuff every day and having a genuine interest in it. There is nothing Wow about it. You too will be writing your own tuts in a few months