SQL 从 codeigniter 中的复选框查询动态 table 列选择?
SQL query for dynamic table column selection from checkbox in codeigniter?
我正在尝试根据 codeigniter 中的复选框 selection 从数据库中检索列。但是我不知道该怎么做?
例如数据库中有一个列table成员:
| ID | Name | Gender | Status |
---------------------------------------
| 1 | Anne | F | Member |
| 2 | Brown | M | Member |
| 3 | Carl | M | Member |
然后 data.php 视图中的复选框 select 要显示的列
<?php echo form_open(data/getData);?>
<label class="col-md-4" for="parameter">Parameter</label>
<div class="col-md-6">
<input type="checkbox" id="parameter" value="name"> Name <br>
<input type="checkbox" id="parameter" value="gender"> Gender <br>
<input type="checkbox" id="parameter" value="status"> status <br>
</div>
<input type="submit" class="btn btn-md btn-info" value="Process" />
<?php echo form_close();?>
<div class="well">
<table class="table table-bordered table-hover" cellspacing="0" width="100%">
<thead>
<tr class="info">
<th>ID</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach($query as $d): ?>
<tr>
<td><?php echo $d['ID'];?></td>
<td><?php echo $d[''];?></td>
<td><?php echo $d[''];?></td>
<td><?php echo $d[''];?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>
如何动态生成从复选框中检索的列名?
Data_model.php:
function getData(){
//I don't know how to get the dynamic column select from the view to the select query
$this->db->select(?);
$this->db->from('member');
$query = $this->db->get();
return $query->result();
}
有人帮我解决这个问题吗?
试试这个简单的方法,将一个 POST/GET 的名称属性设为 param[]
数组
<input type="checkbox" id="parameter" name="param[]" value="name"> Name <br>
<input type="checkbox" id="parameter" name="param[]" value="gender"> Gender <br>
<input type="checkbox" id="parameter" name="param[]" value="status"> status <br>
in PHP(假设 post 值)
$param = $this->input->post('param');
//if no param get all columns
if(empty($param)){
$param = '*';
}else{
//if array value is more than 1 use implode else just take the 1st array
$param = (count($param) > 1 ) ? implode(',', $param) : $param[0]; //make a single string and concatenate them with ,
}
example output: name,status
然后像这样在模型中使用 $param $this->db->select("$param");
我正在尝试根据 codeigniter 中的复选框 selection 从数据库中检索列。但是我不知道该怎么做?
例如数据库中有一个列table成员:
| ID | Name | Gender | Status |
---------------------------------------
| 1 | Anne | F | Member |
| 2 | Brown | M | Member |
| 3 | Carl | M | Member |
然后 data.php 视图中的复选框 select 要显示的列
<?php echo form_open(data/getData);?>
<label class="col-md-4" for="parameter">Parameter</label>
<div class="col-md-6">
<input type="checkbox" id="parameter" value="name"> Name <br>
<input type="checkbox" id="parameter" value="gender"> Gender <br>
<input type="checkbox" id="parameter" value="status"> status <br>
</div>
<input type="submit" class="btn btn-md btn-info" value="Process" />
<?php echo form_close();?>
<div class="well">
<table class="table table-bordered table-hover" cellspacing="0" width="100%">
<thead>
<tr class="info">
<th>ID</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach($query as $d): ?>
<tr>
<td><?php echo $d['ID'];?></td>
<td><?php echo $d[''];?></td>
<td><?php echo $d[''];?></td>
<td><?php echo $d[''];?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
</div>
如何动态生成从复选框中检索的列名?
Data_model.php:
function getData(){
//I don't know how to get the dynamic column select from the view to the select query
$this->db->select(?);
$this->db->from('member');
$query = $this->db->get();
return $query->result();
}
有人帮我解决这个问题吗?
试试这个简单的方法,将一个 POST/GET 的名称属性设为 param[]
数组
<input type="checkbox" id="parameter" name="param[]" value="name"> Name <br>
<input type="checkbox" id="parameter" name="param[]" value="gender"> Gender <br>
<input type="checkbox" id="parameter" name="param[]" value="status"> status <br>
in PHP(假设 post 值)
$param = $this->input->post('param');
//if no param get all columns
if(empty($param)){
$param = '*';
}else{
//if array value is more than 1 use implode else just take the 1st array
$param = (count($param) > 1 ) ? implode(',', $param) : $param[0]; //make a single string and concatenate them with ,
}
example output: name,status
然后像这样在模型中使用 $param $this->db->select("$param");