-1

I want to search my data from database by searching input,Everything is looking right but i can not find why this code is not work.when i input the searching data in input box any thing is done.please help

My controller : AdUnitsController

 class AdUnitsController extends AppController

{

public function index()
{

    $query = $this->_getData();
    $this->set('adUnits', $this->paginate($query));

    $this->paginate = [
        'contain' => ['Networks', 'Games', 'AdTypes']
    ];

    $adUnits = $this->paginate($this->AdUnits);

    $this->set(compact('adUnits'));
    $this->set('_serialize', ['adUnits']);

}

public function _getData()
{
    if (!empty($this->request->data['search'])) {
        $search = $this->request->data['search'];
        $query = $this->AdUnits->find('All')->where([
            'OR' => [
                ['name LIKE' => '%' . $search . '%'],
            ],
        ]);
    }
    else{
        $query = $this->AdUnits->find('All');
    }
    return $query;
}

MY VIEW index.ctp:

        <label> Search</label>
        <?php echo $this->Form->create('AdUnits',array('id' => 'site-search','url'=>array('action'=>'index')));?>
        <div>
            <?php echo $this->Form->input('search',array('class' => 'form-control', 'label' =>false, 'placeholder' => 'Type Network')); ?>
        </div>

        <div> <button type="submit"> Search</button>

            <?php echo $this->Form->end();?>

There searching not work.please help

  • you are paginating two times and I don't know why. Set the pagination array before the `paginate()` call then remove the second pagination call – arilia Jul 20 '16 at 09:35
  • ask yourself why you are paginating two times and setting two times the value of $adUnits. The second one overwrites the first and is useless. – arilia Jul 20 '16 at 10:35
  • what do you mean with don't work? Do you get any error? – arilia Jul 20 '16 at 11:01
  • so enable debug mode and please tell us what error do you get – arilia Jul 20 '16 at 11:17
  • so go read the manual [here](http://book.cakephp.org/3.0/en/development/configuration.html#general-configuration), [here](http://book.cakephp.org/3.0/en/development/debugging.html) and [here](http://book.cakephp.org/3.0/en/debug-kit.html) – arilia Jul 20 '16 at 11:37
  • My guess is that the `name` column is ambigous, replace it with `'AdUnits.name LIKE'` – arilia Jul 20 '16 at 11:38
  • oh great,now it is work.thanks a lot.Can i know you facebook id?please. – Programmer guy Jul 20 '16 at 11:40

2 Answers2

1

first of all simplify your code: you're doing two paginations with no reasons

public function index()
{
    $this->paginate = [
        'contain' => ['Networks', 'Games', 'AdTypes']
    ];
    $query = $this->_getData();
    $this->set('adUnits', $this->paginate($query));

} 

if you are still facing problems probably you have an error in your query. My guess is that the name columns is ambigous so do this in your find() call

['AdUnits.name LIKE' => '%' . $search . '%']

as a side note I suggest you to read the documentation, in particular learn how to debug your code.

Also the _getData methos, in my opinion, should be moved to the model or into a component.

arilia
  • 9,373
  • 2
  • 20
  • 44
0
public function index()
{

    $query = $this->_getData();
    $this->set('adUnits', $this->paginate($query));

    $this->paginate = [
        'contain' => ['Networks', 'Games', 'AdTypes']
    ];

    $adUnits = $this->paginate($this->AdUnits);

    $this->set(compact('adUnits'));
    $this->set('_serialize', ['adUnits']);

}

Replace with

    public function index()
{
    $this->paginate = [
        'contain' => ['Networks', 'Games', 'AdTypes']
    ];
    $query = $this->_getData();
    $this->set('adUnits', $this->paginate($query));

}