0

当我将explode函数与会话变量结合起来时,我试图弄清楚我做错了什么。我已经对会话变量和explode 函数进行了大量研究,但是在“结合”这两者时我很难找到帮助。

下面是我正在使用的代码。你会注意到它$_SESSION['shirt_type']正在“爆炸”。这个变量收集两条信息,衬衫类型和价格。我想炸成$_SESSION['shirt_type']两块。

但是,当我尝试使用以下内容回显块 [0] 或 [1] 时,

echo "Shirt Type = {$_SESSION['$shirt_type_chunks[0]']}<br>";
echo "Shirt Price = {$_SESSION['$shirt_type_chunks[1]']}<br>";

我的网页回显以下内容

Shirt Type = 
Shirt Price = 

我相信我的语法错误,并且在尝试回显分解的会话变量时,我无法找到任何展示正确语法的地方。

任何可以提供的帮助将不胜感激。另外,我对 StackOverflow 非常陌生,所以如果我发布了错误的内容,我深表歉意。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<?php
session_start();
$_SESSION['shirt_type'] = $_POST['shirt_type'];
$_SESSION['shirt_type_chunks'] = explode("|", $shirt_type);

$_SESSION['shirt_size'] = $_POST['shirt_size'];
$_SESSION['shirt_size_chunks'] = explode("|", $shirt_size);

$_SESSION['shirt_qty'] = $_POST['shirt_qty'];

$_SESSION['price'] = $_SESSION['$shirt_type_chunks[1]'] + $_SESSION['$shirt_size_chunks[1]'];
?>
</head>
<body>
<p>
  <?php
    session_start();

    echo "Shirt Type = {$_SESSION['$shirt_type_chunks[0]']}<br>";
    echo "Shirt Price = {$_SESSION['$shirt_type_chunks[1]']}<br>";
    echo "Shirt Up Charge = {$_SESSION['$shirt_size_chunks[1]']}<br>";
    echo "Shirt Size = {$_SESSION['$shirt_type_chunks[0]']}<br>";
    echo "Shirt Qty = {$_SESSION['shirt_qty']}<br>";

    echo "Price = {$_SESSION['price']}<br>";
  ?>
</p>
</body>
</html>
4

1 回答 1

1

You should start session before any output. Change your code to:

<? session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
...
<title>Untitled Document</title>
<?
$_SESSION['shirt_type'] = $_POST['shirt_type'];
$_SESSION['shirt_type_chunks'] = explode("|", $shirt_type);
$_SESSION['shirt_size'] = $_POST['shirt_size'];
$_SESSION['shirt_size_chunks'] = explode("|", $shirt_size);

$_SESSION['shirt_qty'] = $_POST['shirt_qty'];

$_SESSION['price'] = $_SESSION['$shirt_type_chunks[1]'] + $_SESSION['$shirt_size_chunks[1]'];
?>
</head>

Second thing you have wrong here is that $_SESSION['shirt_type'] and $shirt_type can be the same variable if register_global = on on your system. Try to avoid using same names for variables as for global variables, such as $_POST['some_name'] and $some_name or $_SESSION['var_name'] and $var_name

And the third issue, noticed by @brenjt, you need only one call for session_start() in your script.

Fourth =) $_SESSION['$shirt_type_chunks[1]'] won't work. Using single quotes in PHP means that interpreter will not even try to find any variable in your string. Small example:

<?
$a = "Mike";
echo "Hi, my name is $a"; // will output Hi, my name is Mike
echo 'Hi, my name is $a'; // will output Hi, my name is $a
echo 'Hi, my name is '.$a; // will output Hi, my name is Mike
?>

Third way is a most efficient because in this case PHP sees single quotes and do not spend resources to analyse string. So change it to $_SESSION[$shirt_type_chunks[1]]

Here is what, as I suppose, you was heading to:

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<?php
$shirtDetails = explode("|", $_POST['shirt_type']);
$_SESSION['shirt_type'] = $shirtDetails['0'];
$_SESSION['shirt_price'] = $shirtDetails['1'];

$sizeDetails = explode("|", $_POST['shirt_size']);
$_SESSION['shirt_size'] = $sizeDetails[0];
$_SESSION['shirt_up_charge'] = $sizeDetails[1];
$_SESSION['shirt_qty'] = $_POST['shirt_qty'];

$_SESSION['price'] = $_SESSION['shirt_qty'] * $_SESSION['shirt_price'] + $_SESSION['shirt_up_charge'];
?>
</head>
<body>
<p>
  <?php
    echo 'Shirt Type = '.$_SESSION['shirt_type'].'<br>';
    echo 'Shirt Price = '.$_SESSION['shirt_price'].'<br>';
    echo 'Shirt Up Charge = '.$_SESSION['shirt_up_charge'].'<br>';
    echo 'Shirt Size = '.$_SESSION['shirt_size'].'<br>';
    echo 'Shirt Qty = '.$_SESSION['shirt_qty'].'<br>';
    echo 'Total Price = '.$_SESSION['price'].'<br>';
  ?>
</p>
</body>
</html>

One final question - why you not just create single input for each form property? Why you want this data to be concatenated by |, is it really necessary?

于 2013-03-21T18:13:23.293 回答