Codeigniter表单使用我的模型

时间:2015-09-21 19:49:29

标签: php mysql forms codeigniter

学习CodeIgniter 3并尝试进行简单搜索以查询Db ...: - )

我构建了我的Controller,必须查询Db的模型,以及包含结果和搜索字段的视图......

我的表单实际上提交了,我在表单上提交后得到了$ _POST数组......但似乎是模型,甚至被调用(或者不正确!?!?!)不是在查询Db ...为什么不?

Search.php控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Search extends CI_Controller {
          public function __construct()
        {
                parent::__construct();
                $this->load->helper('url');
                $this->load->database();
                $this->load->helper('text');
                $this->load->library('pagination');
                $this->load->helper('form') ;
                //$this->load->library('MY_sphinxclient');

                $this->load->model('Search_model');
        }
    public function index()
    {

      if ($this->form_validation->run() === FALSE)
      {
        $this->load->view('search');
      }
      else
      {
      $data['search']=$this->input->get_post('search');
      $this->Search_model->get_search($data);
      $this->load->view('search');
      }
    }
      }

Search_model.php

<?php
        class Search_model extends CI_Model {

        public function __construct()
        {
        // Call the Model constructor
        parent::__construct();
        }
        public function get_search($search_submit)
        {         
          $sql = "SELECT  ads.id AS id, 1 AS table_id, location, provLabel, text,
          R_rand, addate, adcat.name AS catname, adsubcat.name AS subname, f_value 
          FROM ads 
          JOIN search_town ON search_town.townId=ads.townId 
          JOIN search_region ON search_region.regionId=search_town.relRegionId 
          JOIN search_prov ON search_prov.provId=search_region.relProvId 
          JOIN adcat ON adcat.id=ads.catid 
          JOIN adsubcat ON adsubcat.id=ads.subcatid 
          LEFT JOIN adfields ON adfields.ad_id=ads.id 
          WHERE ads.adactive=1 AND ads.subcatid!=841 AND scam=0 AND ads.catid!=8
          AND (text LIKE '%$search_submit%' OR f_value LIKE '%$search_submit%')";
          $query = $this->db->query($sql);
          return $query->result();
          $result= $query->result();
          echo count($result);

        }

        }

我的简单视图search.php

<?php
echo 'Hello World!!!';
if(isset($_POST)){
var_dump($_POST);
}

echo form_open('search');
echo form_label('Search: ');
$data_name = array(
'name' => 'search',
'id' => 'search',
'placeholder' => 'Search'
);
echo form_input($data_name);
echo form_submit('submit', 'Submit');
echo form_close( );

if(isset($search)){
echo '<table>';
foreach($search as $rows) {
    echo '<tr>
            <td>'.$rows->id.'</td>
            <td>'.$rows->text.'</td>
        </tr>';
}
echo '</table>';
}

search_submit();

3 个答案:

答案 0 :(得分:0)

您的模型名为Search_model,但您使用的是$this->search_model

不应该这样:

$this->search_model

是:

$this->Search_model 

或许您应该在执行加载时指定名称:

$this->load->model('Search_model', 'search');
$this->search->get_search();

检查您的最新修改:

$this->load->model('Search_model', 'search');
$this->search->get_search($seach); //Shouldn't $search be $this->input->get_post('search')

答案 1 :(得分:0)

在form_validation上使用equals而不是=== use ==

确保加载form_validation库

您缺少的form_validation-&gt; set_rules()

defined('BASEPATH') OR exit('No direct script access allowed');

class Search extends CI_Controller {
    public function __construct() {
    parent::__construct();
        // I would autoload Database
        $this->load->database(); 

        $this->load->helper('url');

        $this->load->helper('text');
        $this->load->library('pagination');
        $this->load->helper('form');
        $this->load->model('search_model');
    }

    public function index() {

        $this->load->library('form_validation');

        $this->form_validation->set_rules('search', 'Search');

        if ($this->form_validation->run() == FALSE) {

            $this->load->view('search');

        } else {

            $data['search'] = $this->input->post_get('search');

            $this->search_model->get_search($data);

            $this->load->view('search');

        }

    }
}

答案 2 :(得分:0)

好的,我把它排序了。 要对Db进行简单查询,例如,在Codeigniter 3中创建搜索表单

感谢大家的帮助!

控制器:

LIMIT 10

模特:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Search extends CI_Controller {
          public function __construct()
        {
                parent::__construct();
                $this->load->helper('url');
                $this->load->database();
                $this->load->helper('text');
                $this->load->library('pagination');
                $this->load->helper('form') ;
                $this->load->library('form_validation');
                $this->form_validation->set_rules('fsearch', 'Search', 'required');
                //$this->load->library('MY_sphinxclient');

                $this->load->model('Search_model');

        }
        public function index()
        {

          if ($this->form_validation->run() === FALSE)
          {
            $this->load->view('search');
          }
          else
          {
          $fsearch=$this->input->get_post('fsearch');
          $data['results']=$this->Search_model->get_search($fsearch);
          $this->load->view('search', $data);
          }
        }

      }

观点:

<?php
        class Search_model extends CI_Model {

        public function __construct()
        {
        // Call the Model constructor
        parent::__construct();
        }
        public function get_search($search)
        {         
          $sql = "SELECT  ads.id AS id, 1 AS table_id, location, provLabel, text,
          R_rand, addate, adcat.name AS catname, adsubcat.name AS subname, f_value 
          FROM ads 
          JOIN search_town ON search_town.townId=ads.townId 
          JOIN search_region ON search_region.regionId=search_town.relRegionId 
          JOIN search_prov ON search_prov.provId=search_region.relProvId 
          JOIN adcat ON adcat.id=ads.catid 
          JOIN adsubcat ON adsubcat.id=ads.subcatid 
          LEFT JOIN adfields ON adfields.ad_id=ads.id 
          WHERE ads.adactive=1 AND ads.subcatid!=841 AND scam=0 AND ads.catid!=8
          AND (text LIKE '%$search%' OR f_value LIKE '%$search%') GROUP BY ads.id ORDER BY ads.id DESC LIMIT 20";
          $query = $this->db->query($sql);
          return $query->result();
           echo 'Search Done';
        }

        }

?>