How can I use a wildcard search to get a list from scaffolding in a CakePHP?

后端 未结 2 1292
被撕碎了的回忆
被撕碎了的回忆 2021-01-26 15:19

I\'ve got a scaffold built for CakePHP, but need to have a way for users to type in part of a surname into a text box, click a button and for the list of people to be filtered t

2条回答
  •  心在旅途
    2021-01-26 15:48

    views/users/index.ctp

    Filter users create('User', array('action' => 'index')); echo $form->input('surname', array('div' => false)); echo $form->submit('Filter', array('div' => false)); echo $form->end(); ?>

    controllers/users_controller.php

    public function index($surname = null) {
        // post-redirect-get 
        if ($this->data['User']['surname']) {
           $this->redirect(array('id' => $this->data['User']['surname']));
        }
        // conditions
        $conditions = null;
        if ($surname) {
            $conditions = array('surname LIKE' => '%' . $surname . '%');
        }
        // find
        $users = $this->Users->find('all', array('conditions' => $conditions));
        // set
        $this->set(compact('users'));
    }
    

提交回复
热议问题