简体   繁体   English

PHP-CODEIGNITER如何在会话中获取数据

[英]PHP-CODEIGNITER how to get data in session

I want echo my list of trip, when I try print_r the value can show but when I echo the result always 我想回显我的旅行清单,当我尝试print_r时,该值可以显示,但是当我总是回显结果时

Message: Undefined variable: adventure 消息:未定义的变量:冒险

Filename: views/profile.php 文件名:views / profile.php

Line Number: 121 行号:121

Backtrace: 回溯:

Severity: Warning 严重程度:警告

Message: Invalid argument supplied for foreach() 消息:为foreach()提供了无效的参数

Filename: views/profile.php 文件名:views / profile.php

Line Number: 121 行号:121

this is my controller : 这是我的控制器:

 public function getListTrip(){         
    $this->load->model('userModel');        
    $data['adventure'] = $this->userModel->getTrip()->result_array();
    //echo ($data);
    $this->load->view('profile', $data);        
 }

and this is my model : 这是我的模型:

function getTrip(){
    $userId = $this->session->userdata('user_id');    
    return $this->db->get_where('adventure',['user_id' => $userId]);
}

this is my view 这是我的看法

                        <table>
                           <?php
                            foreach ($adventure as $b){
                                echo "<tr>
                                <td>$b->name</td>
                                <td>$b->place</td>
                                <td>$b->category</td>

                                </tr>";
                            }

                        ?>
                        </table>

so how should I edit my code to make the value show in my page whit echo or foreach not in print_r... thanks a lot 所以我应该如何编辑我的代码以使值显示在我的页面上,而echoforeach不在print_r中显示。

echo is used to output one or more strings, since your $data is an array you see the error, you need to pass data to view and iterate it in the view itself, like: echo用于输出一个或多个字符串,因为$data是一个数组,您会看到错误,因此您需要传递数据进行查看并在视图本身中进行迭代,例如:

public function getListTrip(){
    $this->load->model('userModel');
    $data['adventure'] = $this->userModel->getTrip()->result_array();
    //pass data to your view
    $this->load->view('yourviewname', $data);
}

For more information check Codeigniter Views 有关更多信息,请参见Codeigniter视图。

Update 更新资料

Check if your array is not empty before trying to iterate thru it, like in your view:: 尝试遍历数组之前检查数组是否为空,例如在您的视图中:

<?php
if( !empty($adventure) ) {
    foreach($adventure as $b):
?>
        <tr>
            <td><?php echo $b['name']; ?></td>
            <td><?php echo $b['place']; ?></td>
            <td><?php echo $b['category']; ?></td>
        </tr>
<?php
    endforeach;
}
?>

Change your model 改变你的模型

function getTrip(){
    $userId = $this->session->userdata('user_id');    
    $query= $this->db->get_where('adventure',['user_id' => $userId]);
    return $query->result();
}

Also chnage in your controller 同时更改控制器中的内容

$data['adventure'] = $this->userModel->getTrip()->result_array();

To

$data['adventure'] = $this->userModel->getTrip();

You cannot echo the content of an Array. 您不能回显数组的内容。

So whenever you want to view the Content of an array use print_r($arrayName); 因此,每当您要查看数组的内容时,请使用print_r($arrayName);

And When you just want to print any variable just use echo $variableName; 而且,当您只想打印任何变量时,只需使用echo $variableName;

I don't see any issue with your code. 我看不到您的代码有任何问题。 If you're not using autoloader to load the session library that might be your issue: 如果您没有使用自动加载器加载会话库 ,则可能是您的问题:

$this->load->library('session');

You can check the documentation Codeigniter sessions 您可以查看文档Codeigniter会话

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM