2013-01-04 2 views
1

저는 PHP에 익숙하지 않으므로 전에 기본 질문을하고 있다면 묻습니다. 나는 며칠 동안 이걸 봤는데 이제는 봤어. 이 포럼에서는 대부분의 이전 답변을 찾았지만이 문제에 대해서는 아무 것도 찾을 수 없으므로 물어봐야합니다. 이것이 나의 첫 번째 질문이다.하위 배열을 확인란으로 배열로 다른 페이지로 전달할 수 있습니까?

// this array is coming from MySQL db as a result. It's a list of user's friends and it could contain dozens or hundreds of friends. Now he wants to put them in different groups. 

$array = array(
     array("John", "Doe", "1"), 
     array("Peter", "Citizen", "2") 
     ... 
    ); 


// a page is created with the result. Each record has a checkbox that the user can select. 

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 

<?php 

foreach($array as $item){ 
$nItem = $item; 

?> 

<input type="text" name="fname" value="<?php if(isset($nItem)){echo $nItem[0];} ?>" readonly="readonly" /> 
<input type="text" name="lname" value="<?php if(isset($nItem)){echo $nItem[1];} ?>" readonly="readonly" /> 
<input type="text" name="uid" value="<?php if(isset($nItem)){echo $nItem[2];} ?>" readonly="readonly" /> 
<input type="checkbox" name="val[]" value="<?php if(isset($nItem)){echo $nItem;} ?>" /> // I want to send this $nItem array as it is to the action page and read its keys and values there. 
<br> 

<?php 
} 
?> 

<input type="submit" name="submit" value="Submit"> 
</form> 

<br /> 

<?php 

// if I tick both checkboxes and submit I get the following: 

if(isset($_POST['val'])){ 

$val = $_POST['val']; 

echo var_dump($val), '<br />'; // array(2) { [0]=> string(5) "Array" [1]=> string(5) "Array" } 

echo count($val), '<br />'; // 2 

print_r($val);     // Array ([0] => Array [1] => Array) 

echo("val is {$val[0]}");  // val is Array 

foreach($val as $key => $value){ 
    echo "Key and Value are: ".$key." ".$value, '<br />'; // Key and Value are: 0 Array 
}               // Key and Value are: 1 Array 

} 

?> 

나는 velues를 읽을 수있는 실제 배열 대신 "Array"라는 문자열을 반환합니다.

if(isset($nItem)){echo "".$nItem[0]." ".$nItem[1]." ".$nItem[2]."";} 

- - 나는 다음과 같은 체크 박스 값의 개별 값을 넣으면

0 John Doe 1 

을 - - 그때 얻을 수 있지만 내가 원하는 게 아니에요. 내가 반복 할 수있는 액션 페이지의 실제 배열을 원한다. 나는 그것이 이미 배열 이었기 때문에 어렵지 않을 것이라고 생각했지만 틀렸어.

아무에게도 어떻게 할 수 있습니까?

미리 감사드립니다.

답변

0
<?php foreach($array as $item): ?> 
    <input type="text" name="fname" value="<?php echo $item[0]; ?>" readonly="readonly" /> 
    <input type="text" name="lname" value="<?php echo $item[1]; ?>" readonly="readonly" /> 
    <input type="text" name="uid" value="<?php echo $item[2]; ?>" readonly="readonly" /> 
    <input type="checkbox" name="val" value="<?php echo htmlentities(serialize($item)); ?>" /> 
    <br> 
<?php endforeach; ?> 

제출 한 후 배열 $ _POST [ 'val']을 (를) 직렬화 해제합니다. 이 배열로 무엇을하는지에 따라이 방법은 주사를 맞출 때는 사용하지 않는 것이 좋습니다.

모든 정보가 포함 된 배열로 작업하는 대신 각 레코드에 id를 사용하는 것이 좋습니다. "val"이라는 확인란에는 해당 ID의 값이 있습니다.

<input type="checkbox" name="val" value="<?php echo $item['id']; ?>" /> 

제출 한 후 ID에 속한 레코드를 쿼리하고 반환 된 배열을 사용하여 작업하십시오.

관련 문제