无法使用Codeigniter从数据库中获取部分数据

时间:2018-10-04 09:11:15

标签: php html codeigniter

我在Codeigniter中是新手。这是我的第一个应用程序。在我的数据库中,我插入了empid和deptid。当两者都插入表中时,将显示所有数据。但是我想当两个数据都不在表中时,所有数据也都以空白显示。如果表中没有任何数据,则会显示一个添加按钮来代替删除URL。

  

控制器

log4j.rootLogger=DEBUG, Appender1,Appender2

log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n

log4j.appender.Appender2=org.apache.log4j.FileAppender
log4j.appender.Appender2.File=C:\\webapp1.log
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
  

视图

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>

2 个答案:

答案 0 :(得分:1)

尝试以下代码:

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>

答案 1 :(得分:0)

您必须使用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");