簡體   English   中英

在$ _POST []中使用var使用php對$ _POST數組進行處理

[英]$_POST array processing with php using a var in the $_POST[]

我正在制作一個顯示客戶和該客戶相關帳戶的表格。 例如客戶John有3個帳戶

account_Id 1 account_type hardware
account_Id 4 account_type service
account_Id 6 account_type software

通過從數據庫中選擇所有帳戶信息,然后創建表格,然后

$acc_i=0;
foreach($account as $acc){
?>
    <tr>
    <input type="hidden" name="account_count" value="<?php echo $acc_i; ?>">
    <input type="hidden" name="<?php echo $acc_i; ?>_cust_account_id" value="<?php echo $acc['Id'];?>" />
    <td>
    </td>
    </tr>
<?php
$acc_i++;
}

這給了我一個$ _POST數組

account_count => 1
0_cust_account_id => 1
1_cust_account_id => 4
1_cust_account_id => 6

是否可以使用類似的方法訪問值

for($i=0;$i<=$_POST['account_count'];$i++){ echo $_POST[$i _cust_account_id];}

如果您要處理要發送的數據列表,請考慮使用數組,例如,如果要發送ID列表,可以執行以下形式:

<input type="hidden" name="CustomerId[]" value="1" />
<input type="hidden" name="CustomerId[]" value="2" />
<input type="hidden" name="CustomerId[]" value="3" />

然后,當提交了這些值時,

$_POST['CustomerId'] == array(1, 2, 3)

count($_POST['CustomerId']) == Number of accounts?

在問這個問題時,我找到了一個可行的解決方案,而不是嘗試

$_POST['.$i.'_cust_account_id];

我用這個

$string=$i.'_cust_account_id';
$_POST[$string]

如果您知道如何避免使用額外的變量,請發布該變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM