2012-04-10 5 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'] superglobal을 사용하는 경우, 이스케이프해야합니다. – trickyzter

+0

여기서 temp가 빈 ""과 같지 않은지 확인하고 있습니다. 그런 다음 다음 빈 슬롯 (인덱스)에 배열에 임시 값을 추가하고 itemsList를 세션 –

+0

에 저장합니다.이 작업을 수행하고 정확히 원하는 작업을 수행했습니다 ... 감사 : D –

관련 문제