如果数据库表中没有数据,我怎么能在codeigniter视图中回显“找不到电子邮件”?

时间:2016-06-08 10:35:00

标签: php codeigniter

我必须在我的视图中打印找不到电子邮件等内容。

我的控制器看起来:

public function viewEmailMessage()
    {

        if(($this->session->userdata('username')!=""))
        {
                 $data =array();
                $data['email']=$this->adminModel->viewEmailMessageModel();
                $this->load->view('admin/viewEmailMessages',$data);
        }
        else
        {
            $this->load->view('admin/login');
        }
    }

我的模特看起来:

   public function viewEmailMessageModel()  
  {  

        $this->db->select("*");
        $this->db->from('message');  
         $this->db->order_by("id", "desc");
        $query = $this->db->get();
        return $result=$query->result();  
  }

我的观点看起来:

<h4>View Emails</h4>


            <table class="table">
              <thead>
                <tr>
                  <th>Id</th>
                  <th>Name</th>
                  <th>Email</th>
                  <th>Message</th>

                   <th>options</th>                 
                </tr>
              </thead>
              <tbody>
               <?php  
                  foreach ($email as $row)  
                  {  
                    ?>
                <tr>

                  <td><?php echo $row->id;?></td>

                    <td><?php echo $row->name;?></td>
                    <td><?php echo $row->email;?></td>
                    <td><?php echo $row->message;?></td>

                  <td> 

                  <a href="<?php echo "http://localhost/VI/admin/deleteEmail?id=".$row->id ?>"  onclick="return confirm('Are you sure to delete?')"><i class="fa fa-trash fa-lg "></i></a>
                  </td>
                </tr>
                  <?php }  
                  ?> 


              </tbody>
            </table>

如何修改控制器,型号和放大器图即可。因为当message表为空时,我必须在同一视图中打印“No Emails Found”之类的内容。

3 个答案:

答案 0 :(得分:1)

在视图中试试这个

 <h4>View Emails</h4>


        <table class="table">
          <thead>
            <tr>
              <th>Id</th>
              <th>Name</th>
              <th>Email</th>
              <th>Message</th>

               <th>options</th>                 
            </tr>
          </thead>
          <tbody>
           <?php  
            if(count($email) > 0 ){
              foreach ($email as $row)  
              {  
                ?>
            <tr>

              <td><?php echo $row->id;?></td>

                <td><?php echo $row->name;?></td>
                <td><?php echo $row->email;?></td>
                <td><?php echo $row->message;?></td>

              <td> 

              <a href="<?php echo "http://localhost/VI/admin/deleteEmail?id=".$row->id ?>"  onclick="return confirm('Are you sure to delete?')"><i class="fa fa-trash fa-lg "></i></a>
              </td>
            </tr>
              <?php }  
              } else {
                 echo "msg here";
              }
              ?> 


          </tbody>
        </table>

答案 1 :(得分:0)

<h4>View Emails</h4>


            <table class="table">
              <thead>
                <tr>
                  <th>Id</th>
                  <th>Name</th>
                  <th>Email</th>
                  <th>Message</th>

                   <th>options</th>                 
                </tr>
              </thead>
              <tbody>
               <?php  
                  if($email and other condition){
                  foreach ($email as $row)  
                  {  
                    ?>
                <tr>

                  <td><?php echo $row->id;?></td>

                    <td><?php echo $row->name;?></td>
                    <td><?php echo $row->email;?></td>
                    <td><?php echo $row->message;?></td>

                  <td> 

                  <a href="<?php echo "http://localhost/VI/admin/deleteEmail?id=".$row->id ?>"  onclick="return confirm('Are you sure to delete?')"><i class="fa fa-trash fa-lg "></i></a>
                  </td>
                </tr>
                  <?php }  
}
else{
echo your message;
}
                  ?> 


              </tbody>
            </table>

答案 2 :(得分:0)

使用count()功能

并且无法使用if(!empty($email))来使用数组变量count()

    if(count($email)==0)
    {

      echo "NO email found";

    }
    else
    {
        foreach($email  as $row)
        {
          .........
        }

    }