Welcome Guest, Not a member yet? Register   Sign In
managing values from a Multiple Select list through the MVC way
#1

[eluser]alvaroeesti[/eluser]
Hello


I have a Form and one of the controllers is a multiple select list. Alright, I add [ ] to the name of the control and it will keep the values selected alright, and it forwards them to the controller. So here start the problems

1. First the validation class, I basically have to remove all the validators because they can't digest an array, the trim had to be removed, the max length also, so..it could be abused if not sanitized

Code:
$this->form_validation->set_rules('cities', 'cities', 'xss_clean');

2. Then we want to grab those values from the multiple select list, to me more descriptive, I am actually keeping them in session variables

Code:
$cities[] = $this->getresults_m->cities_handler($this->input->get_post('cities', TRUE));

Alright, but now I have to pass that to the Model, and here is where the problems start

Severity: Notice

Message: Array to string conversion

Filename: database/DB_active_rec.php

A Database Error Occurred

Error Number: 1054

Unknown column 'Array' in 'where clause'

this "array" refers exactly to cities, because when I try to do

where city.id = $city instead of the variable it is the word Array that is standing on the right side of the equal.

So, actually in that array I will have several cities, then how am I going to put that into the sql query? it seems to me that I would have to loop through the array, getting one variable at the time and then running the full query each time for every variable?



thank you


regards
#2

[eluser]pickupman[/eluser]
For your validation rules, you are trying to use string functions on an array. That is what is causing that error. In your situation, the solution would be to create a callback that loops through that field applying each of the string functions (max_length), and xss_clean(). Then return TRUE or FALSE based on the results of your loop.

To solve your second issue in your model, you need to apply similar logic. Without seeing your code from getresults_m->cities_handler, I would bet, that you are trying to fetch the names using the array you are passing. You would want to create a loop in the method as well.
Code:
function cities_handler($cities)
{
    foreach ($cities as $city)
    {
        $this->db->or_like('city_name', $city); // Add an OR LIKE '%city%' where statement
    }
    return $this->db->get('cities')->result();  //Fetch rows from cities table and return result
}
#3

[eluser]alvaroeesti[/eluser]

Thank you very much for your posting. They are good ideas.

I have been working all day on it and have gotten far. I did not treat the sanitation issue but I will after what you write. With regards to

the second issue, I did that but it was very complex. I was thinking about posting the full solution but I can tell you how I went because I think there could be an easier way.

I first did a print_r to see the kind of array I was getting from the View and it was a multidimensional array. This multidimensional

array was unpredictable in size as people could chose one or several cities. Therefore I looked up a php function that can

make a recursion to tell the length of the columns of this bidimensional array.

Once I had the columns I knew how many loops I needed to do inside the body of the function. I would also use the expression

$city = $cities[ ][ ] to use the scalar variable and I would just put it in the where clause as usual.

So that worked out. I haven't still gotten busy with the session issue now that I have the multidimensional array but it should be

about the same. Will be working on it the next days..

If you are interested in seeing the full solution just send me a pm and I will post it as soon as I get it (or come close to get it)

regards and thank you




Theme © iAndrew 2016 - Forum software by © MyBB