-1

I am trying to get the values from foreach when i am trying to get first_name i am getting error like `

Illegal string offset 'first_name'

i didn't understand why this error is coming.

Here is my code:

<tbody>
<?php
 $query = $this->db->query("SELECT * FROM ci_sessions WHERE timestamp >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 2 MINUTE))");
 $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] );

$return_data['user_name'] = array_unique($return_data['user_name']);

print_r($return_data['first_name']);    

foreach ($return_data['user_name'] as $onesession) {?>

<tr>

 <td><?php echo $onesession; ?></td>
 <td><?php echo $onesession['first_name']; ?></td>

  </tr>
 <?php } ?>
 </tbody>

can anyone help me why this error is coming.

Thanks in advance.

Pradeep
  • 9,667
  • 13
  • 27
  • 34
user200
  • 291
  • 1
  • 4
  • 21
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Loek Jun 06 '18 at 09:15
  • 1
    That means `$return_data` or `$onesession` is **not** an array thus you can't try to access it like an array `['...']`. In this case its `$onesession`. `var_dump($onesession);` should help to see what is in there. – Xatenev Jun 06 '18 at 09:17
  • getting this string(5) "admin" i am getting this when i use var_dump – user200 Jun 06 '18 at 09:21
  • Possible duplicate of [Illegal string offset codeigniter](https://stackoverflow.com/questions/18999605/illegal-string-offset-codeigniter) – Pradeep Jun 06 '18 at 09:27
  • can you please help me how i can get first_name in my case @pradeep – user200 Jun 06 '18 at 09:32
  • show your $return_data records pls – Pradeep Jun 06 '18 at 09:37
  • Array ( [__ci_last_regenerate] => Array ( [0] => 1528277571 ) [is_logged] => Array ( [0] => 1 ) [user_name] => Array ( [0] => admin ) [first_name] => Array ( [0] => Pramod ) [last_name] => Array ( [0] => Chandran ) [userlevel] => Array ( [0] => 9 ) [organisation_id] => Array ( [0] => 1 ) [user_id] => Array ( [0] => 7 ) [page_name] => Array ( [0] => organisation ) [org_id] => Array ( [0] => 1 ) [temp_user_id] => Array ( [0] => 7 ) ) – user200 Jun 06 '18 at 09:38
  • @pradeep i am getting the above result when i print $result_data – user200 Jun 06 '18 at 09:46
  • @pradeep hi pradeep i have one doubt can i ask? – user200 Jun 19 '18 at 06:48
  • yes pls sure u can – Pradeep Jun 19 '18 at 06:51
  • can you please check this question https://stackoverflow.com/questions/50922014/token-has-to-remove-from-table-once-user-has-been-used-in-php – user200 Jun 19 '18 at 06:52
  • @pradeep did you check my question and please help me out how to solve that – user200 Jun 19 '18 at 07:00
  • If you want to remove the token update the token column also to blank once the password is set – Pradeep Jun 19 '18 at 07:00
  • when i have to update that column when they logged in? – user200 Jun 19 '18 at 07:01
  • there must be some column corresponding to that u update or delete token of that particular user stored in session – Pradeep Jun 19 '18 at 07:07
  • sorry can you please explain it little more i couldn't understand – user200 Jun 19 '18 at 07:08
  • hi pradeep are you free?i have one doubt – user200 Jun 19 '18 at 13:56

1 Answers1

1

Hope this will help you :

Since $result_data is a single array so you can not loop it ,so store $return_$data into another array to get the desired result

$data[] = $return_data;

Your foreach should be like this;

$data[] = $return_data;

/* use below code if [] not supported*/

$data = array();
array_push($data,$return_data);

foreach ($data as $onesession) {?>

 <tr>

 <td><?php echo $onesession['user_name'][0]; ?></td>
 <td><?php echo $onesession['first_name'][0]; ?></td>
  </tr>
 <?php } ?>
 </tbody>
Pradeep
  • 9,667
  • 13
  • 27
  • 34