【问题标题】:Select columns from mysql table by given array in php(CodeIgniter).通过 php(CodeIgniter) 中的给定数组从 mysql 表中选择列。
【发布时间】:2016-12-10 02:37:35
【问题描述】:

我想从我选择名称作为输入值的 mysql 表中选择列。在我看来,必须选择列名作为多个输入字段。在此选择选项中,值等于数据库中我的“pass_due”表中的列名。

<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>

这是我的控制器中的功能

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);
}
}

在我的模型中是这样的。

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();
        		}
   }

当我运行此代码时,它会从表中选择所有列,而不仅仅是选定的列。它还显示错误为 为 foreach() 提供的参数无效。

【问题讨论】:

  • 你能在评论中回显查询并粘贴吗?使用 echo $this->db->last_query();

标签: php mysql codeigniter


【解决方案1】:

您可以使用这个新的模型方法从您的数据库中检索数据。所以将此方法插入您的模型中

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.

那么这将返回您所期望的。 还想建议您使用通用模型,除了为每个表使用单独的模型。

【讨论】:

    【解决方案2】:
    use below code in form
    
        <select multiple="multiple" class="chzn-select" id=" " data-placeholder="Select sections " name="table_feilds[]">  <!-- use [] for multiple values --> 
    
    and I don't see any foreach loop?
    
    and first 
    echo '<pre>';print_r($fld_name);exit; before 
    $report=$this->Passdue_model->get_report_part($fld_name,$reportdate);
    

    【讨论】:

    • 在我的数据视图中,有foreach循环来写列名&lt;thead&gt; &lt;tr&gt; &lt;?php foreach((array)$fld_name as $fld_name){?&gt; &lt;th&gt;&lt;?=$fld_name?&gt;&lt;/th&gt; &lt;?php }?&gt; &lt;/tr&gt; &lt;/thead&gt;
    • 您是否尝试过使用更新的选择标签并查看 print_r($fld_name);显示 print_r($fld_name);选择一些后
    • 上述代码是否有效,非常酷,非常感谢。也请投票
    【解决方案3】:

    在您的模型中返回如下查询

    return $query->result_array();
    

    在你的控制器中获取数据

    data['fld_name']=$this->Passdue_model->get_report_part($fld_name,$reportdate);
    $this->load->view('Passdue_other_view',$data);
    

    在您的查看页面中

    foreach($fld_name as $fld_name){?> <th><?php echo $fld_name['column_name '];?></th> <?php }?> </tr> </thead>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-14
      • 1970-01-01
      • 2011-11-18
      • 2023-03-24
      • 2015-07-21
      • 2010-11-09
      相关资源
      最近更新 更多