⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

bootstrap theme, search in list with value 0 on tinyint(1) fields problem



VollS
  • profile picture
  • Member

Posted 27 April 2016 - 15:26 PM

Hi,

 

sorry to post another small problem. I use tinyint(1) fields for unchecked/checked markers. Default value is 0, active value is 1. I bought the bootstrap theme which i like alot. Problem is, if I search in list mode and want to see all records with value 0 it doesnt work. Searching with 1 works. Any way I can patch the behavior that it works?

 

Thank you,

 

Karl

 

Grocery Crud 1.5.4 and Codeigniter 3.0.6.

FreeBSD 10/64Bit, Apache 2.4, PHP 5.6.19, MySQL 5.6.27

 


VollS
  • profile picture
  • Member

Posted 30 April 2016 - 11:51 AM

Reducing the question to:

 

Does somebody know where in the base system this header query is built/handled?

 

Thank you,

 

Karl


VollS
  • profile picture
  • Member

Posted 02 May 2016 - 15:26 PM

I am one step further:

--- DB_query_builder.php.ORIG   2016-03-21 17:26:30.000000000 +0100
+++ DB_query_builder.php.KMJ    2016-05-02 16:37:01.000000000 +0200
@@ -962,6 +962,10 @@
                        }
                        else
                        {
+                               // KMJ BUGFIX 20160502 FIX001
+                               // KMJ this one has a bug. if search = "0" , $v becomes NULL
+                               // and search is %% which represents all records.
+                               // need to look where $v is coming from
                                $like_statement = "{$prefix} {$k} {$not} LIKE '%{$v}%'";
                        }
 
Query part in mysql looks like:

WHERE `uidchecked` LIKE '%%' ESCAPE '!'

and %% for "0" is deadly. :-)

 

Backtrace, but I am still not lucky to find where "0" is lost against null:

system/database/DB_query_builder.php(852): CI_DB_query_builder->_like(\'uidchecked\', \'\', \'AND \', \'both\', \'\', NULL)
application/models/Grocery_crud_model.php(165): CI_DB_query_builder->like(\'uidchecked\', \'\', \'both\')
application/libraries/Grocery_CRUD.php(501): Grocery_crud_model->like(\'uidchecked\', \'\', \'both\')
assets/grocery_crud/themes/bootstrap/views/list.php(11): grocery_CRUD_Model_Driver->get_total_results()
application/libraries/Grocery_CRUD.php(2916): include(\'/usr/local/www/...\')
application/libraries/Grocery_CRUD.php(1611): grocery_CRUD_Layout->_theme_view(\'list.php\', Object(stdClass))
application/libraries/Grocery_CRUD.php(4578): grocery_CRUD_Layout->showList(true)
application/controllers/Ctserp.php(1291): Grocery_CRUD->render()
[internal function]: Erp->mgmt_partner(\'ajax_list\')
system/core/CodeIgniter.php(514): call_user_func_array(Array, Array)
index.php(315): require_once(\'/usr/local/www/...\')

Anybody?


GiancarloN
  • profile picture
  • Member

Posted 14 July 2016 - 07:10 AM

Hello, I have faced the same problem and found the bug. Problem is that php empty($mixed) function returns true in case $mixed = 0.

Since I prefer not to modify the core, I have extended grocery crud as described in this thread /topic/90-how-to-create-an-extension-for-grocery-crud-real-example-included/

after that it is enough to override getStateInfo method in this way:

/**
	 * Override to fix search problem when value == 0 (actually empty(0) returns true)
	 * @see grocery_CRUD_States::getStateInfo()
	 */
	public function getStateInfo()
	{
		$state_code = $this->getStateCode();
		$segment_object = $this->get_state_info_from_url();
	
		$first_parameter = $segment_object->first_parameter;
		$second_parameter = $segment_object->second_parameter;
	
		$state_info = (object)array();
	
		switch ($state_code) {
			case parent::STATE_LIST:
			case self::STATE_ADD:
				//for now... do nothing! Keeping this switch here in case we need any information at the future.
				break;
	
			case self::STATE_EDIT:
			case self::STATE_READ:
				if ($first_parameter !== null) {
					$state_info = (object) array('primary_key' => $first_parameter);
				} else {
					throw new Exception('On the state "edit" the Primary key cannot be null', 6);
					die();
				}
				break;
	
			case self::STATE_DELETE:
				if ($first_parameter !== null) {
					$state_info = (object) array('primary_key' => $first_parameter);
				} else {
					throw new Exception('On the state "delete" the Primary key cannot be null',7);
					die();
				}
				break;
	
			case self::STATE_DELETE_MULTIPLE:
				if (!empty($_POST) && !empty($_POST['ids']) && is_array($_POST['ids'])) {
					$state_info = (object) array('ids' => $_POST['ids']);
				} else {
					throw new Exception('On the state "Delete Multiple" you need send the ids as a post array.');
					die();
				}
				break;
	
			case self::STATE_INSERT:
				if(!empty($_POST))
				{
					$state_info = (object)array('unwrapped_data' => $_POST);
				}
				else
				{
					throw new Exception('On the state "insert" you must have post data',8);
					die();
				}
				break;
	
			case 6:
				if(!empty($_POST) && $first_parameter !== null)
				{
					$state_info = (object)array('primary_key' => $first_parameter,'unwrapped_data' => $_POST);
				}
				elseif(empty($_POST))
				{
					throw new Exception('On the state "update" you must have post data',9);
					die();
				}
				else
				{
					throw new Exception('On the state "update" the Primary key cannot be null',10);
					die();
				}
				break;
	
			case 7:
			case 8:
			case 16: //export to excel
			case 17: //print
				$state_info = (object)array();
// 				vdebug($_POST,false,true);
				if(!empty($_POST['per_page']))
				{
					$state_info->per_page = is_numeric($_POST['per_page']) ? $_POST['per_page'] : null;
				}
				if(!empty($_POST['page']))
				{
					$state_info->page = is_numeric($_POST['page']) ? $_POST['page'] : null;
				}
				//If we request an export or a print we don't care about what page we are
				if($state_code === 16 || $state_code === 17)
				{
					$state_info->page = 1;
					$state_info->per_page = 1000000; //a very big number!
				}
				if(!empty($_POST['order_by'][0]))
				{
					$state_info->order_by = $_POST['order_by'];
				}
				if(!empty($_POST['search_text']))
				{
					if(empty($_POST['search_field']))
					{
						$search_text = strip_tags($_POST['search_field']);
						$state_info->search = (object)array('field' => null , 'text' => $_POST['search_text']);
					}
					else
					{
						if (is_array($_POST['search_field'])) {
							$search_array = array();
							foreach ($_POST['search_field'] as $search_key => $search_field_name) {
// 								$search_array[$search_field_name] = !empty($_POST['search_text'][$search_key]) ? $_POST['search_text'][$search_key] : '';
								//BUGFIX when search_text == 0
								if ((!empty($_POST['search_text'][$search_key])) || $_POST['search_text'][$search_key] == '0') {
									$search_array[$search_field_name] = $_POST['search_text'][$search_key];
								}
							}
							$state_info->search	= $search_array;
						} else {
							$state_info->search	= (object)array(
									'field' => strip_tags($_POST['search_field']) ,
									'text' => $_POST['search_text'] );
						}
					}
				}
				break;
	
			case 9:
	
				break;
	
			case 10:
				if($first_parameter !== null)
				{
					$state_info = (object)array('primary_key' => $first_parameter);
				}
				break;
	
			case 11:
				$state_info->field_name = $first_parameter;
				break;
	
			case 12:
				$state_info->field_name = $first_parameter;
				$state_info->file_name = $second_parameter;
				break;
	
			case 13:
				$state_info->field_name = $_POST['field_name'];
				$state_info->search 	= $_POST['term'];
				break;
	
			case 14:
				$state_info->field_name = $_POST['field_name'];
				$state_info->search 	= $_POST['term'];
				break;
	
			case 15:
				$state_info = (object)array(
				'primary_key' 		=> $first_parameter,
				'success_message'	=> true
				);
				break;
		}
	
		return $state_info;
	}

apart from it I found and workaround other problems in search function described here /topic/1963-simple-guide-to-executing-custom-queries/#entry14231

 

Bye,

G.


goodlucknow
  • profile picture
  • Member

Posted 05 October 2018 - 02:38 AM

Thank you for posting you solution. Helped me fix my current problem.

 

Hello, I have faced the same problem and found the bug. Problem is that php empty($mixed) function returns true in case $mixed = 0.

Since I prefer not to modify the core, I have extended grocery crud as described in this thread /topic/90-how-to-create-an-extension-for-grocery-crud-real-example-included/

after that it is enough to override getStateInfo method in this way:

 

apart from it I found and workaround other problems in search function described here /topic/1963-simple-guide-to-executing-custom-queries/#entry14231

 

Bye,

G.