如何在控制器中将多个变量从控制器传递到视图?
这是我的控制者:
class EchoActionTest extends \PHPUnit_Framework_TestCase
{
public function testGetRequestReturnsEcho()
{
// instantiate action
$action = new \App\Action\EchoAction();
// We need a request and response object to invoke the action
$environment = \Slim\Http\Environment::mock([
'REQUEST_METHOD' => 'GET',
'REQUEST_URI' => '/echo',
'QUERY_STRING'=>'foo=bar']
);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = new \Slim\Http\Response();
// run the controller action and test it
$response = $action($request, $response, []);
$this->assertSame((string)$response->getBody(), '{"foo":"bar"}');
}
}
以下是我的观点:
function viewmembers(){
$this->load->model('UsersModel');
$data['students'] = $this->UsersModel->studentlist_all();
$data['user'] = $this->UsersModel->select_admin($id);
$this->load->view('portal/viewstudents', $data);
}
它给了我"未定义的变量:nick"。
我也试过了 print_r($ user),但它给了我一个值" Array()"
虽然print_r($学生)工作
答案 0 :(得分:0)
result_array()
不会返回单行。如果您想要单行,那么您应该使用row_array()
。此外,在where condition
您尝试将username
字段与id
匹配时,这是错误的。因此,您的方法必须如下:
public function select_admin($id){
$this->db->select('*');
$this->db->from('admins');
$this->db->where('id',$id);
$query = $this->db->get();
return $query->row_array();
}
并在视图中回显字段值:
echo $user['your_field_value_name'];
如果您想了解更多内容,请参阅此doc