本文介绍了自动填充数据字段并将数据保存在cakephp 2.5.1中的数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CakePHP的新手。我有一个CakePHP应用程序,它有街道表(列有street_name,house_no,区,城市)。我需要在表单中自动填充这些数据并将它们保存在一个数组中以便进一步处理数据。应该是这样,用户将在具有字段 - 街道名称,房屋号,区域和城市的表单的街道名称字段中键入街道名称。从汽车建议中选择街道名称后,相应的数据(房屋号码,区域和城市)将填入表格的其他字段。



什么我试过了:



现在,街道表中加载了超过25000行,这是一个城市的整个街道数据库。此表未通过外键与应用程序的其他表链接。 /Model/Street.php中的街道模型如下:



I am new in CakePHP. I have a CakePHP application which has streets table (with columns street_name, house_no, district, city). I need to autocomplete these data in a form and save them in an array for further processing of the data. It should be like, the user will be typing a street name in the Street Name field of a form which has fields- Street Name, House No, District and City. After selecting a street name from auto suggestions, corresponding data (House No, District and City) will be populated in the other fields of the form.

What I have tried:

Now, over 25000 rows are loaded in the streets table which is the entire street database of a city. this table is not linked with other tables of the application through foreign keys. Street model in /Model/Street.php is following:

class Street extends AppModel {

  public function getStreet ($term = null) {
    if(!empty($term)) {
      $streets = $this->find('list', array(
        'conditions' => array(
          'street_name LIKE' => trim($term) . '%'
        )
      ));
      return $streets;
    }
    return false;
  }
}





在/Controller/StreetsController.php,





In /Controller/StreetsController.php,

class StreetsController extends AppController {

   public function autocomplete($term){

   if ($this->request->is('get')) {
       $this->autoRender = false;
       $data = $this->Street->getStreet($term);
       $this->set(compact('data'));
       $this->set('_serialize', array('data'));

       echo json_encode($data);
       }
   }
 }





/ webroot / js / street。 js文件,





In /webroot/js/street.js file,

(function($) {
  $('#autocomplete').autocomplete({
        source: "/street.json"
  });
})(jQuery);





输入表单位于不同的控制器中。在View / Users / add.ctp文件中,





The input forms are in different controller. In View/Users/add.ctp file,

<fieldset>
        <legend><?php echo __('Address Data'); ?></legend>

        echo $this->Form->input('street_name', array(
                   'class' => 'ui-autocomplete',
                   'id' => 'autocomplete'));
        echo $this->Form->input('house_no');
        echo $this->Form->input('district');
        echo $this->Form->input('city');

  </fieldset>
  <?php echo $this->Form->end(__('Save')); ?>



有人能告诉我,请问如何进一步处理?我找到了一些教程,其中使用CakePHP中的jQuery实现了这个自动完成功能。但是,它使用相同的表,模型,视图和控制器实现,如汽车,Car.php,/ View / Cars / index.ctp,CarsController.php。这与我的场景不相似。


Could anyone tell me please how to proceed further ? I found some tutorials where this autocomplete function is implemented using jQuery in CakePHP. But, it is implemented using same table, model, view and controller like cars, Car.php, /View/Cars/index.ctp , CarsController.php. which is not similar to my scenario.

推荐答案




这篇关于自动填充数据字段并将数据保存在cakephp 2.5.1中的数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 00:17