Welcome Guest, Not a member yet? Register   Sign In
Checkboxes, i have no clue...
#1

[eluser]Jbeasley6651[/eluser]
I have never had to do this before so i have no clue.

I am building a real estate property search and all is well except for the fact that there is a need to search by multiple cities using check boxes. So I am outputting all the cities and populating a page with about 25 cities all with checkboxes next to them.

THE PROBLEM : I do not know how to associate the multiple selected/checked boxes into my search query.

Can someone please write a very rough example or point me to an open source ci project that uses this?

Thanks,

Jbeasley
#2

[eluser]heat23[/eluser]
What I do is set the "name" and id" attribute of the checkboxes all to "cb[]", that way the values for all the checkboxes will be stored as an array. For the "value" attribute you want to set it as the city names or IDs (whichever is stored in your database)

Then you can pass this array into your query...
$this->db->where_in('city_id',$this->input->post('cb'))
#3

[eluser]Jbeasley6651[/eluser]
Thanks,

So just to reiterate incase people were to come across this in a search

looping through available checkboxes would look like.

foreach($allcity->result() as $row):
<input type="checkbox" id="cb[]" name="cb[]" value="$row->cityid">$row->cityname</input>
endforeach;

and then in the query you would find the selected check boxes and search them by:

$this->db->where_in(‘city_id’,$this->input->post(‘cb’));

Correct? Please correct me if i am wrong.
#4

[eluser]heat23[/eluser]
That sounds correct, but give it a try and let me know if that works out.
#5

[eluser]Jbeasley6651[/eluser]
Works Perfectly!
#6

[eluser]heat23[/eluser]
Woohoo! The first person I've helped on these forums!
I'm new to CI and have asked a bunch of questions myself but I haven't contributed yet.
#7

[eluser]Jbeasley6651[/eluser]
Well it really helped, i had been straying away from having to use checkboxes for a long time, now i can stop thinking of ways around them and start using them.

Today i'm going to use them to improve my searching and to compare the users favorites side by side.

I haven't looked into it yet, but i'll just ask since you seem to be in the helping mood.

Is there a way to limit these check boxes to only allow 4 of them to be selected at once?

So lets say my foreach loop output 50 checkboxes but i only want them to be able to select 4 of them.

Thanks in advance!
#8

[eluser]heat23[/eluser]
The solution depends on where you want to catch that condition
1) If you want to prevent them from physically clicking on boxes after they have done 4, you will need to use the onClick attribute for each checkbox and have a Javascript function that iterates through all the checkboxes on the form and count the number of ones that are checked. If it detects there are 4 checked, throw an alert() to the user

2) If you want them to be able to check as many as they want, but catch it when they hit the submit button, put the onClick() on the button itself

3) If you want to bypass Javascript altogether, you can check for this condition after the form has been submitted. In PHP just do a sizeof($this->input->post('cb')) and if it equal to more than 4, throw an error message

Btw: My Javascript skills aren't good enough to help you out with this solution, otherwise I would have.
#9

[eluser]Jbeasley6651[/eluser]
Thanks!

This worked excellent!

http://www.javascriptkit.com/script/scri...imit.shtml
#10

[eluser]Unknown[/eluser]
It can very easily using array. I also face that problem once when I have published my website.
$this->db->where_in(‘city_id’,$this->input->post(‘cb’))
Just try it once.




Theme © iAndrew 2016 - Forum software by © MyBB