Get the values from $i , with disturbed order

橙三吉。 提交于 2020-01-22 03:47:06

问题


for ($i = 0; $i < count($name); $i++) 
   {
    //some output ommited

        <td><input type='submit' value='Purchase' name='$name[$i]'></td></tr>";
    }

$name[$i] = name1, name2, name3 , name4 , name5 , name6

Now once the button is clicked, this is what I should be displaying.

if (isset($_REQUEST['name1'])) {echo "name1 selected";}
if (isset($_REQUEST['name2'])) {echo "name2 selected";}
if (isset($_REQUEST['name3'])) {echo "name3 selected";}
if (isset($_REQUEST['name4'])) {echo "name4 selected";}
if (isset($_REQUEST['name5'])) {echo "name5 selected";}
if (isset($_REQUEST['name6'])) {echo "name6 selected";}

I have option in the db to enable or disable any name. If name3 is disabled , the sequence/ordering of $i will change.and I am not able to display the required parameter w.r.t $i.

For exmaple I want to display , with name3 disabled:

if (isset($_REQUEST['name4'])) {
    echo "$name[4] selected, kind is $kind[4] kind and type is $type[4]";
}

with name3 disabled , name4 will display values from name5

2:

How to get the values of Request with $name[$i]

for loop () {
    if (isset($_REQUEST['$name[$i]'])) {echo "name1 selected";} 
}

Help me device an algo to get the values of $i, so that even if the order is disturbed , i should know $i is carrying which value.


回答1:


First you have to concat $i to name to get unique names:

for ($i = 0; $i < count($name); $i++) 
{
   echo "<td><input type='submit' value='Purchase' name='$name . $i'></td></tr>";
}

If $name is "name" then you will get "name1", "name2", "name3", etc.

Then you can print_r($_REQUEST); to see the items submitted.

You do not need a for loop to see which of the items is set, but if you want to loop through the REQUEST array you could do something like this:

foreach($_REQUEST as $post_var) 
{
    if('name' == substr($post_var, 0, 4))
    {
        echo $post_var . ' ';
    }
}

Result:

name1 name2 name3



来源:https://stackoverflow.com/questions/36091062/get-the-values-from-i-with-disturbed-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!