duanniu3385 2018-06-05 11:50
浏览 120
已采纳

foreach循环只显示codeigniter中的最后一条记录,并获取5分钟后记录的活动记录

Hi all i am trying to get active session users in codeigniter.So i am storing all my sessions in database and table name called ci_sessions and i got user data from ci_sessions table and i am trying to get user_name from data through foreach but i am getting only last active user.

Here is my code:

<?php
$query = $this->db->query('SELECT * FROM ci_sessions');

foreach ($query->result() as $row) {
    $session_data = $row->data;

    $return_data = array();  // array where you put your "BLOB" resolved data

    $offset = 0;
    while ($offset < strlen($session_data)) 
    {
        if (!strstr(substr($session_data, $offset), "|")) 
        {
            throw new Exception("invalid data, remaining: " . substr($session_data, $offset));
        }
        $pos = strpos($session_data, "|", $offset);
        $num = $pos - $offset;
        $varname = substr($session_data, $offset, $num);
        $offset += $num + 1;
        $data = unserialize(substr($session_data, $offset));
        $return_data[$varname] = $data;  
        $offset += strlen(serialize($data));
    }    
}
echo $return_data['user_name'];

?>

Can anyone help me where i did mistake.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongxiaoyan4388 2018-06-05 11:55
    关注

    A small mistake you have done

    <?php
    $query = $this->db->query('SELECT * FROM ci_sessions');
    $return_data = array();  // array where you put your "BLOB" resolved data
    foreach ($query->result() as $row) {
       $session_data = $row->data;
    
       $offset = 0;
       while ($offset < strlen($session_data)) 
        {
           if (!strstr(substr($session_data, $offset), "|")) 
            {
              throw new Exception("invalid data, remaining: " . substr($session_data, $offset));
            }
              $pos = strpos($session_data, "|", $offset);
              $num = $pos - $offset;
              $varname = substr($session_data, $offset, $num);
              $offset += $num + 1;
              $data = unserialize(substr($session_data, $offset));
              $return_data[$varname][] = $data;  
              $offset += strlen(serialize($data));
          }
    
          }
         var_dump( $return_data['user_name'][0] );
    
         foreach($return_data['user_name'] as $onesession) {
              echo $onesession;
              echo '<br>';
         }
    
    ?>
    

    $return_data = array(); I moved it out of foreach loop

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 maccms影视模板 制作影视网站失败 求
  • ¥15 stm32按键设置闹钟数进退位不正常
  • ¥15 三电平逆变器中点电位平衡问题
  • ¥20 这怎么写啊 java课设
  • ¥15 用C语言完成一个复杂的游戏
  • ¥15 如何批量更改很多个文件夹里的文件名中包含文件夹名?
  • ¥50 MTK手机模拟HID鼠标出现卡顿
  • ¥20 求下下面这个数据结构代码
  • ¥20 前端 二进制文件流图片转化异常