0

这是我正在尝试做的事情:

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();
}

房间的用户没有增加。我检查了服务器的会话设置。?已经谢谢了。

这是整个代码,请帮助:

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;
}

代码很好。对我来说,我只是找不到问题所在。这是代码中 var_dump 转储的数组:

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

3 回答 3

1

检查后您应该更新会话:

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;
}
于 2013-08-23T12:13:45.520 回答
0

自己修好了。如果这对某人有帮助,我会使用此代码:

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;}
    }
于 2013-08-26T11:44:26.107 回答
0

试试卢克

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

或者直接

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

但我建议你第一个答案。它只更新满足条件的记录。

于 2013-08-23T12:11:34.790 回答