基于result_array()的条件

时间:2013-12-02 14:36:46

标签: php codeigniter

我正在使用codeigniter,我想根据模型的返回值创建一个条件。

这是模型:

    public function get_sentitems()
{
    $query = $this->db->get('sentitems');
    return $query->result_array();
}

这是控制器:

    public function sentitems()
{
    $data['sentitems'] = $this->sms_model->get_sentitems();

    if($data['sentitems']['Status'] === 'SendingOKNoReport')
    {
        $data['status_message'] = 'Sent';
    }
    else
    {
        $data['status_message'] = 'Failed';
    }

    $data['title'] = ucwords('sent items');

    $this->load->view('templates/header', $data);
    $this->load->view('sms/sentitems', $data);
    $this->load->view('templates/footer');
}

这是视图

    <h2><?php echo $title; ?></h2>
<table border="1" width="100%">
    <thead>
        <tr>
            <th>No.</th>
            <th>Tujuan</th>
            <th>Waktu</th>
            <th>Isi</th>
            <th>Ket.</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($sentitems as $sentitems_item): ?>
        <tr>
            <td><?php echo $sentitems_item['ID']; ?></td>
            <td><?php echo $sentitems_item['DestinationNumber']; ?></td>
            <td><?php echo $sentitems_item['SendingDateTime']; ?></td>
            <td><?php echo $sentitems_item['TextDecoded']; ?></td>
            <td><?php echo $sentitems_item['Status']; ?></td>
        </tr>
    <?php endforeach ?>
    </tbody>
</table>

我有一个列状态,但为什么浏览器中的结果总是如下:

  

遇到PHP错误   严重性:通知
  消息:未定义的索引:状态
  文件名:controllers / sms.php
  行号:62

解决方案是什么?抱歉我的英语不好。

1 个答案:

答案 0 :(得分:0)

问题在于,您的控制器$data['sentitems']包含数据库中的一系列结果,因此您无法使用$data['sentitems']['Status']获取结果。你实际拥有的是

$data['sentitems'][0]['Status']
$data['sentitems'][1]['Status']
$data['sentitems'][2]['Status']

等等。

我认为此时您最好的选择是从控制器中删除以下内容:

if($data['sentitems']['Status'] === 'SendingOKNoReport')
{
    $data['status_message'] = 'Sent';
}
else
{
    $data['status_message'] = 'Failed';
}

并修改输出状态的模板行:

<td><?php echo $sentitems_item['Status'] == 'SendingOKNoReport' ? 'Sent' : 'Failed'; ?></td>
相关问题