如何将数据库检索的数据与cakephp中的输入值进行匹配

时间:2015-04-27 13:49:18

标签: php mysql regex cakephp

我想在数据库表中搜索与输入值匹配的数据,当找到它时,我想选择具有最高匹配数的数据并将其保存在字段中,我使用的是cakephp。 这是我到目前为止所做的:

public function add() {
    if ($this->request->is('post')) {

            $data = $this->Code->find('all',array(
            'conditions'=>array('Model.is_expired'=>0),
            'fields'=>array('Model.words'),

            ));

            $str = $this->request->data['Model']['words'] ;//the input value
            $sort_array=array();
            foreach($data as $value){
            $num = similar_text($value, $str);
            $sort_array[$value] = $num;
            }
         arsort($sort_array, SORT_REGULAR);
         $res = array_slice($sort_array,0,1);
         foreach($res as $key=>$value){
               return $key;
         }
            $this->request->data['Model']['invitee_1'] = $key;              
        }
        $this->Model->create();
        if ($this->Model->save($this->request->data)) {
            $this->Session->setFlash(__('The Model has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The Model could not be saved. Please, try again.'));
        }
    }

}

表格字段是: ID |话| new_word |

我想在“单词”字段中搜索与输入值(来自表单)匹配的单词,并选择具有最高匹配计数的单词,最后将其保存在表单旁边的“new_word”字段中输入将保存在“单词”字段中。

0 个答案:

没有答案
相关问题