Welcome Guest!
Please login
If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:
- Receive Fast Advice
- Learn Programming Languages
- Get Professional Website Reviews
- Quick Troubleshooting Assistance
|
|
Help With Check Boxes And Submitting
This is a discussion on Help With Check Boxes And Submitting, within the PHP section. This forum and the thread "Help With Check Boxes And Submitting" are both part of the Programming Your Website category.
![]() ![]() |
Apr 6 2008, 03:34 PM
Post
#1
|
|
|
Rapid Squeezer ![]() ![]() ![]() ![]() Group: Members Posts: 102 Joined: 16-February 08 Member No.: 163 |
I'm working on the form and it has check boxes with different categories, let's say the categories were which sports you liked?
Rugby Football Tennis Cricket Volleyball For example. I want, for each one to be ticked, it to be added to a category colum in a database. This would allow for searching, IE: search.php?sport=football Would find the people who ticked football, but not only football. I don't know the best way of doing it? Does anyone understand what I mean? -------------------- Jack Franklin | Eportfolio & Weblog
|
|
|
Apr 6 2008, 03:38 PM
Post
#2
|
|
![]() Squeeze Machine ![]() ![]() ![]() ![]() ![]() Group: Administrators Posts: 643 Joined: 13-February 08 From: Catching the squeezed drips downunder. Member No.: 13 |
Yes, PHP has a way of allowing for arbitrary numbers of checkbox.
CODE <input type="checkbox" name="sport[]" value="football" /> <input type="checkbox" name="sport[]" value="rugby" /> etc.... By naming your inputs this way, PHP will build all the selected checkboxes into an array that you can access via $_GET['sport'] or $_POST['sport'] This means that you would then be able to have the same thing on your search form to search for multiple sports at once. Have I answered the right question? -------------------- |
|
|
Apr 6 2008, 03:41 PM
Post
#3
|
|
|
Rapid Squeezer ![]() ![]() ![]() ![]() Group: Members Posts: 102 Joined: 16-February 08 Member No.: 163 |
I think so, but how would I go about Inserting them into a database in an easy to search way? I don't want a column for each category, do I? Or do I? lol.
-------------------- Jack Franklin | Eportfolio & Weblog
|
|
|
Apr 6 2008, 03:49 PM
Post
#4
|
|
![]() Squeeze Machine ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 573 Joined: 13-February 08 From: Scotland, UK Member No.: 4 |
Hmm.. I dont really understand your question, but you want the user to be able to tick many boxes.. Correct?
Can you not do this by something like (lets say the url is: 'index.php?sport=football,rugby'): [php] // assume there is multiple sports checked $sports = $_GET['sport']; $singleSports = explode(",", $sports); //print the sports selected print $singleSports[0]; print $singleSports[1]; [/php] I'll say agina.. I dont really get yer question.. lol -------------------- Thanks,
Marc |
|
|
Apr 6 2008, 03:49 PM
Post
#5
|
|
![]() Squeeze Machine ![]() ![]() ![]() ![]() ![]() Group: Administrators Posts: 643 Joined: 13-February 08 From: Catching the squeezed drips downunder. Member No.: 13 |
Well, the best way would be create a table for sports with say two columns
SPORT_ID(primary key) sportName You would then fill that up with the sports you want in there and each will get it's own primary key. You then add this primary key as a foreign key where you need it -- say in a user's table in a column, favouriteSports. If you use the SPORT_ID key in your form say if football had the SPORT_ID of 1, your checkbox would be <input type="checkbox" name="sport[]" value="1" />. Then you can just search against the primary SPORT_ID key or foreign key in other tables to find things matching that sport. So instead of having a column for each sport, have a table for each category and a row each each option. Correct use of primary and foreign keys in your databases will make searching like this very easy. I hope that makes sense. -------------------- |
|
|
Apr 6 2008, 03:53 PM
Post
#6
|
|
|
Rapid Squeezer ![]() ![]() ![]() ![]() Group: Members Posts: 102 Joined: 16-February 08 Member No.: 163 |
What's a foriegn key? Only ever heard of primary, know what that does!
Basically, I want to insert the categories the user ticks in the 'sign up' form into the database in a way that makes it easy to search for a specific category. Hopefully that makes more sense. Thanks for the help so far This post has been edited by jackfranklin: Apr 6 2008, 03:53 PM -------------------- Jack Franklin | Eportfolio & Weblog
|
|
|
Apr 6 2008, 03:55 PM
Post
#7
|
|
![]() Squeeze Machine ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 573 Joined: 13-February 08 From: Scotland, UK Member No.: 4 |
Can you not just insert them as the sports name.. separated by ','s and then explode them out? I'm just thinking out loud here.. lol!
-------------------- Thanks,
Marc |
|
|
Apr 6 2008, 03:56 PM
Post
#8
|
|
|
Rapid Squeezer ![]() ![]() ![]() ![]() Group: Members Posts: 102 Joined: 16-February 08 Member No.: 163 |
Sounds fine, but how to insert them with a comma sperating each one?
-------------------- Jack Franklin | Eportfolio & Weblog
|
|
|
Apr 6 2008, 04:00 PM
Post
#9
|
|
![]() Squeeze Machine ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 573 Joined: 13-February 08 From: Scotland, UK Member No.: 4 |
CODE $chkboxes = array($_POST['football'], $_POST['rugby']); $commas = implode(",", $chkboxes); I really aint sure about this.. there might be a better way, just let one of the php gurus reply lol -------------------- Thanks,
Marc |
|
|
Apr 7 2008, 12:56 AM
Post
#10
|
|
![]() Rapid Squeezer ![]() ![]() ![]() ![]() Group: Advisors Posts: 176 Joined: 14-February 08 From: Willich, Germany Member No.: 56 |
Nope, that won't work.... Use array_merge:
CODE $a1=array('a','b','c');
$a2=array('aa','bb','cc'); $commas=implode(',',array_merge($a1,$a2)); -------------------- www.c010depunkk.com ~ the hangout of a web developer
|
|
|
Apr 7 2008, 04:13 AM
Post
#11
|
|
![]() Squeeze Machine ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 573 Joined: 13-February 08 From: Scotland, UK Member No.: 4 |
Yeah, when I thought about it.. I realised this and was about to look for a function that would...Seems that's the one!
Thanks Jan! -------------------- Thanks,
Marc |
|
|
Apr 7 2008, 05:42 AM
Post
#12
|
|
![]() Squeeze Machine ![]() ![]() ![]() ![]() ![]() Group: Administrators Posts: 643 Joined: 13-February 08 From: Catching the squeezed drips downunder. Member No.: 13 |
What's a foriegn key? Only ever heard of primary, know what that does! A foreign key is a column in a table that references the primary key of another table. By maintaining this primary/foreign reference, you can cross-reference data and maintain integrity. mySQL doesn't enforce foreign key usage and doesn't check redundancy but building databases in this manner is definitely the way to go. Essentially, as soon as you start seeing data duplication in a table (like 3 users all liking football) you want to move that data into a new table and replace it in the original table with foreign key references. It helps to keep database size down as well. -------------------- |
|
|
Apr 7 2008, 11:47 AM
Post
#13
|
|
|
Rapid Squeezer ![]() ![]() ![]() ![]() Group: Members Posts: 102 Joined: 16-February 08 Member No.: 163 |
Thanks Luke, turns out I do know what foreign keys are, just not by that name!
Jan/marc - I'll try it later and get back to you. Thanks, -------------------- Jack Franklin | Eportfolio & Weblog
|
|
|
Apr 7 2008, 01:44 PM
Post
#14
|
|
![]() Squeezing ![]() ![]() ![]() Group: Members Posts: 70 Joined: 14-February 08 From: Las Vegas, NV Member No.: 122 |
Can you not just insert them as the sports name.. separated by ','s and then explode them out? I'm just thinking out loud here.. lol! I personally wouldn't recommend this. Normalizing databases makes things very much easier to work with. -------------------- |
|
|
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
6 | Jason | 247 | 21st February 2008 - 12:25 PM Last post by: thesealportalteam |
|||
![]() |
20 | thesealportalteam | 286 | 1st March 2008 - 09:50 AM Last post by: Marc |
|||
![]() |
9 | Linda | 291 | 27th March 2008 - 03:01 PM Last post by: Jacob |
|||
![]() |
1 | Mark | 153 | 4th June 2008 - 01:24 PM Last post by: karinne |
|||
![]() |
2 | Jason | 171 | 13th June 2008 - 05:32 AM Last post by: Rakuli |
|||






Apr 6 2008, 03:34 PM












