$ _POST Döngü

2 Cevap php

I have a for loop which actually displays a product name and several buttons like: Edit, Update , Cancel For each product i am displaying , it will have its own set of Edir, Update, and Cancel button as below.

Düzenleme Güncelleme İptal Boya

I want to loop through the buttons so that for each category, I can perform a different action. I was thinking about using something like btn_edit1, btn_edit2 for the name of the button and use a for loop. 1, 2 are the category ids. Maybe Im not clear enough. Sorry for that. Can anyone give me some suggestions?

for($i = 0; $i<count($obj_categories_admin->categories);$i++)
{   

            echo "<tr>";

            echo "<td width='1500'>";
            echo "<input type='text' name='name'  size = '30' value='" . $obj_categories_admin->categories[$i]['name'] . "'/>";

            echo "</td>";

            echo "<td width='500'>";

            echo "<input type='submit' value = 'Update details' name='submit_update_category_" . 
            $obj_categories_admin->categories[$i]['category_id'] . "'/>";

            echo "</td>";



            echo "<td width='500'>";

            echo "<input type='submit' value = 'Edit Sub Categories' name='submit_edit_sub_" . 
            $obj_categories_admin->categories[$i]['category_id'] . "'/>";

            echo "</td>";

            echo "<td width='500'>";
            echo "<input type='submit' value = 'Delete' name='submit_delete_category_" . 
            $obj_categories_admin->categories[$i]['category_id'] . "'/>";
            echo "</td>";

            echo "<td width='500'>";

            echo "<input type='submit' value = 'Cancel' name='cancel'" . "'/>" ;

            echo "</td>";

            echo "</tr>";   
    }

Ben böyle bir şey yapmak istiyorum

foreach($_POST as $key => $value)
{

}

i bir düğmeye tıkladığınızda category_id bağlı bir eylem gerçekleştirir böylece.

Önerdiği gibi ben denedim:

echo "<input type='submit' name='submit[add_category]'" . 
"[" . $obj_categories_admin->categories[$i]['category_id'] . "]". " value='Add' />";

Şimdi benim sınıf, i var:

$a1 = $_POST['submit']; 
    $which_action = reset(array_keys($a1)); 
    $which_category = reset(array_keys($a1[$which_action])); 

Ama, ben hata alıyorum: tanımsız endeksi göndermek

2 Cevap

Ben şu desen kullanarak benim gönderme düğmeleri adı özelliklerini vermek istiyorum:

name="submit[which_action][which_category]"

Kategori 123 için 'Güncelle' butonuna örneğin:

name="submit[update][123]"

Kullanıcı kullanıcı tıklandığında hangi spesifik düğmesine belirlemek için gönderme düğmeleri herhangi tıkladığında sadece PHP kodu ['submit'] $ _POST kontrol gerekir:

$a1 = $_POST['submit'];
$which_action = reset(array_keys($a1));
$which_category = reset(array_keys($a1[$which_action]));

Peki ben böyle bir şey kullanmak istiyorsunuz:

<fieldset>
<!-- product info -->
<input name="productName[paint]" />
<input name="productName[edit]" />
<input name="productName[delete]" />
<input name="productName[cancel]" />
</fieldset>

Eğer serverside her şeyi olsun bu şekilde iç içe diziler güzel ve derli toplu olacaktır.