2012-04-10 7 views
0

私は次のコードを持っています。私は選択された項目を追跡し、配列からそれらを表示しようとしています。PHP配列にアイテムがありませんか?

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

これは何も表示されない理由は何ですか? おかげで、

+0

あなたは「何も表示されない」とは何を意味するのですか?何も起きていないのですか? – Gricey

+0

エラー報告をオンにすると、より多くのヘルプを提供できるようになります。 – Jim

+0

毎日のデバッギングを試してから、その情報を入力してください。変数を設定するIFブロックに入っていますか?もしそうなら、それが設定された直後の値は何ですか? IFブロックの直後はいかがですか? – Dave

答えて

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; 
} 
+0

Travesty3にも同意します。もし$ _SERVER ['PHP_SELF']のスーパーグローバルを使用しているなら、それをエスケープする必要があります。 – trickyzter

+0

ここでは、tempが空 ""と等しくないかどうかを確認しています。そうでない場合は、次の空きスロット(インデックス)に配列にtemp値を追加し、itemsListをセッション –

+0

に保存してください。これはうまくいきました...ありがとう:D –

関連する問題