0

工作一个项目并在将多维数组分配给会话时遇到挑战。

项目信息:

公司应在网站上注册/预订课程,并且公司可以注册多个课程,并且对于每门课程,公司必须指定候选人的数量及其生物数据。

例子:

如果 XYZ 公司注册了课程 A、课程 B 和课程 C。并且为每门课程提供候选人信息如下:

<?php

 $all_reg_courses = array
  (
  $course_id => array(
    "candidate_no" => $cand_no,
  "all_candidates" => array(
 array
  (
  'cand_name' => 'Alex Holmes',
  'cand_email' => 'al@yah.com',
  'cand_sex' => 'Male',
  'cand_phone' => '070043635690',
  'cand_job' => 'IT'
  ),
 array
  (
  'cand_name' => 'Tilla Reel',
  'cand_email' => 'till@yah.com',
  'cand_sex' => 'Female',
  'cand_phone' => '0700833435690',
  'cand_job' => 'HR'
  ),
 array
  (
  'cand_name' => 'Bill Enya',
  'cand_email' => 'bill@yah.com',
  'cand_sex' => 'Male',
  'cand_phone' => '07008055435690',
  'cand_job' => 'Admin'
  )
  )
  ),
  $course_id => array(
    "candidate_no" => $cand_no,
  "all_candidates" => array(
 array
  (
  'cand_name' => 'Alan Pole',
  'cand_email' => 'al@yah.com',
  'cand_sex' => 'Male',
  'cand_phone' => '07009973635690',
  'cand_job' => 'IT'
  ),
 array
  (
  'cand_name' => 'Coleen Curtis',
  'cand_email' => 'cole@yah.com',
  'cand_sex' => 'Female',
  'cand_phone' => '5553435690',
  'cand_job' => 'HR'
  )
  )
  ),
    $course_id => array(
    "candidate_no" => $cand_no,
  "all_candidates" => array(
 array
  (
  'cand_name' => 'Tamia James',
  'cand_email' => 'tam@yah.com',
  'cand_sex' => 'Female',
  'cand_phone' => '7673435690',
  'cand_job' => 'HR'
  )
  )
  )
  );

?>

为各种课程提供的这些数据和为每门课程提名的候选人将临时存储在一个会话中,如果“XYZ 公司”最终单击“最终提交按钮”,最终将发送到数据库中,否则会话中的所有数据将在特定时间后销毁(即课程预订车将被清空)

我一直在尝试将上面的示例多维数组分配到一个会话中,但这不仅仅是工作。

收到错误消息:无法使用字符串偏移量;

这是将数组附加到会话的代码:

<?php

session_name("cand_cart");
session_start();

$c_id = $_POST['c_id'];
//$cand_no = $_POST['cand_no'];

$_SESSION['cand_cart']['c_id']['cands'] = array();
$_SESSION['cand_cart']['c_id'] = array();
$_SESSION['cand_cart']['c_id']['cand_no'] = array();

$_SESSION['cand_cart']['c_id']['cand_no'] = $_POST['cand_no'];
$_SESSION['cand_cart']['c_id'] = $_POST['c_id'];

//$candidates = array();

foreach ($_POST['candidate'] as $i => $can)
{
    $candidates[$i] = array();

    foreach ($can as $key => $value)
    {
        //$candidates[$i][$key] = $value;
        $_SESSION['cand_cart']['c_id']['cands'][$i][$key] = $value;
    }
}


echo '<pre>';
//var_dump($candidates);
print_r($_SESSION['cand_cart']);
echo '</pre>';

echo "<br><br>";

?>

<table border="1" cellspacing="0" cellpadding="6">
    <thead>
        <th>#</th>
        <th>Name</th>
        <th>Phone</th>
        <th>Email</th>
        <th>Sex</th>
        <th>Position</th>
    </thead>
    <tbody>
<?php

foreach ($_SESSION['cand_cart'] as $i => $can)
{
    $c = (object) $can;
    $sn = $i + 1;
    echo '<tr><td>' . $sn . '</td><td>' . $c->name . '</td><td>' . $c->phone . '</td><td>' . $c->email . '</td><td>' . $c->sex . '</td><td>' . $c->pos . '</td></tr>';
}

?>
    </tbody>
</table>

将不胜感激得到这方面的帮助.. 谢谢!

4

3 回答 3

1

尝试更改这些行:

$_SESSION['cand_cart']['c_id']['cands'] = array();
$_SESSION['cand_cart']['c_id'] = array();

$_SESSION['cand_cart']['c_id'] = array();
$_SESSION['cand_cart']['c_id']['cands'] = array();
于 2012-12-19T11:31:56.190 回答
0

您正在覆盖整个数组,丢失了以前存储的信息

$_SESSION['cand_cart']['c_id']['cands'] = array();
$_SESSION['cand_cart']['c_id'] = array(); // here you overwrite the whole 'c_id' array and loose cands

$_SESSION['cand_cart']['c_id']['cand_no'] = $_POST['cand_no'];
$_SESSION['cand_cart']['c_id'] = $_POST['c_id']; // another overwite
于 2012-12-19T11:32:17.960 回答
-1

在将数据存储到会话之前序列化和反序列化数据会容易得多。这似乎更轻量级,您不必专门设置每个值:

// Build up your data-Array from the submitted values stored in $_POST
$data = array();
foreach($_POST['candidate'] AS $i => $can)
{
     // Do your logic here, like you did before with the session
}

// Now serialize and store into session at once (= more performance)
$_SESSION['data'] = serialize($data);

如果您想在下一个请求中使用数据,则必须对其进行反序列化:

$data = unserialize($_SESSION['data']);

希望这可以帮助。

于 2012-12-19T11:32:54.143 回答