以下是我要做的事情:
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)
}
}
答案 0 :(得分: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;
}
答案 1 :(得分:0)
试试luke
$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;
但我建议你第一个ans.Which只更新满足条件的记录。
答案 2 :(得分: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;}
}