获取存储在多个数组中的值。复选框涉及

时间:2015-06-01 09:13:26

标签: php arrays

Heloo,

我有以下PHP代码:

 <?php       
$ratings = array(
            1 => "All",
            2 => "Love", 
            3 => "Hate",
            4 => "Maybe",
            5 => "Super"
);

$question = array(
            1 => "Pizza",
            2 => "Prajitura",
            3 => "Placinta",

);


 echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
  echo '<p>How do you feel about each topic?</p>';
foreach ($question as $key=>$value){
    echo "".$value."<br>";
    echo "aici are valoarea".$key;
    $importHtml="";
    foreach ($ratings as $cheie => $raspuns) {
         //   echo "cheie: ".$key."cheie raspuns:".$cheie."raspuns".$raspuns."\n";
            $importHtml .= "$raspuns <input type='checkbox' name='i_".$key."_importance[] id='$raspuns' value='$cheie' />";
}
echo "". $importHtml."<br>\n";
}

 echo '<input type="submit" value="Save Questionnaire" name="submit" />';
  echo '</form>';

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


foreach ($question as $key => $value) {
    //if(isset( $_REQUEST["i_".$key."_importance"]))
    $print="";
     $importance = $_REQUEST["i_".$key."_importance"];
     //var_dump($importance);
     echo "cheie >".$key."<br>\n";
    foreach($importance as $cheie=>$valoare){
        $print .="cheie".$cheie."valoare =".$valoare;
        //echo "cheie".$cheie."valoare =".$valoare;
        echo "<br>\n";
    }
    echo $print;
}
    }

    ?>

我希望使用foreach循环或for循环检索存储在名为i_ $ key_importance的每个数组中的值。结果并不像我预期的那样。在i_ $ key_importance中,如果客户端没有选中该框,则我想存储0值,如果已经这样做,则存储来自关联数组评级的键值。

预期结果:

 $importance = $_REQUEST["i_".$key."_importance"];
$importance will be an array containing {
1=> 1,
2=> 0 (here the user hasn't selected an option),
3=> 3,
4=> 0 (here the user hasn't selected an option),
}

结果将存储在具有Id =键的表中,并且每个答案的相应列将包含0(如果客户端未选择答案)或数字(如果已选择答案)。

任何帮助都非常感谢,

1 个答案:

答案 0 :(得分:0)

您应该考虑使用标签而不是常规复选框。

为了对复选框做你想做的事情,你可以测试复选框返回的值是否等于链接的值,如果不是那意味着用户没有检查它。