0
votes

Its been two days I'm working with these guys. Need help here.

here is my view

$username = json_decode($this->session->userdata('users'), TRUE);

    foreach ($username AS $key => $value) {

    <?php echo $value['username']; ?>
    <?php echo $value['firstname']; ?>
    <?php echo $value['lastname']; ?>
    <?php $u = $value['username']; ?>
    <?php echo "<a href=".base_url()."index.php/bets/un_sess/$u /> X </a> " ?>

    <?php } ?>

here is my controller

$user1 = json_decode($this->session->userdata('users'), TRUE);
 foreach ($user AS $key => $value) {
       $index = array_search($username, $user);
       unset($user[$index]);          
      $this->session->set_userdata('users', $user);

array from user_data

{"username":"user1","firstname":"name1","lastname":"name2"},
{"username":"user2","firstname":"test","lastname":"test"}

the output of this is when the first time i click the
username its working but when i click another username it gives error like this

A PHP Error was encountered

Severity: Warning

Message: Illegal offset type in isset or empty

Filename: libraries/Session.php

Line Number: 433 A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: controllers/bets.php

Line Number: 290

1
look at your controller, you are storing json_decoded data in $user1 and then using $user in foreach - Uttara
when i change that $user1 to $user it gives error like this A PHP Error was encountered Severity: Warning Message: json_decode() expects parameter 1 to be string, array given Filename: controllers/bets.php Line Number: 288 - webstudent
Here is my answer $user = json_decode($this->session->userdata('users'), TRUE); foreach ($user AS $key => $value) { if ($username == $value['username']){ // $array_items = array('username' => $value['username'], 'firstname' => $value['firstname'],'lastname' => $value['lastname'] ); unset($user[$key]); } } $this->session->set_userdata('users', json_encode($user)); - webstudent

1 Answers

0
votes

ok since there is no answering in my questions. Here is my solutions

 $user = json_decode($this->session->userdata('users'), TRUE);
        foreach ($user AS $key => $value) {
          if ($username == $value['username']){

          unset($user[$key]);
         }
        }

       $this->session->set_userdata('users', json_encode($user));