如果在Codeigniter中从db循环数据时使用Statement

时间:2015-03-03 21:34:58

标签: php codeigniter model-view-controller

我想在循环创建表时检查我的任务ID。有可能吗???

             <table class="data-table">
                <thead>
                    <tr>
                        <th>No.</th>
                        <th>ID Admin</th>
                        <th>IP Address</th>
                        <th>Date</th>
                        <th>Time</th>
                        <th>Admin Activity</th>
                        <th>ID Stuff</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    foreach($all_data->result() as $data){
                    echo '
                        <tr>
                            <td>'.$data->id_history.'</td>
                            <td>'.$data->id_admin.'</td>
                            <td>'.$data->ipc.'</td>
                            <td>'.$data->task_date.'</td>
                            <td>'.$data->task_time.'</td>
                            'if ($data->id_task == 1){'  <========
                                <td>Login Site</td>      <========
                            '}else{'                     <========
                                <td>Logout Site</td>     <========
                            '}'                          <========
                            <td>-</td>
                        </tr>
                        ';
                        }
                    ?>          
                </tbody>
            </table>

这是用于在视图中循环数据的代码。

function get_log_history(){
        $this -> db -> select('id_history, id_admin, ipc, task_date, task_time, id_task, id_stuff');
        $this -> db -> from('tbl_history');
        $this -> db -> where('id_task', 1);
        $this -> db -> or_where('id_task', 2);

        return $query = $this -> db -> get();
    }

这是模型的代码。来自tbl_task的id task(外键)。

Table relationship

1 个答案:

答案 0 :(得分:1)

好的,我发现了它。你不能在这样的回声中放置一个if ..

这应该是它的样子:

<?php foreach($all_data->result() as $data){ ?>
      <tr> 
           <td><?php echo $data->id_history;?></td> //no short tags
           <td><?=$data->id_admin;?></td> //using short tags
           //your other echos
    <?php if ($data->id_task == 1){ ?>
           <td>Login Site</td>  
    <?php }else{ ?>
           <td>Logout Site</td>
    <?php } ?>
       </tr>
    <?php } ?>

或者使用三元运算符,它看起来像这样......

<?php foreach($all_data->result() as $data){ ?>
      <tr> 
           <td><?php echo $data->id_history;?></td> //no short tags
           <td><?=$data->id_admin;?></td> //using short tags
           //your other echos
           <td><?=$data->id_task===1?"Login Site":"Logout Site";?></td>
      </tr>
<?php } ?>