PHP:从单选框获取价值
PHP: Get Value from Radio Boxes
我正在做一个没有正确答案的测验,而是将比较和计算个性以创建匹配算法。到目前为止我有:
我有 3 个 table 涉及测验:问题,question_answers,question_choices:
questions:
+-------------+---------------+
| question_id | question |
+-------------+---------------+
| 1 | Is your... |
| 2 | Do you.. |
| 3 | Have you... |
|_____________|_______________|
question_choices:
+-------------+-------------+-------------+
| choice_id | question_id | choice_text |
+-------------+-------------+-------------+
| 1 | 1 | Somewhere.. |
| 2 | 1 | Somewhere.. |
| 3 | 1 | Somewhere.. |
| 4 | 2 | Yes |
| 5 | 2 | No |
| 6 | 3 | Yes |
| 7 | 3 | No |
|_____________|_____________|_____________|
question_answers:
+------------+---------+------------+------------+
| answer_id | user_id |question_id | choice_id |
+------------+---------+------------+------------+
| 1 | 125 | 1 | 2 |
| 2 | 3 | 1 | 3 | --> 125 & 3 = No Match
| 3 | 125 | 2 | 1 |
| 4 | 3 | 2 | 1 | --> 125 & 3 = Match
|____________|_________|____________|____________|
我现在正努力将 post 的所有相关信息转移到空的 question_answers table。但是,我很难提取所选选项的值,并且在单击提交时,所有问题仅显示为最后一个问题(问题 3)单击的选项。
我如何为每个单独的问题定制这个以及我如何 post 使用 UPDATE 将所有这三个结果添加到数据库中?
<?php
$choices = (choices());
$questions = (questions());
$questions_id = (questions_id());
$questions_length = count(questions());
for ($i = 0; $i < $questions_length; $i++){
$each_q = $questions[$i];
$each_q_id = $questions_id[$i];
?>
<div class="poll">
<div class="poll_question">
<?php echo $each_q_id . ". " . $each_q; ?>
</div>
<form action="" method="post">
<div class="poll_options">
<div class="poll_option">
<?php
$each_c = $choices[$i];
$choices_length = count($each_c);
for ($k = 0; $k < $choices_length; $k++){
$each_c = explode('<br />', $each_c);
?>
<input type="radio" name="$each_c" value="1" id="c1">
<label for="c1"><?php echo $each_c[0]; ?></label>
<input type="radio" name="$each_c" value="2" id="c2">
<label for="c2"><?php echo $each_c[1]; ?></label>
<?php if (!empty($each_c[2]) === true){ ?>
<input type="radio" name="$each_c" value="3" id="c3">
<label for="c2"><?php echo $each_c[2]; ?></label>
<?php
}}
$data = $_POST;
$each_cc = count($each_c);
print_r($session_user_id);
$option = 0;
if(isset($data['$each_c'])){
$option = $data['$each_c'];
echo "--".$each_q_id."--".$each_cc."--". $option. "<br>";
}
?>
</div>
</form>
<?php
}
?>
函数:
function choices(){
$query = mysqli_query($_POST['x'], "SELECT `question`, GROUP_CONCAT(`choice_text` ORDER BY `choice_text` DESC SEPARATOR '<br />') as `choice_texts` FROM `questions` JOIN `question_choices` ON questions.question_id = question_choices.question_id GROUP BY `question`");
while($row = mysqli_fetch_assoc($query)){
$rows[] = $row;}
if (count($rows)>0){
foreach ($rows as $key => $value) {
$c[] = $value['choice_texts'];
}
return $c;}else return false;
}
function questions_id(){
$query = mysqli_query($_POST['x'], "SELECT `question_id` FROM `questions` WHERE DATE(NOW()) BETWEEN `start` AND `end`");
while($row = mysqli_fetch_assoc($query)){
$rows[] = $row;}
if (count($rows)>0){
foreach ($rows as $key => $value) {
$q_id = $value['question_id'];
$array_q[] = ($q_id);
}
return $array_q;}else return false;
}
function questions(){
$query = mysqli_query($_POST['x'], "SELECT `question` FROM `questions` WHERE DATE(NOW()) BETWEEN `start` AND `end`");
while($row = mysqli_fetch_assoc($query)){
$rows[] = $row;}
if (count($rows)>0){
foreach ($rows as $key => $value) {
$q = $value['question'];
$array_q[] = $q;
}
return $array_q;}else return false;
}
首先,我认为在您的代码中,您为每个问题创建了不同的形式(即在第一个 for 循环中)。
使用第二个 for 循环,您正在尝试设置单选按钮,但您正在使用
与此循环无关的变量。
我正在对您的代码进行一些修改,因此在提交时 $_POST 将看起来: $_POS['1'] => 选择值,$_POST['2'] => 选择值...
其中“1”、“2”...是您的问题 ID。
<?php
$choices = (choices());
$questions = (questions());
$questions_id = (questions_id());
$questions_length = count(questions());
echo '<form action="#" method="post">';
for ($i = 0; $i < $questions_length; $i++) {
$each_q = $questions[$i];
$each_q_id = $questions_id[$i];
?>
<div class="poll">
<div class="poll_question">
<?php echo $each_q_id . ". " . $each_q; ?>
</div>
<div class="poll_options">
<div class="poll_option">
<?php
$each_c = $choices[$i];
$question = $questions[$i];
$choices_length = count($each_c);
for ($k = 0; $k < $choices_length; $k++){
$each_c = explode('<br />', $each_c);
?>
<input type="radio" name="<?php echo $each_q_id; ?>" value="<?php echo $choices[$i][$k]; ?>" id="c<?php echo $choices[$i][$k] ?>">
<label for="c<?php echo $choices[$i][$k] ?>"><?php echo $choices[$i][$k]; ?></label>
<?php
}
$data = $_POST;
$each_cc = count($choices[$i]);
print_r($session_user_id);
$option = 0;
if(isset($data[$each_q_id])){
$option = $data[$i];
echo "--" . $each_q_id . "--" . $each_cc . "--" . $option . "<br>";
}
?>
</div>
<?php
}
echo '<input type=submit value="submit" />';
echo '<br /></form><br /><br />';
if (!empty($_POST)) {
var_dump($_POST);
}
?>
(代表 OP 发布。)
通过将代码更改为以下内容解决了此问题:
<?php
$choices = (choices());
$questions = (questions());
$questions_id = (questions_id());
$questions_length = count(questions());
echo '<form action="#" method="post">';
for ($i = 0; $i < $questions_length; $i++) {
$each_q = $questions[$i];
$each_q_id = $questions_id[$i];
?>
<div class="poll">
<div class="poll_question">
<?php echo $each_q_id . ". " . $each_q; ?>
</div>
<div class="poll_options">
<div class="poll_option">
<?php
$each_c = $choices[$i];
$each_c = explode('<br />', $each_c);
$choices_length = count($each_c);
for ($k = 0; $k < $choices_length; $k++){
?>
<input type="radio" name="<?php echo $each_q_id; ?>" value="<?php echo $each_c[$k]; ?>" id="<?php echo $each_c[$k] ?>">
<label for="<?php echo $each_c[$k] ?>"><?php echo $each_c[$k]; ?></label>
<?php
}
$data = $_POST;
$each_cc = count($choices[$i]);
// print_r($session_user_id);
$option = 0;
if(isset($data[$each_q_id])){
$option = $data[$each_q_id];
echo $session_user_id . "--" . $each_q_id . "--" . $each_cc . "--" . $option . "--" . "<br>";
}
?>
</div>
<?php
}
echo '<input type=submit value="submit" />';
echo '<br /></form><br /><br />';
if (!empty($_POST)) {
var_dump($_POST);
}
?>
我正在做一个没有正确答案的测验,而是将比较和计算个性以创建匹配算法。到目前为止我有:
我有 3 个 table 涉及测验:问题,question_answers,question_choices:
questions:
+-------------+---------------+
| question_id | question |
+-------------+---------------+
| 1 | Is your... |
| 2 | Do you.. |
| 3 | Have you... |
|_____________|_______________|
question_choices:
+-------------+-------------+-------------+
| choice_id | question_id | choice_text |
+-------------+-------------+-------------+
| 1 | 1 | Somewhere.. |
| 2 | 1 | Somewhere.. |
| 3 | 1 | Somewhere.. |
| 4 | 2 | Yes |
| 5 | 2 | No |
| 6 | 3 | Yes |
| 7 | 3 | No |
|_____________|_____________|_____________|
question_answers:
+------------+---------+------------+------------+
| answer_id | user_id |question_id | choice_id |
+------------+---------+------------+------------+
| 1 | 125 | 1 | 2 |
| 2 | 3 | 1 | 3 | --> 125 & 3 = No Match
| 3 | 125 | 2 | 1 |
| 4 | 3 | 2 | 1 | --> 125 & 3 = Match
|____________|_________|____________|____________|
我现在正努力将 post 的所有相关信息转移到空的 question_answers table。但是,我很难提取所选选项的值,并且在单击提交时,所有问题仅显示为最后一个问题(问题 3)单击的选项。
我如何为每个单独的问题定制这个以及我如何 post 使用 UPDATE 将所有这三个结果添加到数据库中?
<?php
$choices = (choices());
$questions = (questions());
$questions_id = (questions_id());
$questions_length = count(questions());
for ($i = 0; $i < $questions_length; $i++){
$each_q = $questions[$i];
$each_q_id = $questions_id[$i];
?>
<div class="poll">
<div class="poll_question">
<?php echo $each_q_id . ". " . $each_q; ?>
</div>
<form action="" method="post">
<div class="poll_options">
<div class="poll_option">
<?php
$each_c = $choices[$i];
$choices_length = count($each_c);
for ($k = 0; $k < $choices_length; $k++){
$each_c = explode('<br />', $each_c);
?>
<input type="radio" name="$each_c" value="1" id="c1">
<label for="c1"><?php echo $each_c[0]; ?></label>
<input type="radio" name="$each_c" value="2" id="c2">
<label for="c2"><?php echo $each_c[1]; ?></label>
<?php if (!empty($each_c[2]) === true){ ?>
<input type="radio" name="$each_c" value="3" id="c3">
<label for="c2"><?php echo $each_c[2]; ?></label>
<?php
}}
$data = $_POST;
$each_cc = count($each_c);
print_r($session_user_id);
$option = 0;
if(isset($data['$each_c'])){
$option = $data['$each_c'];
echo "--".$each_q_id."--".$each_cc."--". $option. "<br>";
}
?>
</div>
</form>
<?php
}
?>
函数:
function choices(){
$query = mysqli_query($_POST['x'], "SELECT `question`, GROUP_CONCAT(`choice_text` ORDER BY `choice_text` DESC SEPARATOR '<br />') as `choice_texts` FROM `questions` JOIN `question_choices` ON questions.question_id = question_choices.question_id GROUP BY `question`");
while($row = mysqli_fetch_assoc($query)){
$rows[] = $row;}
if (count($rows)>0){
foreach ($rows as $key => $value) {
$c[] = $value['choice_texts'];
}
return $c;}else return false;
}
function questions_id(){
$query = mysqli_query($_POST['x'], "SELECT `question_id` FROM `questions` WHERE DATE(NOW()) BETWEEN `start` AND `end`");
while($row = mysqli_fetch_assoc($query)){
$rows[] = $row;}
if (count($rows)>0){
foreach ($rows as $key => $value) {
$q_id = $value['question_id'];
$array_q[] = ($q_id);
}
return $array_q;}else return false;
}
function questions(){
$query = mysqli_query($_POST['x'], "SELECT `question` FROM `questions` WHERE DATE(NOW()) BETWEEN `start` AND `end`");
while($row = mysqli_fetch_assoc($query)){
$rows[] = $row;}
if (count($rows)>0){
foreach ($rows as $key => $value) {
$q = $value['question'];
$array_q[] = $q;
}
return $array_q;}else return false;
}
首先,我认为在您的代码中,您为每个问题创建了不同的形式(即在第一个 for 循环中)。 使用第二个 for 循环,您正在尝试设置单选按钮,但您正在使用 与此循环无关的变量。 我正在对您的代码进行一些修改,因此在提交时 $_POST 将看起来: $_POS['1'] => 选择值,$_POST['2'] => 选择值... 其中“1”、“2”...是您的问题 ID。
<?php
$choices = (choices());
$questions = (questions());
$questions_id = (questions_id());
$questions_length = count(questions());
echo '<form action="#" method="post">';
for ($i = 0; $i < $questions_length; $i++) {
$each_q = $questions[$i];
$each_q_id = $questions_id[$i];
?>
<div class="poll">
<div class="poll_question">
<?php echo $each_q_id . ". " . $each_q; ?>
</div>
<div class="poll_options">
<div class="poll_option">
<?php
$each_c = $choices[$i];
$question = $questions[$i];
$choices_length = count($each_c);
for ($k = 0; $k < $choices_length; $k++){
$each_c = explode('<br />', $each_c);
?>
<input type="radio" name="<?php echo $each_q_id; ?>" value="<?php echo $choices[$i][$k]; ?>" id="c<?php echo $choices[$i][$k] ?>">
<label for="c<?php echo $choices[$i][$k] ?>"><?php echo $choices[$i][$k]; ?></label>
<?php
}
$data = $_POST;
$each_cc = count($choices[$i]);
print_r($session_user_id);
$option = 0;
if(isset($data[$each_q_id])){
$option = $data[$i];
echo "--" . $each_q_id . "--" . $each_cc . "--" . $option . "<br>";
}
?>
</div>
<?php
}
echo '<input type=submit value="submit" />';
echo '<br /></form><br /><br />';
if (!empty($_POST)) {
var_dump($_POST);
}
?>
(代表 OP 发布。)
通过将代码更改为以下内容解决了此问题:
<?php
$choices = (choices());
$questions = (questions());
$questions_id = (questions_id());
$questions_length = count(questions());
echo '<form action="#" method="post">';
for ($i = 0; $i < $questions_length; $i++) {
$each_q = $questions[$i];
$each_q_id = $questions_id[$i];
?>
<div class="poll">
<div class="poll_question">
<?php echo $each_q_id . ". " . $each_q; ?>
</div>
<div class="poll_options">
<div class="poll_option">
<?php
$each_c = $choices[$i];
$each_c = explode('<br />', $each_c);
$choices_length = count($each_c);
for ($k = 0; $k < $choices_length; $k++){
?>
<input type="radio" name="<?php echo $each_q_id; ?>" value="<?php echo $each_c[$k]; ?>" id="<?php echo $each_c[$k] ?>">
<label for="<?php echo $each_c[$k] ?>"><?php echo $each_c[$k]; ?></label>
<?php
}
$data = $_POST;
$each_cc = count($choices[$i]);
// print_r($session_user_id);
$option = 0;
if(isset($data[$each_q_id])){
$option = $data[$each_q_id];
echo $session_user_id . "--" . $each_q_id . "--" . $each_cc . "--" . $option . "--" . "<br>";
}
?>
</div>
<?php
}
echo '<input type=submit value="submit" />';
echo '<br /></form><br /><br />';
if (!empty($_POST)) {
var_dump($_POST);
}
?>