在foreach中有问题吗?

时间:2015-02-16 09:55:31

标签: php

在foreach中遇到问题。基于复选框值,我列出了一些形式,但这里的问题是foreach不是将值分开。任何一个指南我必须改变。我必须改变

<form name="frm" method="post">
    <table align="center">
    <tr>
    <td>Name</td>
    <td><input type="text" name="txt" id="txt" required ></td><td></td>
    </tr>
    <tr>
    <td>Product</td>
    <td>
    <input type="checkbox" name="chk[]" value="class1">class1
    <input type="checkbox" name="chk[]" value="class6">class6
    <input type="checkbox" name="chk[]" value="class8">class8
    <input type="checkbox" name="chk[]" value="class10">class10
    <input type="checkbox" name="chk[]" value="class12">class12
    <input type="checkbox" name="chk[]" value="engineering">engineering
    <input type="checkbox" name="chk[]" value="technology">technology
    </td><td></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" name="submit" value="Save"></td><td></td>
    </tr>
    </table>
    </form>

**My PHP VALUES**


 <?php


     if (isset($_POST['submit']))
        {
            $name    = $_POST['txt'];
            $product = $_POST['chk'];
            print_r($product);
            $people = array("class1", "class6", "class8", "class10");
foreach($product as  $v)
{
   if (in_array($v, $people)) 
    {
        if($v=="class1")
        {
            echo "the class one is";
        }    
        else if($v=="class6" && $v="class1")
        {
            echo "the combiane class1 and class6";
        }    
    }
}
        }
    ?>

2 个答案:

答案 0 :(得分:1)

for($i=0; $i<count($product); $i++){
    if (in_array($product[$i], $people)) {
        if($product[$i]=="class1"){
            echo "the class one is";
        }else if($product[$i]=="class6" && $product[$i]="class1"){
            echo "the combiane class1 and class6";
        }
    }
}

答案 1 :(得分:0)

$people = array("class1", "class6", "class8", "class10");
foreach($product as  $v)
{
   if (in_array($v, $people)) 
    {
        if($v=="class1")
        {
            echo "class1";
        }    
        else if($v=="class6")
        {
            echo "class6";
        }    
    }
}

<强> Demo

相关问题