Checkbox multiselect help

lnpfc 16 Oct, 2015
Hy,
From a few days I´m traying to make a simple multiselect checkbox, but when I´m do the filter appears just one of these options. The filtter works, but just for one option.

My code (in the header of my table):
<input type="checkbox" name="fltr[model][Tipo]" value="0" />Value 0<br />
<input type="checkbox" name="fltr[model][Tipo]" value="1" />Value 1<br />
<input type="checkbox" name="fltr[model][Tipo]" value="2" />Value 2<br />
<?php 
if(isset($_POST["fltr[model][Tipo]"])) { 
foreach($_POST["fltr[model][Tipo]"] as $fltr[model][Tipo]) {  } } 
else { }
?>

I have 3 entries (0, 1 y 2) of my item Tipo. I tried replase the checkbox name to "fltr[model][Tipo][]" but it didn't work. I supose it is a simple thing, but nine of the solutions that I found in Internet works.

Thank you very much!
GreyHead 16 Oct, 2015
Hi lnpfc,

You have to use the array name fltr[model][Tipo][] to get multiple results returned.
But your PHP code doesn't actually do anything ?

Bob
lnpfc 16 Oct, 2015
Hi, thank you for your reply!

When I use the []s (like below), return to me: "strlen() expects parameter 1 to be string, array given in...", and actually my php (that I wrote you before) not works. In the same header, I have one more filter (for other item, and in "text" mode) and it works, and if I use this same "Tipo" filter as a text, works too, but I can do the multiple choices.

<input type="checkbox" name="fltr[model][Tipo][]" value="0" />Value 1<br />
<input type="checkbox" name="fltr[model][Tipo][]" value="1" />Value 2<br />
<input type="checkbox" name="fltr[model][Tipo][]" value="2" />Value 3<br />


Thanks again!
GreyHead 16 Oct, 2015
Hi lnpfc,

Well yes - you are now getting an array of values back which is what you need.

There is no strlen() in the code that I can see so I have no idea what you are trying to do with it.

Bob
lnpfc 16 Oct, 2015
Sorry, I really don´t know where I have to use the strlen. The message appears even when I don´t use the strlen in anywhere.

I just want to make a simple filter checkbox, that allows multiple (and mark simultaneously more than one option)


Again, thank you very much!
GreyHead 17 Oct, 2015
Hi lnpfc,

Please post the full error message including srtlen() as that should tell us if it is actually an error and where it is coming from.

Are you now getting the array of results from the Tipo check boxes?

Bob
lnpfc 18 Oct, 2015
Hi Grey, thanks again!

The error happens when I select one (or more) of my 3 Tipo´s option, like below:

strlen() expects parameter 1 to be string, array given in /var/www/html/***.com/web/libraries/cegcore/libs/gcontroller.php on line 217

On gcontroller.php file:
function _filter($columns = array()){
		$model_class = !empty($this->filter_model) ? $this->filter_model : null;
		if(empty($model_class)){
			$alias = $this->get_main_model();
			if(!empty($alias)){
				$model_class = $this->{$alias};
			}else{
				return;
			}
		}
		$prefix = '';
		if(!empty($this->filter_prefix)){
			$prefix = '.'.$this->filter_prefix;
		}

		$session = Base::getSession();
		$filters = Request::data('fltr', $session->get(get_class($this).$prefix.'.'.$model_class->alias.'.filters', array()));
		$conditions = array();
		if(!empty($filters)){
			foreach($columns as $k => $column){
				$fv = Arr::getVal($filters, explode('.', $column), null);
				if(strlen($fv) > 0){       //////////////////////////////line 217
					$conditions[$column] = $fv;
				}
			}
		}
		Request::set('fltr', $filters);
		//set model conditions based on filters
		$model_class->conditions = array_merge($conditions, $model_class->conditions);
		$session->set(get_class($this).$prefix.'.'.$model_class->alias.'.filters', $filters);
	}



Thank you so much, again!
GreyHead 18 Oct, 2015
Hi lnpfc,

First it isn't an error - as it says at the start it is a PHP Warning that something that might be a problem has happened.

In this case the problem is that you are trying to add an array - the results from your checkboxes - when the MySQL query that you are creating in the filter needs a string. Usually the answer to that is to convert the array using implode().

Bob
lnpfc 19 Oct, 2015
Hi Grey! Thanks for a part of your sunday to reply me...

I tried a lot of things, but didn't work. The best what I have, is doing the select field work with just one option. When I select 2 or 3, it returns me the last of these. I write the code below (note: when I change the "" of name of the fields ***[]" to ***"[], the message "strlen() expects parameter 1 to be string, array given in ar/www/html/***.com/web/libraries/cegcore/libs/gcontroller.php on line 217" returns)
<?php 

if(isset($_POST["fltr[model][Tipo]"])) { 
foreach($_POST["fltr[model][Tipo]"] as $fltr[model][Tipo]) {  } } 
else { };
implode(",",array("fltr[model][Tipo]"));

?>
<input type="checkbox" name="fltr[model][Tipo][]" value="0" />Value 0<br />
<input type="checkbox" name="fltr[model][Tipo][]" value="1" />Value 1<br />
<input type="checkbox" name="fltr[model][Tipo][]" value="2" />Value 2<br />



Thanks again!
GreyHead 20 Oct, 2015
Hi lnpfc,

I'm afraid that you have lost me. I'm not clear where you have this PHP; most of it does nothing and the last line is broken array("fltr[model][Tipo]") is just an array containing that string.

Bob
lnpfc 21 Oct, 2015
Hi Grey, thanks agains, specially for your patience.

Actually I note that my php code didn´t nothing! Now, I leave just a implode, but I´m not sure with it is working. The filtter is almost ok, but when I select more than one option, the results considers just the last one. Now, I have the following:

<?php

implode(",",array($fltr[model][Tipo]))
?>

<input type="checkbox" name="fltr[model][Tipo]"[] value="0" />Value 0<br />
<input type="checkbox" name="fltr[model][Tipo]"[] value="1" />Value 1<br />
<input type="checkbox" name="fltr[model][Tipo]"[] value="2" />Value 2<br />


Thanks again, when my website is ok, I wont pay a beer, I will pay a box of Heineken! lol....
lnpfc 23 Oct, 2015
Hy Grey, me again...

I tried a lot of things, but always when I use the [] inside the "" in the form name, returns to me the strl PHP warning and it don´t do my filter. I read a lot of things (including yours tutorials), but I couldn´t find a solution. I imploded the array, but I didn=t win this game yet! I just want to that this filtter returns more than one option, but the simple "[]" didn´t work!

My code right now:

<input type="checkbox" name="fltr[model][Tipo][]" value="0" />Value 0<br />
<input type="checkbox" name="fltr[model][Tipo][]" value="1" />Value 1<br />
<input type="checkbox" name="fltr[model][Tipo][]" value="2" />Value 2<br />



<?php
implode(" ",array("fltr[model][Tipo]"));
$multi = $_POST['fltr[model][Tipo]'];
for($i = 0; $i < count($multi); $i++) {

}
?>


Thanks, again!
GreyHead 23 Oct, 2015
Hi lnfpc,

You can't make up code out of thin air and expect it to work :-(

array("fltr[model][Tipo]") - creates a new array with one entry, the string 'fltr[model][Tipo]' but as there is no variable name it does nothing except maybe generate an error message.

implode(" ",array("fltr[model][Tipo]")); will implode the newly created array but as it only has the one entry (see above), it doesn't change anything - and again there is no variable name assigned so it does nothing.

Bob




Bob
This topic is locked and no more replies can be posted.