0

Here is what I am trying to do :

function increment_users($id){
    $rooms = $_SESSION['rooms_array'];
    //var_dump($rooms);
    foreach ($rooms as $room) {
        if($room['id'] == $id){$room['users']=$room['users']+1;}
    }
    session_write_close();
}

Room's users are not being incremented. I've checked session settings of the server.? Thanks already.

Here is the whole code, please help :

ini_set(' session.save_path','C:/session');
session_start();
$requested_room_id = $_REQUEST['new_room_id'];

//echo $requested_room_id;
function create_new_room($id, $current_users) {
    $room = array();
    $room ['id'] = $id;
    $room ['users'] = $current_users;
    $_SESSION['rooms_array'][]=$room;
}

function increment_users($id){
    $rooms = $_SESSION['rooms_array'];
    //var_dump($rooms);
    foreach ($rooms as $room) {
        if($room['id'] == $id){$room['users']=$room['users']+1;}
    }
    $_SESSION['rooms_array'] = $rooms;
}

$rooms = $_SESSION['rooms_array'];

$room_available = false;
$room_available_ids = array();
var_dump($rooms);
if ($rooms) { //check if there is a room with space
foreach ( $rooms as $room =>$val ) {
    //echo $val['users'];
    if ($val ['users'] < 4) {
        $room_available = true;
        $room_available_ids [] = $val ['id'];
    }
}

}
if ($room_available) { //if there is a room available, add requesting client to the room
    $room_available_id = array_pop ( $room_available_ids );
    increment_users($room_available_id);

    echo $room_available_id;
}
else { //create a new room otherwise
    $rand11=rand(1000000, 99999999);
    if(!$room_available) {$new_room = create_new_room($rand11, 1);}
    echo $rand11;
}

The code is fine acc. to me , I just cannot find what is wrong. Here is the array being dumped by var_dump in the code :

array(1) {
  [0]=>
  array(2) {
    ["id"]=>
    int(63594055)
    ["users"]=>
    int(1)
  }
}

3 Answers 3

1

You should update your session after check:

function increment_users($id){
    $rooms = $_SESSION['rooms_array'];
    foreach ($rooms as $room) {
        if($room['id'] == $id){$room['users']=$room['users']+1;}
    }
    $_SESSION['rooms_array'] = $rooms;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try luke

$rooms = $_SESSION['rooms_array'];
//var_dump($rooms);
foreach ($rooms as $room) {
    if ($room['id'] == $id) {
       $_SESSION['rooms_array'][$room['users']] = $room['users'] + 1;
    }
}

Or directly

foreach ($rooms as $room) {
    if ($room['id'] == $id) {
         $room['users']=$room['users']+1;
    }
}
$_SESSION['rooms_array'] = $rooms;

But I suggest you the first ans.Which only updates the record which satisfies the condition.

1 Comment

No, first one results in a wrong array: array(3) { [0]=> array(2) { ["id"]=> int(98909332) ["users"]=> int(1) } [1]=> int(2) [""]=> int(1) }, second one doesn't increment the value at all!
0

Fixed it myself. I used this code if that helps someone:

function increment_users($id){
    $rooms = $_SESSION['rooms_array'];
    //var_dump($rooms);
    foreach ($rooms as $room) {
        if($room['id'] == $id){$_SESSION['rooms_array'][$id]['users'] = $room['users']+1;}
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.