我怎样才能用不同的表格检查代码。
如果表中已有数据,STAFF用户只允许编辑
否则用户允许在该编辑后永久插入。
关于代码注释的更多解释 希望能够理解。谢谢你掌握。
控制器
public function Test()
{
$d['checking'] = $this->db->query("SELECT status,amount,br_loc FROM account");
$d['test'] = $this->db->query("SELECT br_loc FROM staff");
$this->load->view('Account/global/header');
$this->load->view('test',$d);
$this->load->view('Account/global/footer');
}
查看
<table class="table table-hover table-condensed">
<thead>
<tr>
<th></th>
<th>Code</th>
</tr>
</thead>
<tbody>
<?php
foreach($checking as $c)
{
foreach($data->result_array() as $row) // data select from table ACCOUNT and show as array..From table ACCOUNt data will insert to table STAFF.
{
?>
<tr>
<td><?php echo $row['br_loc'];?></td> <!--CHECKING -if data already insert in table staff show button edit else button save to insert-->
<?php if($c['status'] == 'save')
{ ?>
<td><button type="button" value ='Edit' class="btn btn-primary" id="edit<?php echo $row['br_loc'];?>" >Edit</button></td>
<?php }
else
{ ?>
<td><button type="button" value ='Save' class="btn btn-primary" id="save<?php echo $row['br_loc'];?>" >save</button></td>
<?php } ?>
</tr>
<?php
}
}
?>
</tbody>
</table>
结果
保持循环和重复。
答案 0 :(得分:0)
除非我遗漏了一些东西,但我觉得你的循环错了
您在控制器中设置了查询,但在视图中您没有接触到结果....
这样做 $ checking-&GT; result_array() $测试 - &GT; result_array()
<table class="table table-hover table-condensed">
<thead>
<tr>
<th></th>
<th>Code</th>
</tr>
</thead>
<tbody>
<?php
foreach($test->result_array() as $row) // data select from table ACCOUNT and show as array..From table ACCOUNt data will insert to table STAFF.
{
?>
<tr>
<td><?php echo $row['br_loc'];?></td> <!--CHECKING -if data already insert in table staff show button edit else button save to insert-->
<?php
$tempStatus = null;
foreach($checking->result_array() as $c){
if($c['br_loc'] == $row['br_loc']){
$tempStatus = $c['status'];
}
}
if($tempStatus == 'save')
{ ?>
<td><button type="button" value ='Edit' class="btn btn-primary" id="edit<?php echo $row['br_loc'];?>" >Edit</button></td>
<?php }
else
{ ?>
<td><button type="button" value ='Save' class="btn btn-primary" id="save<?php echo $row['br_loc'];?>" >save</button></td>
<?php } ?>
</tr>
<?php
}
?>
</tbody>
</table>
所以我猜你想要这样的东西吗? 同样对于codeigniter我会推荐模板插件,它会让你的生活更轻松,而不必加载大量的观点。