我知道这是简单的 php,但是我很难用 codeigniter 写出我的逻辑。
我正在尝试提取 12 个最近的用户并在视图中显示他们的信息。我在我的数据库表中创建了一个“created_on”字段来跟踪注册活动。
当我在控制器中指定一个 id 时,我可以提取从模型传递到控制器然后是视图的结果,但是它只会像这样拉一个用户。
模型:
public function newest_members()
{
$session = $this->session->userdata('is_logged_in');
$user_id = $this->session->userdata('id');
$query = $this->db->query("SELECT * FROM users WHERE status = 'active' && id = $user_id ORDER BY created_on DESC LIMIT 12");
if($query->num_rows()==1)
{
$data = $query->result_array();
return $data;
}else{
return array();
}
}
控制器:
public function dashboard()
{
$this->load->library('session');
$this->load->helper('date');
$session_id = $this->session->userdata('id');
$this->load->model('account_model');
$this->load->model('community_model');
$user = $this->account_model->user();
$data['user'] = $user;
$newest_members = $this->community_model->newest_members();
$data['newest_members'] = $newest_members;
$data['main_content'] = 'account/dashboard';
$this->load->view('includes/templates/main_page_template', $data);
}
看法:
<?php
foreach($newest_members as $nm)
{
echo $nm['first_name']; echo "<br />"; echo $nm['last_name'];
}
?>
这是提取最新成员和已登录成员的结果。但是我想拉所有已登录或不登录,所以我在我的模型上尝试这样的事情:
public function newest_members()
{
$query = $this->db->query("SELECT * FROM users WHERE status = 'active' ORDER BY created_on DESC LIMIT 12");
// OR YOU CAN USE THE BELOW
// $query = $this->db->query("SELECT * FROM users ORDER BY `created_on` DESC LIMIT 12");
// YOU ALSO MIGHT WHAT TO ADD WHERE `status` = 'active' (or whatever applies for your db table)
// EITHER SPIT OUT RESULTS OR EMPTY ARRAY
if($query->num_rows()==1)
{
$data = $query->result_array();
return $data;
}else{
return array();
}
提前致谢。