我有2张桌子,一张用于:
用户信息
秒:
“用户上一次活动”
我有代码可以提取并在页面上显示所有用户,每个用户都应显示其状态-一切都如我的代码所示
但是
我想使代码干净,因此我想移动“如果 状态为空时在我的视图页面到模型页面的“条件” 表格
我想在页面中显示所有具有用户状态的用户,而没有 在视图页面中使用if条件。
我的代码:
数据库:
我的页面:
查看:
<?php foreach($Users as $c){ ?>
<th>Firstname</th>
<th>Lastname</th>
<th>Last Activity Time</th>
<th>State</th>
<th>User ID</th>
</tr>
<tr>
<td><?php echo $c->email ?></td>
<td><?php echo $c->type ?></td>
<td><?php echo $c->last_activity ?></td>
<td>
<!-- this code is run good , but i want to change it to contolloer to make the code clean ,
and if i changed to controller hw i can call the state here -->
<?php $current_timestamp = strtotime(date("Y-m-d H:i:s") . '- 10 second');
$current_timestamp = date('Y-m-d H:i:s', $current_timestamp);
if($c->last_activity > $current_timestamp)
{
echo "online";
}
else
{
echo "offline";
} ?>
型号:
public function getAllUsers() //from 1 table
{
$this->db->select('m.user_id, m.email , m.type, u.last_activity ,u.id ,u.state');
$this->db->from('tbl_user m');
$this->db->join('tbl_user_activity u ', 'u.user_id = m.user_id' ,'LEFT');
$this->db->select_max('u.last_activity');
// $this->db->where($where);
$this->db->group_by('m.user_id');// add group_by
$query = $this->db->get();
foreach ($query as $c)
{
$current_timestamp = strtotime(date("Y-m-d H:i:s") . '- 10 second');
$current_timestamp = date('Y-m-d H:i:s', $current_timestamp);
if('$c->last_activity' > $current_timestamp) //try
{ // here must to be function to checl the last user activity and get it
// here must to set the u.state to online
}
else if($query < $current_timestamp) //try
{
}
}
return $query->result();
答案 0 :(得分:0)
如果您在“模型/控制器”页面上检查并设置“状态”怎么办
MODEL PAGE
<?php
//your code,
$last_activity = $c->last_activity; //make sure it won't return NULL
if($last_activity > $current_timestamp) { //return true if online,
$state = "online";
}else{
$state = "offline";
}
//your code,
?>
只需在VIEW页面中读取状态
VIEW PAGE
<?php
//your code,
$new_state = $c->state; //so $new_state = "online" or $new_state = "offline"
//your code,
?>
答案 1 :(得分:0)
尝试一下。 它将使用称为“状态”的新行创建一个新的数据集合; 返回值是一个数组,因此您可能应该更改视图以循环数组而不是对象。
$query = $this->db->get()->result_result_array();
$new_record = array();
foreach($query as $old_record)
{
$current_timestamp = strtotime(date("Y-m-d H:i:s") . '- 10 second');
$current_timestamp = date('Y-m-d H:i:s', $current_timestamp);
if('$c->last_activity' > $current_timestamp) //try
{
$new_record[]['state'] = 'online';
$new_record[] = $old_record;
}
else if($query < $current_timestamp) //try
{
$new_record[]['state'] = 'offline';
$new_record[] = $old_record;
}
}
return $new_record;