Help with Notice (8): Undefined index:

[php]

public function index()
{
$keyword="";

    if ($this->request->is('post')){
        $keyword = $this->request->data['benzs']['keyword'];
    }else if(isset($this->params['named']['keyword'])){
        $keyword = $this->params['named']['keyword'];
    }

    if($keyword !=""){
        $this->paginate = array(
            'limit' => 10,
            'conditions' => array(
                'or'=> array(
                    array("year_make_model LIKE" =>'%' . $keyword . '%'),
					array("year LIKE" =>'%' . $keyword . '%'),
                    array("mileage LIKE" => '%' . $keyword . '%'),
                    array("price LIKE" => '%' . $keyword . '%')
                )
            )
        );
        $this->set('keyword', $keyword);
    }
    else{
        $this->paginate = array(
            'limit' =>10,
        );
    }



    $this->set('benzs', $this->paginate('Benz'));
}

[/php]

You aren’t giving enough information for anyone to help. An undefined index generally means, there is not that specific name associated with the array you are using.

Here is the error that I am getting.
Notice (8): Undefined index: benzs [APP/Controller/BenzsController.php, line 18]
Here is line 18:
[php]
$keyword = $this->request->data[‘benzs’][‘keyword’];
[/php]
Here is the whole contoller:
[php]
class BenzsController extends AppController
{
public $helpers = array(‘Html’, ‘Form’);
public $components = array(‘Paginator’);

public function index()
{
    $keyword="";

    if ($this->request->is('post')){
        $keyword = $this->request->data['benzs']['keyword'];
    }else if(isset($this->params['named']['keyword'])){
        $keyword = $this->params['named']['keyword'];
    }

    if($keyword !=""){
        $this->paginate = array(
            'limit' => 10,
            'conditions' => array(
                'or'=> array(
                    array("year_make_model LIKE" =>'%' . $keyword . '%'),
					array("year LIKE" =>'%' . $keyword . '%'),
                    array("mileage LIKE" => '%' . $keyword . '%'),
                    array("price LIKE" => '%' . $keyword . '%')
                )
            )
        );
        $this->set('keyword', $keyword);
    }
    else{
        $this->paginate = array(
            'limit' =>10,
        );
    }



    $this->set('benzs', $this->paginate('Benz'));
}

public function view($id = null) {
    if (!$id) {
        throw new NotFoundException(__('Invalid car'));
    }

    $benz = $this->Benz->findById($id);
    if (!$benz) {
        throw new NotFoundException(__('Invalid car'));
    }
    $this->set('benz', $benz);
}
public function add() {
    if ($this->request->is('benz')) {
        $this->Benz->create();
        if ($this->Benz->save($this->request->data)) {
            $this->Session->setFlash(__('Your car has been saved.'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Unable to add your car.'));
    }
}
public function edit($id = null) {
    if (!$id) {
        throw new NotFoundException(__('Invalid car'));
    }

    $benz = $this->Benz->findById($id);
    if (!$benz) {
        throw new NotFoundException(__('Invalid car'));
    }

    if ($this->request->is(array('post', 'put'))) {
        $this->Benz->id = $id;
        if ($this->Benz->save($this->request->data)) {
            $this->Session->setFlash(__('Your car has been updated.'));
            return $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Unable to update your car.'));
    }

    if (!$this->request->data) {
        $this->request->data = $benz;
    }
}
public function delete($id) {
    if ($this->request->is('get')) {
        throw new MethodNotAllowedException();
    }

    if ($this->Benz->delete($id)) {
        $this->Session->setFlash(
            __('The car with id: %s has been deleted.', h($id))
        );
    } else {
        $this->Session->setFlash(
            __('The car with id: %s could not be deleted.', h($id))
        );
    }

    return $this->redirect(array('action' => 'index'));
}

}

[/php]

Also the search works fine for my other pages. Just not this one. It is basically the exact same code. That is why I am dumbfounded.

Just in case anyone runs into this problem. It is a singular plural issue.
[php]
here error code : $keyword = $this->request->data[‘benzs’][‘keyword’];

that should be
$keyword = $this->request->data[‘benz’][‘keyword’];

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service