简体   繁体   English

会话数组仅显示一个结果

[英]Session array display only one result

Hello i receive by $_POST a user_name and a user_id. 您好,我通过$ _POST收到一个user_name和一个user_id。

I want to get something like this in $_SESSION : 我想在$ _SESSION中得到这样的东西:

(id) - (user_name)
5 - John
6- Paul 


<?php
    $user_id = $_POST['user_id'];
    $user_name = $_POST['user_name'];

       $_SESSION['test'] = array('user_id' => $user_id, 'user_name' => $user_name);

    foreach( $_SESSION['test'] as $key => $value )
    {
          echo $key;
          echo $value;
    }
?>

Only John and his id is displayed. 仅显示John及其ID。 I mean Paul and his id is not showing. 我的意思是保罗和他的身份证没有显示。 Why ? 为什么呢


i have a js script , 我有一个js脚本,

$.ajax({
                    url : "ajax_chat.php",
                    type: "POST",
                    data: 'action='+action+'&user_id='+user_id,
                    success: function(result)
                    {
                    }
                });

When someone click on a user i want a session create for each id which will be send to my php file. 当有人单击用户时,我要为每个ID创建会话,该会话将发送到我的php文件。

ajax_chat.php ajax_chat.php

if ($action == "ChangeSessionValueUser")
{ 
   $_SESSION['test'] = array('user_id' => $user_id, 'user_name' => $user_name);
}

in index.php 在index.php中

<?php
foreach( $_SESSION['test'] as $key => $value )
{
      echo $key;
      echo $value;
}
?>

You did not understand arrays correctly. 您没有正确理解数组。 Take simpler code without sessions: 采取没有会话的简单代码:

$test = array('user_id' => 5, 'user_name' => 'john');
$test = array('user_id' => 6, 'user_name' => 'paul');

print_r($test);
// this only prints paul, since you've overwritten the variable.

// You want an array of arrays instead, so
$test = array();
$test[] = array('user_id' => 5, 'user_name' => 'john');
$test[] = array('user_id' => 6, 'user_name' => 'paul');
print_r($test);
// this prints out both

// If you want to loop, you must change it a little bit
foreach ($test as $value) {
   echo 'User ' . $value['user_name'] . ' with user id ' . $value['id'];
}
// if you do echo($value); this yields "Array"
// since you have an array in an array.

To init the array in the session only once, you can use something like this 要仅在会话中初始化数组一次,可以使用如下所示的内容

if (!isset($_SESSION['test'])) {
  $_SESSION['test'] = array();
}
// then you can push in additional users on all pages like
$_SESSION['test'][] = array('id' => 7, 'user_name' => 'test');

// and output all with
foreach ($_SESSION['test'] as $value) {
  echo 'User ' . $value['user_name'] . ' with user id ' . $value['id'];
}

I may be misunderstanding what you are doing. 我可能会误会你在做什么。 But if you are getting only one user_id and one user_name per POST request, then there are a few things that you'll need to do. 但是,如果每个POST请求仅获得一个 user_id一个 user_name ,那么您将需要做一些事情。

  1. You are not actually creating a session. 您实际上不是在创建会话。

You'll need to add session_start() to either activate, or reactivate a session here's an simple resource: http://www.w3schools.com/php/php_sessions.asp 您需要添加session_start()来激活或重新激活会话,这是一个简单的资源: http : //www.w3schools.com/php/php_sessions.asp

  1. with the line $_SESSION['test'] = array('user_id' => $user_id, 'user_name' => $user_name); $_SESSION['test'] = array('user_id' => $user_id, 'user_name' => $user_name); You will overwrite the $_SESSION['test'] variable every time. 您每次都会覆盖$_SESSION['test']变量。 So you'll need to append that new array to the end of 'test' like this $_SESSION['test'][] = array(...) (if I'm understanding what you want to do) 因此,您需要像$_SESSION['test'][] = array(...)这样,将新数组追加到'test'的末尾(如果我了解您要做什么)

So your code should look something like this: 因此,您的代码应如下所示:

<?php
    session_start();
    $user_id = $_POST['user_id'];
    $user_name = $_POST['user_name'];
    $_SESSION['test'][] = array('user_id' => $user_id, 'user_name' => $user_name);

    foreach( $_SESSION['test'] as $key => $value )
    {
          echo $key;
          echo $value;
    }
?>

This should add a user_id and user_name pair each time it is given one to the $_SESSION global under the "test" key. 每次在“ test”键下为$ _SESSION全局变量分配一个user_id和user_name对时,都应添加该对。

You are receiving a single value from POST. 您正在从POST接收单个值。 You need to have array names for each input field like: 您需要为每个输入字段输入数组名称,例如:

<input type="text" name="user_id[]" />
<input type="text" name="user_name[]" />

Then when you receive these values, put them in session one by one: 然后,当您收到这些值时,将它们一一置于会话中:

$_SESSION['test'] = array();
foreach ($_POST['user_id'] as $i => $id) {
    $_SESSION['test'][] = array($id => $_POST['user_name'][$i]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM