douxuanling6523 2016-08-04 05:53
浏览 45
已采纳

从php(CodeIgniter)中的给定数组中选择mysql表中的列。

I want to select columns form mysql table which I have selected names as input value. In my view, have to select column names as multiple input fields. In this selection option values are equals to column names in my “pass_due” table in database.

<form id=""  name=" Passdue "  action="<?=base_url('index.php/Passdue_ctrl/select')?>" method="post">
<div >
     <input class="date-picker" id="report_date" name="report_date"   value="" placeholder="Select Date"/>
     <select multiple="" class="chzn-select" id=" " data-placeholder="Select sections " name="table_feilds">
                           <option value="" />
                           <option value="below_1" /> Below month
                           <option value="month_1_3" /> Month 1-3
                           <option value="month_3_6" /> Month 3-6
                            <option value="month_6_9" /> Month 6-9
                            <option value="over_9" /> Over 9 month
      </select>
</div>
<div>
            <button type="submit" class="btn btn-mini btn-info">Submit</button> 
              <button type="reset" id="reset" class="btn btn-mini btn-info">Reset</button>
</div>   
</form>

This is function in my controller

Function select (){
$fld_name =$this->input->post('table_feilds');
$reportdate=$this->input->post('report_date');
$report=$this->Passdue_model->get_report_part($fld_name,$reportdate);
If($report){
$this->data['report_part']=$report;
$this->data['fld_name']=$fld_name;
$this->load->view('Passdue_other_view',$this->data);
}
}

In my model like this.

function get_report_part1($fld_name,$reportdate)
    {
$this->db->select($fld_name);
$this->db->from(‘pass_due’);
$this->db->where('fld_actORinact_date <=',$reportdate);
            $query = $this->db->get();
                if($query){
                 return $query->result();
                }
   }

When I run this code it select all columns from table, not only selected ones. And also it shows error as Invalid argument supplied for foreach() .

</div>
  • 写回答

3条回答 默认 最新

  • dook0034 2016-08-04 07:32
    关注

    You can use this new model method to retreive data from your db.So insert this method into your model

    function getDetail($tablename = '', $columns_arr = array(), $where_arr = array(), $limit = 0, $offset = 0)
    {
        $limit = ($limit == 0) ? Null : $limit;
    
        if (!empty($columns_arr)) {
            $this->db->select(implode(',', $columns_arr), FALSE);
        }
    
        if ($tablename == '') {
            return array();
        } else {
            $this->db->from($tablename);
    
            if (!empty($where_arr)) {
                $this->db->where($where_arr);
            }
    
            if ($limit > 0 AND $offset > 0) {
                $this->db->limit($limit, $offset);
            } elseif ($limit > 0 AND $offset == 0) {
                $this->db->limit($limit);
            }
    
            $query = $this->db->get();
    
            return $query->result();
        }
    }
    //These are include within controller methods
    
    $fld_name =$this->input->post('table_feilds');
    //you have to send your columns from your multiple select object as array.
    //create column array
    $arr_columns=array();
    for($i=0,$i<=count($fld_name);$i++){
    $arr_columns=array_push($fld_name)
    }
    $reportdate=$this->input->post('report_date');
     //create where array
    $arr_where=array('fld_actORinact_date <=' => $reportdate);
    //retreive data
    $datatable=‘pass_due’;
       $report=$this->Passdue_model->getDetail($datatable,$arr_columns,$arr_where,0,0);
    var_dump($report);
    
    //finally dump_data set .it return array object.Then loop retrieved object.
    

    then this will return what you expect. Also like to advise you to use generalized model except using separate models for each & every tables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度