Jump to content


Photo

Comparing Checkbox Data in Database with Checkbox Data from $_POST


  • Please log in to reply
5 replies to this topic

#1 artemisbow

artemisbow
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 06 May 2006 - 04:35 AM

Hi, I'm having problems with comparing values stored in database with values from checkbox... ok let me explain :)

Several users selects several checkbox in a form whose values are 1, 2, 3 and 4, and is submited to a script that stores the $_POST data in an array called locations[]. Following that, I did
$locations = implode("|", $locations)
which gives me $locations = "1|2|3|4". $locations is then stored in a mysql table in the column user_location.

Next a visitor comes along, and in a seperate form, containing the same series of checkboxs, but selects 1 and 3, which is imploded and stored as $requiredlocations = 1|3.

Now this is where i'm stuck. The above visitor is filling up a search form to search for users who provide services in various locations. I initially thought of
$sql = "SELECT * FROM users where user_location LIKE '%".$requiredlocations."%'
but that will only work if the visitor only checked a single checkbox. How should I write the code such that the visitor can search for multiple locations?

Will really appreciate any help. Thanks!

#2 artemisbow

artemisbow
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 06 May 2006 - 11:26 AM

Please... I need help! Thanks!

#3 artemisbow

artemisbow
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 07 May 2006 - 02:07 PM

Anyone?

#4 artemisbow

artemisbow
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 08 May 2006 - 01:02 AM

bump^


#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 08 May 2006 - 02:02 AM

hi artemisbow. actually, i'm working on something very similar to this. the way i have gone about it is to setup 3 tables:

providers
======
provider_name
provider_id

services
=====
service_name
service_id

maintable
======
provider_id
service_id


this is a relational database. what happens is in table providers, you have a list of all providers and they each have their own id.

then, you have another table of services. all the available services are listed, and each one has its own id.

then you have this 3rd table (maintable) that looks something like this:

maintable
======
provider_id | service_id
---------------------------
1 | 3
1 | 4
1 | 8
2 | 1
2 | 3
2 | 6
2 | 8
.
.
.


now the idea behind all this is that when the user wants to search for providers based on the services they offer, you can refer to the maintable, and linkie the provider and service names to their id's from the other tables.

now, the query involved in making this happen well probably make you wet your pants. well it made me wet mine, anyways :)

anyways, the way i have my script setup so far is for example:

if you search for a provider by service1,2 and 3 you can have it match any of the 3 or all of the 3. that is, if a provider has any one of the 3 checked services, it will return that provider. Or, you can set it for 'all' and it will only return providers that offer all 3 of the checked services.

now, even though the query matches for the conditions above, it actually lists all of the services provided by each returned provider.

that is, let's say you have a provider1 and he offers service1, service2, service3, and service4.

now let's say you do a search to find all providers that offer service1 and service3. well, regardless of whether you do search for any or search for all, provider1 will be returned, because it offers both service1 and service3. but what will actually be returned is this:

provider1 | service1
provider1 | service2
provider1 | service3
provider1 | service4

in other words, it returns all services provided by provider1. the reason why i have it do this is because of how i want to actually display the results to the user. I want it to display something like this:

provider1 offers the following services: service1, service2, service3, service4

that's my own personal style i guess. the query probably wouldn't be half as complicated if all i wanted to do was return the provider names and that's it.

if you go to :
[a href=\"http://www.chroniclesofwar.com/providersearch.php\" target=\"_blank\"]http://www.chroniclesofwar.com/providersearch.php[/a]

you can see what i have so far. if you want, i can send you my code so far as well as a create table query for each table i have...
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 artemisbow

artemisbow
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 09 May 2006 - 01:07 AM

Thanks for the reply. I kindda figured it out by letting part of the sql query be generated by PHP depending on how many checkboxes the user ticked; rest of it is same as wat i had described above. :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users