Codeigniter搜索Db,处理它并在提交

时间:2018-03-05 22:53:34

标签: php mysql codeigniter

我正在codeigniter上构建一个应用程序,用户搜索特定类型的立法。 我已经建立了主页,但我在如何构建结果页面方面有点困难(我是codeigniter的新手)。提交时,我需要将数据发布到后端,并让浏览器重定向到渲染结果页面。

搜索的某些选项是可选的,因此我还需要考虑搜索。

任何帮助我构建此页面的答案都将不胜感激

编辑:进一步澄清。 搜索的可选参数是视图中的隐藏参数。 当用户单击该复选框时,将显示db中显示的选项列表,并选择其中一个。

编辑:db的结构示例。

Id:1

法律:民事

主题:赡养费

主题:家庭法

地区:离婚法

法律文本:第12330/2012号法律第1条......

结果页面返回法律文本。

(例如:如果用户检查“法律”框,则会出现一系列法律选项:民法,刑法等。当他选择一个选项时,结果页面应该返回所选实例的所有实例)

以下是主页的控制器,模型和视图:

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

class Home extends CI_Controller {

public function index(){

    $this->load->model('Index_model', 'index');


    $dados['law'] = $this->index->get_law();
    $dados['area'] = $this->index->get_area();
    $dados['subjects'] = $this->index->get_subject();
    $dados['themes'] = $this->index->get_theme();
    //the index model query a specific table from the db

    $this->load->view('include/header_public');
    $this->load->view('public/home', $data);

}


}

型号:

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

class Index_model extends CI_Model{

public function __construct()
{
    parent::__construct();
}

public function get_index($limit = null, $offset = null){
    if($limit){
        $this->db->limit($limit, $offset);
    }



public function get_law(){
    $query = $this->db->get('tb_law');
    return $query->result();
}


public function get_area(){
    $query = $this->db->get('tb_area');
    return $query->result();
}

public function get_subject(){
    $query = $this->db->get('tb_subject');
    return $query->result();
}

public function get_theme(){
    $query = $this->db->get('tb_theme');
    return $query->result();
}


}

查看(因为视图有点大,使用材质设计精简版构建,我只会在代码的一小部分内部加上,因为它会自行重复):

<main class="mdl-grid">
<div class="mdl-cell mdl-cell--6-col">
    <div class="search-section">
        <h4 class="search-title">What are you searching today</h4>
        <form method="POST">
  <div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label      mdl-cell--12-col-desktop">
                <input class="mdl-textfield__input" type="text" id="search" name="search" required>
                <label class="mdl-textfield__label" for="search">Search now</label>
 </div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label  mdl-cell--8-col-desktop">

              <input type="checkbox" id="lawcheck" name="law" value="law"
              onclick='toggleLawVisibility'>
              <label for="law">Law</label>
            </div>

            <div id='lawdiv' class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label  mdl-cell--4-col-desktop">
                            <select class="browser-default" name="law" id="law" required>
                                <option value="" disabled selected>law</option>
                                <?php foreach($laws as $law){?>
                                <option value="<?= $law->CO_LAW?>">
                                    <?= $law->DS_LAW?>
                                </option>
                                <?php } ?>
                            </select>
                        </div>

<!-- Code repeats for area,subject, etc -->
...
  <button type="submit"class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect btn-search">
                Search
            </button>
          </form>
        </form>
    </div>
<script>
document.getElementById('lawCheck').onchange = function() {
document.getElementById('lawdiv').style.visibility = this.checked ? 'visible' : 'hidden';
};
<!--Same script for area, subject, etc -->
</script>

提前感谢您的帮助!!

1 个答案:

答案 0 :(得分:0)

这可能会帮助您入门。基本上,它使用数组搜索不同的表:$search_tbl = array('projects' => 'project_name', 'testimonials' => 'company');其中key是表名,value是您要搜索的字段。

    // controller/search/{$term}
    public function search($term = null) {

        if (!is_null($term)) {
            $term = urldecode($term);
            echo 'Term to search: ' . $term . '<br>';
        } else {
            echo 'Returning all results... <br>';
        }

        $search_tbl = array('tblname1' => 'field_in_tbl1', 'tblname2' => 'field_in_tbl2');

        $master = array();

        foreach ($search_tbl as $tbl_name => $tbl_search_field) {
            $this->db->like($tbl_search_field, $term);
            $q = $this->db->get($tbl_name);
            if ($q->num_rows() > 0) {
                $data = array();
                $master = array_merge($master, $q->result_array());
                /*
                 * Do tables not have the same structure?
                 * You might have to do something like this to pick out only the "shared" field names
                 * Otherwise you can't loop the results in a structured way...
                 * 
                foreach ($q->result_array() as $row) {
                    $data[] = array(
                        'field1' => $row['field1'],
                        'field2' => $row['field2']
                    );
                }
                $master = array_merge($master, $data);
                 * 
                 */
            } else {
                echo 'No results for table: ' . $tbl_name . '<br>';
            }
        }

        if (count($master) < 1) {
            echo 'No results!<br>';
        } else {
            echo '<pre>';
            print_r($master);
        }
    }