在html表循环中回显php数组

问题描述 投票:0回答:4

请运行html片段https://codepen.io/anon/pen/YeoZQr,我想用数组和foreach这样做,我花了几个小时但没有成功,请任何帮助将不胜感激。

我的PHP代码:

<?
$do['a']['ask'] = 'Do you agree with me';
$do['a']['text'][] = 'Completely Agree';
$do['a']['text'][] = 'Agree';
$do['a']['text'][] = 'Neutral';
$do['a']['text'][] = 'Disagree';
$do['a']['text'][] = 'Completely Disagree';

$do['b']['ask'] = 'All people are differents';
$do['b']['text'][] = 'Completely Agree';
$do['b']['text'][] = 'Agree';
$do['b']['text'][] = 'Neutral';
$do['b']['text'][] = 'Disagree';
$do['b']['text'][] = 'Completely Disagree';

foreach($do as $key => $value)
{
    $text= $do[$key]['text'];
    foreach($text as $ask_key => $ask_value)
    {
        echo "<table>
        <tr>
            <td style=\"width:50%;\"></td>
            <td>".$ask_value."</td>
        </tr>
        <tr>
            <td>".$do[1]['ask']."</td>
            <td><input type=\"radio\" name=".$key." value=".$ask_value." /></td>
        </tr>
        </table>";
    }
}
?>

提前致谢..

php html loops
4个回答
0
投票

这是我的代码,它可以是完美的但它可以解决这个问题

<?php
$do['a']['ask'] = 'Do you agree with me';
$do['a']['text'][] = 'Completely Agree';
$do['a']['text'][] = 'Agree';
$do['a']['text'][] = 'Neutral';
$do['a']['text'][] = 'Disagree';
$do['a']['text'][] = 'Completely Disagree';

$do['b']['ask'] = 'All people are differents';
$do['b']['text'][] = 'Completely Agree';
$do['b']['text'][] = 'Agree';
$do['b']['text'][] = 'Neutral';
$do['b']['text'][] = 'Disagree';
$do['b']['text'][] = 'Completely Disagree';
$html = '<table>';
$header = '';
$i = 0;
foreach ($do as $key => $value) {
    if (!$i++) {// onetime
        if (!$header) {//one time
            $header .= '<tr><td styl="width:50%;"></td>';
            foreach ($do[$key]['text'] as $texthead) {
                $header .= "<td>$texthead</td>";
            }
            $header .= '</tr>';
        }
        $html .= $header;
    }
    $html.='<tr>';
    foreach ($value as $k => $val) {
        if($k=='ask'){
            $html.="<td>$val</td>";
        }else{
            foreach ($val as $v){
                $html.="<td><input type='radio' name='$key' value='$v' /></td>";
            }

        }
    }
    $html.='</tr>';
}
$html .= '</table>';
echo $html;?>

0
投票

我认为最简单的解决方案是重写数组,因此您不会多次存储相同的数据。

$questions = ['a'=>'Do you agree with me','b'=>'All people are differents'];
$options = ['Completely Agree','Agree','Neutral','Disagree','Completely Disagree'];
echo "<table>";
echo "<tr><td>&nbsp;</td>";
foreach($options as $option){
  echo "<td>".$option."</td>";
}
echo "</tr>";
foreach($questions as $id => $question){
  echo "<tr><td>".$question."</td>";
  foreach($options as $option){
        echo "<td><input type=\"radio\" name=".$id." value=".$option." /></td>";
  }
  echo "</tr>";
}
echo "</table>";
?>

这假设您的答案选项永远不会改变。


0
投票

你可以做的一种方法是首先打印标题(至少我现在想的是)。然后在下一批打印输入。

echo '<table cellpadding=10>';
// print headers
echo '<tr><td></td>'; // empty cell
foreach ($do['a']['text'] as $text) {
    echo "<th>{$text}</th>";
}

// print rows
foreach ($do as $key => $value) {
    $ask = $value['ask']; // label for the row
    $radio_value = $value['text']; // values for radio button for each row
    echo '<tr>';
    echo "<td>{$ask}</td>";
    foreach ($radio_value as $radio) { // loop the values and create a list of radio buttons
        echo "<td><input type=\"radio\" name=\"input[{$key}]\" value=\"{$radio}\" /></td>";
    }
    echo '</tr>';
}
echo '</tr>';
echo '</table>';

只需使用每一行的键(ab)作为name属性,以便它们可以分组,因此在表单上下文中有意义。

如果你也控制数组,是的,你可以通过分离问题和选择(如果选择是不变的)来简化它。

$questions = ['a' => 'Do you agree with me', 'b' => 'All people are differents'];
$choices = ['Completely Agree', 'Agree', 'Neutral', 'Disagree', 'Completely Disagree'];

echo '<table cellpadding=10>';
// print headers
echo '<tr><td></td>'; // empty cell
foreach ($choices as $text) {
    echo "<th>{$text}</th>";
}
// print rows
foreach ($questions as $key => $question) {
    echo '<tr>';
    echo "<td>{$question}</td>";
    foreach ($choices as $radio) { // loop the values and create a list of radio buttons
        echo "<td><input type=\"radio\" name=\"input[{$key}]\" value=\"{$radio}\" /></td>";
    }
    echo '</tr>';
}
echo '</tr>';
echo '</table>';

应用相同的概念,打印标题然后打印问题。然后访问值也与表单中的值相同。使用适当的<form>标签和相应的method = POST / GET(您喜欢的),并相应地访问它们:

$input = $_GET['input'];
// or
$input = $_POST['input'];
foreach ($input as $question_key => $selected_choice) {
    // $question_key contains a or b
    // $selected_choice contains the selected radio
    // do what you have to do
}

如果这已经很明显,请忽略表单位部分。


0
投票

为了使它看起来像你的尝试,你可以做这样的事情

<?php
$radio = [
    'questions' => [],
    'choices' => []
];

$radio['questions']['a'] = 'Do you agree with me';
$radio['questions']['b'] = 'All people are differents';

$radio['choices'][] = 'Completely Agree';
$radio['choices'][] = 'Agree';
$radio['choices'][] = 'Neutral';
$radio['choices'][] = 'Disagree';
$radio['choices'][] = 'Completely Disagree';

echo "<table><tr><td style=\"width:50%;\"></td>";

foreach($radio['choices'] as $choice)
{
    echo "<td>$choice</td>";
}

echo "</tr>";

foreach($radio['questions'] as $id => $question)
{
    echo "<tr><td>$choice</td>";
    foreach($radio['choices'] as $choice)
    {
        echo "<td><input type="radio" name='$id' value='$choice'/></td>";
    }
    echo "</tr>";
}

echo "</table>";
?>

编辑:我更改了foreach($question),因为我昨天晚上只是复制了你的例子而没有设置value标签。

所以,我想你注意到了,但我冒昧地重命名你的变量(do => radioask => questionstext => choices)。避免重复数据的要点是明确哪个变量“直接依赖”哪个变量。

要创建你的整个收音机你将需要一切(这是逻辑,因为这段代码只创建所述收音机),所以我(就像你)制作了包含所有选择和问题的$radio表。然后,我们有问题。要创建它们中的每一个,您需要访问它们的文本,它们的id以及整套choices。但由于choices对于每个问题都是相同的,因此他们不需要“拥有”这些选择。什么“取决于”问题(每个问题可能会改变什么)只是它的id和文本。所以这就是我存储在每个$radio['questions']$id => $question_text的联想数组(再一次就像你一样)所以如前所述,如果choices不直接依赖于一个问题,那么他们就是radio选择集。所以他们直接属于radio

现在要创建无线电表,我们可以简单地迭代选择以创建第一行(标签),对于每个问题文本和id,我们创建一个带有问题文本的新行,并为每个选择一个带有相应id的单选框。

如果你想创建一个createRadio($radio)函数,这个数组和对象的方法非常酷,因为它包装数据和简洁。但是如果你想打印收音机的一些问题(你需要创建一个全新的$radio),它会变得有点无聊。

另一个选择是将$radio的概念“溶解”在两个数组$questionsand $choices中,这就是@TCooper在他的回答中所做的。它会让你失去数据集中化(在这种情况下,差异非常小)。另一方面,优点是你可以更自由地修改你的问题和选择(限制问题的数量或限制选择的集合,你只需要更改相应的表格),而且不需要知道如何你设想$radio数组称为createRadio($questions, $choices)函数。

© www.soinside.com 2019 - 2024. All rights reserved.