php数组没有项目?

时间:2012-04-10 13:05:31

标签: 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 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>

知道为什么这不显示任何东西? 谢谢,

1 个答案:

答案 0 :(得分: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;
}