如何在我的视图文件中显示每个用户id的记录数:Codeigniter


How to show the number of record against each user id into my view file: Codeigniter

我已经从数据库中针对每个用户提取了模型中的记录,如下所示:

public function counter_records() {
$table = 'usersearchs';
$this->db->select("domain, time");
$this->db->from($table);
$this->db->where("user_id", $this->session->userdata("user_id"));
$res = $this->db->get();
$num_of_records = $res->num_rows();
return $num_of_records;
}

现在,我想将这些数量的记录显示到每个用户帐户的视图文件中,他们必须知道自己有多少记录。

使用此函数显示模型:中针对user_id的所有记录

public function userData()
{
    $results = array();
    $table = 'usersearchs';
    $this->db->select("domain, time");
    $this->db->from($table);
    $this->db->where("user_id",$this->session->userdata("user_id"));
    $query = $this->db->get();
    $num_of_records = $query->num_rows();
    if($num_of_records > 0){
        $results = $query->result_array();
    }   
    return $results;    
}

在控制器中:在控制器文件中调用此函数。

$this->load->model('model_yourmodel');
$data['records'] = $this->model_yourmodel->userData();

并将这些数据从控制器传递到您的视图中,如下所示:

$this->load_view("yourviewpath" , $data);

在HTML视图文件中:使用循环显示这样的数据:

<?
$total_records = count($records);
if($total_records <= 3){
echo (4-$total_records). " records remaining in your account";
}
else{
echo "You have reached the maximum record";
}
if($total_records > 0){  // $records that you have pass from your controller.
   foreach($records as $value){
   ?>
   // Your HTML Code 
   Domain <?=$value['domain']?> - Time <?=$value['time']?> <br/>
   // Your HTML Code
   <?
   }
}
else{
   // your HTML
   echo "No record found";
   // your HTML
}
?>

结果应该与HTML:中的结果类似

domain1 - time1 
domain2 - time2 
domain3 - time3 
domain4 - time4 
domain5 - time5