0

我有这段代码,我只是不知道如何输出视图中的每个项目,例如名字、姓氏和用户名。

controller

{
    $selected_users = array(
          'username' => $username,
          'firstname' => 'peter',
          'lastname' => 'Sample'
          );
    $users = json_decode($this->session->userdata('users'), true);
    $users[] = $selected_users;
    $this->session->set_userdata('users', json_encode($users));
    $this ->load->view('db_control\sess');
   }

view:

$username = $this->session->userdata('users');
echo $username;

but it output all of it 
output of this code is  

[{"username":"newtest1","firstname":"newtest1","lastname":"newtest1"},{"username":"sample_beth","firstname":"sample_beth","lastname":"sample_beth "},{"username":"default","firstname":"default","lastname":"default"}]

这是正确的,但我想输出用户名、姓氏和名字,但仍然是所有记录

4

3 回答 3

0

您将 selected_users 数组设置在一个变量中。尝试这个:

controller
{
$selected_users = array(
'username' => $userID,
'firstname' => 'peter',
'lastname' => 'Sample'
);
$this->session->set_userdata($selected_users);
}

view:
echo $this->session->userdata('username');
echo $this->session->userdata('firstname');
echo $this->session->userdata('lastname');
于 2013-01-22T11:02:50.083 回答
0

在您看来,将您的更改code为:

$username = json_decode($this->session->userdata('users'), TRUE);

      foreach ($username AS $key => $value) {
              echo 'Username: ' . $value['username'];
              echo '<br/>';
              echo 'First Name: ' . $value['firstname'];
              echo '<br/>';
              echo 'Last Name: ' . $value['lastname'];
              echo '<br/><hr><br/>';  
      }

基本上,您正在将您的JSON背部转换为 Array,然后循环遍历它。

于 2013-01-22T12:56:07.963 回答
0

你可以像这个控制器一样简单地传递数据

$data['selected_users'] = array(
                        'username'  =>  'testuser',
                        'firstname' =>  'peter',
                        'lastname'  =>  'Sample'
                    );
$this->load->view('viewname',$data );

看法:

echo $selected_users['username'];
echo '<br>';
echo $selected_users['firstname'];
echo '<br>';
echo $selected_users['lastname'];
于 2013-01-22T10:06:47.193 回答