result_array值未在codeigniter中的foreach循环内显示

时间:2019-05-27 05:50:59

标签: php codeigniter mysqli

我只是使用Codeigniter触发一个选择查询,当我使用print_r函数时所有值都显示出来,但是当我使用foreach循环时却没有值显示我不知道是什么问题是吗我该如何解决这个问题?

$this->db->select('*');
    $this->db->from('user_detail');
    $where = "jid='".$jid_final."' and cid='".$cid_final."' and offer='1'";
    $this->db->where($where);
    $sql_user = $this->db->get();
    $result_user = $sql_user->result_array();
    print_r($result_user);
    foreach($result_user as $row_user)
    {

        $user_data = '<tr>
                        <td class="td">'.$row_user['uid'].'</td>
                        <td class="td">'.$row_user['fname'].' '.$row_user['lname'].'</td>
                        <td class="td">'.$row_user['email'].'</td>
                        <td class="td">'.$row_user['phone'].'</td>
                    </tr>';
    }
    echo $user_data;

谢谢

2 个答案:

答案 0 :(得分:0)

使用下面的查询,需要检查表中是否有数据,然后将其传递给foreach循环。

 $user_data = '';
 $this->db->select('*');
    $where = "jid='".$jid_final."' and cid='".$cid_final."' and offer='1'";
    $this->db->where($where, NULL, FALSE);
    $result_user = $this->db->get('user_detail')->result_array();

    if($result_user) {
    foreach($result_user as $row_user)
    {

        $user_data .= '<tr>
                        <td class="td">'.$row_user['uid'].'</td>
                        <td class="td">'.$row_user['fname'].' '.$row_user['lname'].'</td>
                        <td class="td">'.$row_user['email'].'</td>
                        <td class="td">'.$row_user['phone'].'</td>
                    </tr>';
    }
     echo $user_data;
    } else {
     echo "no data found";
    }

答案 1 :(得分:0)

尝试将值与.串联

$user_data = '';
if(isseet($result_user) && count($result_user) > 0){
foreach($result_user as $row_user)
{

    $user_data .= '<tr>
                    <td class="td">'.$row_user['uid'].'</td>
                    <td class="td">'.$row_user['fname'].' '.$row_user['lname'].'</td>
                    <td class="td">'.$row_user['email'].'</td>
                    <td class="td">'.$row_user['phone'].'</td>
                </tr>';
}
    echo $user_data;
}else{
    echo 'Record not found';
}