我对代码点火器很陌生。这是我的第一个应用程序。在我的数据库中,我插入了 empid 和 deptid。当两者都插入表中时,将显示所有数据。但是我希望当表中不存在两个数据时,所有数据也都显示为空白该列。如果表中不存在任何数据,则会显示一个添加按钮来代替删除 URL。
控制器
public function index()
{
$rows = $this->EmpDept_model->get_empdept();
$data['recdept'] = $this->EmpDept_model->get_empdept();
$this->load->helper('url');
$this->load->view('empdept/EmpDept_list',$data);
}
视图
<table class="table table-striped table-bordered" id="dataTables-example">
<thead>
<tr style="background-color: #d7ccc8;">
<th>Employee Name</th>
<th>Department Name</th>
<th>Option</th>
</tr>
</thead>
<tbody>
<?php
foreach($recdept as $r)
{
echo "<tr class='odd gradeX'>";
echo "<td>".$r->empname."</td>";
echo "<td>".$r->deptfname."</td>";
echo " <td> <a data-toggle='tooltip' data-placement='bottom' title='Delete' style='color: #000000;' href = '".base_url()."index.php/empdept/delete/".$r->empdeptid."'>Delete</a></td>";
/*echo "<td><a data-toggle='tooltip' data-placement='bottom' title='Edit' style='color: #000000;' href = '".base_url()."index.php/emp/edit/".$r->empid."'><span class='glyphicon glyphicon-pencil'></span></a>";
echo " || <a data-toggle='tooltip' data-placement='bottom' title='Delete' style='color: #000000;' href = '".base_url()."index.php/emp/delete/".$r->empid."'><span class='fa fa-ban fa-1x'></span></a></td>";*/
echo "</tr>";
}
?>
</tbody>
</table>
型
public function get_empdept()
{
$this->db->select('ed.empdeptid,e.empname,d.deptfname');
$this->db->from('empdept ed,empinfo e,deptinfo d');
$this->db->where('e.empid =ed.empid and d.deptid = ed.deptid');
$query = $this->db->get();
return $query->result();
}
试试这段代码:
public function get_empdept()
{
$this->db->select('ed.empdeptid,e.empname,d.deptfname');
$this->db->from('empdept ed');
$this->db->join('empinfo e','e.empid = ed.empid', 'left');
$this->db->join('deptinfo d','d.deptid = ed.deptid', 'left');
$query = $this->db->get();
return $query->result();
}
检查列是否为空,而不是设置添加按钮,否则删除按钮:
<table class="table table-striped table-bordered" id="dataTables-example">
<thead>
<tr style="background-color: #d7ccc8;">
<th>Employee Name</th>
<th>Department Name</th>
<th>Option</th>
</tr>
</thead>
<tbody>
<?php
foreach($recdept as $r)
{
echo "<tr class='odd gradeX'>";
echo "<td>".$r->empname."</td>";
echo "<td>".$r->deptfname."</td>";
if(!empty($r->empdeptid)){
echo " <td> <a data-toggle='tooltip' data-placement='bottom' title='Delete' style='color: #000000;' href = '".base_url()."index.php/empdept/delete/".$r->empdeptid."'>Delete</a></td>";
} else {
echo " <td> <a data-toggle='tooltip' data-placement='bottom' title='Add New' style='color: #000000;' href = '".base_url()."index.php/empdept/add/'>Add</a></td>";
}
echo "</tr>";
}
?>
</tbody>
</table>
您必须使用union
来实现此目的。但是代码点火器不支持联合
所以你可以尝试像以下.更改模型中的查询
$this->db->query("Select empdeptid from empdept union Select empname from empinfo union Select deptfname from deptinfo where empinfo.empid = empdept.empid and deptinfo.deptid = empdept.deptid");