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 に答える