php - Codeigniter 从哪里选择

标签 php mysql codeigniter

只是想让 codeigniter 过滤器表单工作。例如,如果从下拉列表中选择“Samsung”并从复选框字段中选择“2G”,则应返回 id 为 1 和 4 的行。但我的模型什么也没返回。我认为问题出在模型的 IF 语句中,但我无法找出确切的原因是什么。请帮助我。

这是我的数据库表:

database table

这是我的过滤表单:

filter form

这是我的模型:

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

        class Model_example extends CI_Model {

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

        public function did_filter() {


       $types = $this->input->post('types');

            $data = array(
                '2g' => 0,
                    '3g' => 0,
                    '4g' => 0,                
                         );                  
            foreach ($types as $type) {
                                           $data[$type] == 1;
                                      }
    $this->db->select('*');
    $this->db->from('table_example');
    $this->db->where('phone', $this->input->post('phone'));

                if (
 $query = $this->db->get()   
    )     
    {
    if (
        (('2g' == 1) AND ($data['2g'] == 1)) OR
        (('3g' == 1) AND ($data['3g'] == 1)) OR  
        (('4g' == 1) AND ($data['4g'] == 1)))
       {                 
       return $result = $query->result_array();
       } 
        else {
        return false;
        }               
           }

    else {
    return false;
    } 

            }  
        }

这是我的 View 1:

 <?php 

             $this->load->helper("form","file");

             echo validation_errors();              

             echo form_open_multipart("example/search");

             echo form_label("Phone:<br>","phone");
             $data = array(
                  "" => "Select Phone",
                  "samsung" => "Samsung",
                  "htc" => "HTC",
                  "nokia" => "Nokia",
                );
             echo form_dropdown('phone', $data, set_value('phone'));

             echo br(5);
              ?> 

             <?php echo form_label("Network Type:<br>","type");?>                   
<input type="checkbox" name="types[]" value="2g" id="types"  <?php echo set_checkbox('types[]', '2g', FALSE); ?>/>2G<br />
<input type="checkbox" name="types[]" value="3g" id="types" <?php echo set_checkbox('types[]', '3g', FALSE); ?>/>3G<br />
<input type="checkbox" name="types[]" value="4g" id="types" <?php echo set_checkbox('types[]', '4g', FALSE); ?>/>4G<br />
<br />          
             <?php

             echo br(1);

             echo form_submit("filterSearch", "Search");

             echo form_close();

             ?>

这是我的观点 2:

<?php 

       print_r($result);     

这是我的 Controller :

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

class Example extends MX_Controller {

    public function index() { //main function

        $this->load->view("view_example");
    }

public function search() { 

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

            $this->form_validation->set_rules('phone', 'Phone','required');
            $this->form_validation->set_rules('types[]', 'Network Type','required'); 

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

            $data["result"] = $this->model_example->did_filter();


            $this->load->view("view_search_results",$data); 

            }
            else
            {

            $this->load->view("view_no_search_results");

             }

    }
}

最佳答案

试试这个,它应该可以工作,我给了你两个选项,所以使用你需要的一个

 public function did_filter() {

    $types = $this->input->post('types');

    //list of types column name you can ignore this part by in cases someone change the value in html your query will fail so i am keeping this
    $data = array(
        '2g' => 0,
        '3g' => 0,
        '4g' => 0,
    );

    $this->db->select('*');
    $this->db->from('table_example');
    $this->db->where('phone', $this->input->post('phone'));

    // if you want to use and where use this block, or use the next block the is commented out
    foreach ($types as $type) {
        if (isset($data[$type])) { // this making sure that your column is correct 
            $this->db->where($type, 1);
        }
    }

    /**
    //If you want your checkbox to work as or, ie if 2g and 3g select and you want to show any check box match. 
    //In case of your example still it will give row 1 and 4, but if you use fist block it will give you row 1 only because row 1 got both 2g and 3g
    $or_where = array();
    foreach ($types as $type) {
        if (isset($data[$type])) { // this makeing sure that your colum is correct 
            $or_where[] = "$type = 1";
        }
    }
    if (count($or_where) > 0) {
        $where = implode(' OR ', $or_where); // make the or where for array
        $this->db->where("($where)");
    }
     * 
     */

    $query = $this->db->get();

    if ($query && $query->num_rows() > 0) {
        return $query->result_array();
    } else {
        return false;
    }
}

关于php - Codeigniter 从哪里选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22512209/

相关文章:

php - 在 PHP 中从 mysql 检索图像时无法查看图像

javascript - 每次使用 Codeigniter 单击打开 Bootstrap Modal 时如何调用 javascript 函数?

php - Postgres,查询错误

php - Doctrine 选择多个对象

php - 如何删除特定页面的缓存

mysql - 使用不同行中的值对同一个表进行查询

php - 即使已调用 Controller ,也不会触发 CodeIgniter 索引函数

php - 将 2700 多个文件名与数据库表中的 29000 多行进行比较

PHP & MySQL : Query to "sum" 2 rows from PHP

sql - 我如何使用 SQL 来选择重复记录以及相关项目的计数?