php数组没有项目?

php数组没有项目?,php,arrays,Php,Arrays,我有以下代码。我试图跟踪所选项目,并从阵列中向下显示它们 <?php session_start(); //temp stores the submitted value $temp = $_POST['field']; if (!isset($_SESSION['itemsList'])) { $itemsList = array(); } else { $itemsList = $_SESSION['itemsList']; } //check how many

我有以下代码。我试图跟踪所选项目,并从阵列中向下显示它们

<?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>

测试页Khalid
知道这为什么没有显示什么吗?
谢谢,

这就是您想要实现的目标:

<?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>

“什么都不展示”是什么意思?什么都没有出现吗?打开错误报告将允许人们提供更多帮助。尝试日常调试内容,然后提供信息:它是否进入设置变量的IF块?如果是这样,设置后的值是多少?在IF块之后如何…等等。html只是数组中的项的一部分…我在脚本中做了什么错误吗?在第二个IF语句中,您使用的是
$\u SESSION['itemList']
$itemList
,而在其他地方它是
itemsList
(看起来您忘记了第二个IF语句中的s)。另外,在将表单的操作设置为
$\u SERVER['PHP\u SELF']
时要小心。这很容易受到攻击。我也同意Travesty3,如果使用$_服务器['PHP_SELF']超全局,则需要对其进行转义。我在这里检查temp是否不等于空“”。如果不是,则将临时值添加到下一个空闲插槽(索引)中的数组中,并将itemsList存储到Session。您使用的函数用于比较字符串值,这对于仅检查值状态来说太过分了。更好的方法是使用。我对相同的代码有一个问题。现在,当我选择一个项目时,它会重新加载页面(因为self_submit)。但该项目不显示在列表中。然后我再次选择相同的项目,页面将加载列表中的项目。然后我去选择另一个项目,但这次列表中出现的是第一个项目的丢失条目(好像它被缓存和延迟了)。你知道会发生什么事吗?我不能说我以前见过这种情况。我已经更新了代码以使用$items变量集。此外,我忘了响应表单操作,但是这些更改不应该反映功能更改。你肯定没有改变什么?您尝试过其他浏览器吗?
$emptyval = "";

if (strcmp($temp,$emptyval)!=TRUE) {
    array_splice($itemsList, $arraySize+1, 0, $temp);
    unset($temp);
    unset($_SESSION['field']);
    $_SESSION['itemList'] = $itemList;
}