Jump to content

A better filter

Filter wildcards

  • Please log in to reply
3 replies to this topic

#1 Crashcode



  • Members
  • Pip
  • 4 posts

Posted 17 December 2014 - 12:17 PM



I just  want to show how to create a better filter. You only have to add a little bit more code and the filter will work more specify.




Line: 127 to 153

<div class="sDiv quickSearchBox" id='quickSearchBox' >
		<div class="sDiv2" style="width: 80%;">
            <?php echo $this->l('list_search');?>:
            <select name="search_field" id="search_field">
				<option value=""><?php echo $this->l('list_search_all');?></option>
				<?php foreach($columns as $column){?>
				<option value="<?php echo $column->field_name?>"><?php echo $column->display_as?>&nbsp;&nbsp;</option>
				<?php }?>
            <select name="wildcard" id="wildcard">
                <option value="equal">equal</option>
                <option value="not_equal">does not equal</option>
				<option value="begin">begins with</option>
				<option value="end">ends with</option>
				<option value="contain">contain</option>
				<option value="not_contain">does not contain</option>
			 <input type="text" class="qsbsearch_fieldox search_text" name="search_text" size="30" id='search_text'>

            <input type="button" value="<?php echo $this->l('list_search');?>" class="crud_search" id='crud_search'>
        <div class='search-div-clear-button'>
        	<input type="button" value="<?php echo $this->l('list_clear_filtering');?>" id='search_clear' class="search_clear">

On the first picture you can see the few options.




Now edit the file Grocery_CRUD.php.


Start at line 593.



					$escaped_text = $this->basic_model->escape_str($state_info->search->text);
					$this->having($state_info->search->field." LIKE '%".$escaped_text."%'");
				$wildcard = $_POST['wildcard'];
                                if($wildcard == "equal")
					$this->like($state_info->search->field , $state_info->search->text , 'none');
                                elseif($wildcard == "not_equal")
					$this->where($state_info->search->field.' !=', $state_info->search->text);
                                elseif($wildcard == "begin")
					$this->like($state_info->search->field , $state_info->search->text , 'after');
                                elseif($wildcard == "end")
					$this->like($state_info->search->field , $state_info->search->text , 'before');
                                elseif($wildcard == "contain")
					$this->like($state_info->search->field , $state_info->search->text);
                                elseif($wildcard == "not_contain")
					$this->where($state_info->search->field.' not like ', "%".$state_info->search->text."%");


Thats all.


I wrote this because I didn't found something like this.

It isn't very much but I hope someone of you could use it for his own table. And I hope it will help somebody of you.

#2 Amit Shah

Amit Shah

    grocery CRUD Master

  • Advanced Member
  • PipPipPip
  • 1,192 posts
  • LocationMumbai, India

Posted 18 December 2014 - 05:12 AM

Good effort-  some one might build something useful from this!!

#3 Anten



  • Members
  • Pip
  • 1 posts

Posted 15 May 2018 - 10:26 AM

@Crashcode, Thank you so much for your post. Helped me customize my code such that its similar to yours.



I had been a silent reader & silent user of grocerycrud for almost 2 years, and your code is what tipped me over.

Just wanna give credits where its due. Thanks guys!!

#4 nurdiansyah



  • Members
  • Pip
  • 1 posts

Posted 04 July 2019 - 02:17 AM

thank you sir, this is very helpful.

Also tagged with one or more of these keywords: Filter, wildcards

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users