0

我有以下代码。我正在尝试跟踪选定的项目并将它们从数组中显示出来。

<?php
session_start();

//temp stores the submitted value
$temp = $_POST['field'];

if (!isset($_SESSION['itemsList'])) {
    $itemsList = array();
} else {
    $itemsList = $_SESSION['itemsList'];
}

//check how many elements in array
$arraySize = count($itemsList);

//set that as i and then add after that
$emptyval = "";

if (strcmp($temp,$emptyval)!=TRUE) {
    array_splice($itemsList, $arraySize+1, 0, $temp);
    unset($temp);
    unset($_SESSION['field']);
    $_SESSION['itemList'] = $itemList;
}
?>
<html>
<head>
    <title>Test Page Khalid</title>
</head>
<body>
<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">

    <table height="100%" width="100%" cellspacing="10" cellpadding="10" border="1">
        <tr>
            <td align="center"><input name="field" value="shirt" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="pants" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="socks" style="width:200px; height:100px;" type="submit"/></td>
        <tr>
        <tr>
            <td align="center"><input name="field" value="dress" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="skirt" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="topbody" style="width:200px; height:100px;" type="submit"/></td>
        <tr>
        <tr>
            <td align="center"><input name="field" value="sheets" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="pillowcover" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="blanket" style="width:200px; height:100px;" type="submit"/></td>
        <tr>        
    </table>
</form>
<br><br><br>
<?php
$itemsReturned = $_SESSION['itemList'];
echo "The items stored are: <br>";
print_r($itemsReturned);
?>

</body>
</html>

知道为什么这没有显示任何内容吗?谢谢,

4

1 回答 1

2

这是您想要实现的目标:

<?php
session_start();
//store submitted value
$val = $_POST['field'];
//create a reference to the session
$items = (!isset($_SESSION['items']) ? array() : $_SESSION['items']);

//did the user submit a value?
if($val){
//append the value to the items array, contained within the session
    $_SESSION['items'][] = $val;
//update the reference
    $items =& $_SESSION['items'];
}

?>
<html>
<head>
    <title>Test Page Khalid</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

    <table height="100%" width="100%" cellspacing="10" cellpadding="10" border="1">
        <tr>
            <td align="center"><input name="field" value="shirt" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="pants" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="socks" style="width:200px; height:100px;" type="submit"/></td>
        <tr>
        <tr>
            <td align="center"><input name="field" value="dress" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="skirt" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="topbody" style="width:200px; height:100px;" type="submit"/></td>
        <tr>
        <tr>
            <td align="center"><input name="field" value="sheets" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="pillowcover" style="width:200px; height:100px;" type="submit"/></td>
            <td align="center"><input name="field" value="blanket" style="width:200px; height:100px;" type="submit"/></td>
        <tr>        
    </table>
</form>
<br><br><br>
<?php
echo "The items stored are: <br>";
print_r($items);
?>

</body>
</html>

您能否提供有关以下目的的详细信息:

$emptyval = "";

if (strcmp($temp,$emptyval)!=TRUE) {
    array_splice($itemsList, $arraySize+1, 0, $temp);
    unset($temp);
    unset($_SESSION['field']);
    $_SESSION['itemList'] = $itemList;
}
于 2012-04-10T13:20:23.493 回答