从数据库中获取数据并在视图中显示

I have a foreach in my controller - method:

foreach ($unanswered_questions as $un_questions) {
    //get asker_id and asked_id details
    $un_questions->asker = $this->get_user_details($un_questions->asker_id); // where asker_id can be 0 or any user id

}

In my_controller i have this function

public function get_user_details($user_id)
{
    $user = new stdClass();

    switch ($user_id) {
        //if anonimous
        case 0:
            $user->?????? = $this->lang->line('anonymous'); //Result Anonymous text
            $user->profile_picture = 'assets/images/default50.jpg';
            break;

        default:
            $user = $this->users->select_details_by_id($user_id); // would get $user->first_name, $user->last_name, $user->username from database (columns)
            $user->profile_picture = $this->images->get_profile_image($user_id);
            break;
    }

    return $user;

}

So how can I set data to display correctly in view?

I need that anonymous to be shown without link and if is not anonymous to show the username with link.

Also in view I have some elements that would be available only for users and not for anonymous. If my question is not clear please ask me for more details.

I assume in the default: case $user is being set to an object containing all the column names from a database table. So you need to make it look the same as that in your case 0: case.

So you need to do something like this

public function get_user_details($user_id)
{
    $user = new stdClass();

    switch ($user_id) {
        //if anonimous
        case 0:
            $user = new stdClass();

            // make sure ->name matches the actual column name 
            // that would have been generated from the table

            $user->name = $this->lang->line('anonymous');
            // also add any other properties that may be required in your view
            // with some default data.
            $user->profile_picture = 'assets/images/default50.jpg';

            $user->view_type = 1;

            break;

        default:
            $user = $this->users->select_details_by_id($user_id); // would get $user->first_name, $user->last_name, $user->username from database
            $user->profile_picture = $this->images->get_profile_image($user_id);
            $user->view_type = 0;
            break;
    }

    return $user;

}

So showing the right data in the view would be a simple case of testing the new property $user->view_type in the view.

// in controller
$data['title'] = 'A Title`;
$data['user'] = $user;
$this->load->view('myview', $data);


// myview
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>

<?php
   if ( $user->view_type = 0 ) :
      // show with line
   else: 
      // show without line
   emdif; 
?>
</body>
</html>