Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以使用<;输入>;php函数中的标记,然后该函数位于<;表格>;标签?_Php_Html_Forms_Input - Fatal编程技术网

是否可以使用<;输入>;php函数中的标记,然后该函数位于<;表格>;标签?

是否可以使用<;输入>;php函数中的标记,然后该函数位于<;表格>;标签?,php,html,forms,input,Php,Html,Forms,Input,我对php和HTML都很陌生,在过去的几天里,我一直在努力学习它们。我正在尝试创建一个带有单选按钮的问卷,有56个问题 这是我现在的通用布局 你吃奶酪吗? 从不 过去 有时 通常 使用以下方法: <!DOCTYPE html> <html> <head> <title>Title of the document</title> </head> <body> <?php // Make s

我对php和HTML都很陌生,在过去的几天里,我一直在努力学习它们。我正在尝试创建一个带有单选按钮的问卷,有56个问题

这是我现在的通用布局


  • 你吃奶酪吗?


    从不
    过去
    有时
    通常

  • 使用以下方法:

    <!DOCTYPE html>
    <html>
    <head>
    <title>Title of the document</title>
    </head>
    
    <body>
    <?php
    
        // Make sure you have 56 elements in the array
        $questions = array("Do you always eat cheese?", "Are you human?",
                           "Do you have a dog?", "Mr. Murphy is funny?",
                           ...);
    
        for($i=1; $i < 57; $i++)
        {
            echo '<table>
                  <tr>
                      <td><p>'.$questions[$i-1].'</p></td>
                  </tr>
                  <tr>
                      <td><input type="radio" name="Q'.$i.'" value="0"/>Never</td>
                  </tr>
                  <tr>
                      <td><input type="radio" name="Q'.$i.'" value="1"/>In the past</td>
                  </tr>
                  <tr>
                      <td><input type="radio" name="Q'.$i.'" value="2"/>Sometimes</td>
                  </tr>
                  <tr>
                      <td><input type="radio" name="Q'.$i.'" value="4"/>Often</td>
                  </tr>
                  </table>';
       }
    ?>
    </body>
    </html>
    
    
    文件标题
    
    我将把它作为一个答案而不是评论来写,因为里面的代码太多了

    你应该做什么(在这种学习状态下):

    让函数返回稍后要回显的内容。其次,让我们纠正一个小错误:

    <?php
    
    function inputs($question) { // you were missing these { here
    
                               // missing ' here          and here + the dot  
        $html = '<input type="radio" name='.$question.' value="0"/>Never';
        // add some more html                 // better with " around the string
        $html += '<br/><input type="radio" name="'.$question.'" value="1" />In the past';
        // add whatever you like to add to that...
    
        // at the end let's return all we've got to use it later:
        return $html;
    }
    
    
    // and now you can do:
    $questions = array("Q1","Q2");
    for($i=0;$i<count($questions);$i++) {
         echo inputs($questions[$i]); // send only one question to the function
    }
    ?>
    
    
    我应该咬紧牙关写下问题吗

    绝对不是!那太多代码了

    如果我正确理解您的问题,您只需要更改56个不同问题的
    名称
    。当您使用数组时,我认为每次增加
    $I
    会更容易,如下所示:

    function inputs($counter) {
       $i = 0
       while($i < 57) {
          echo'
          <input type="radio" name="Q'.$i.'" value="0" 
          />Never
          <br />
          <input type="radio" name="Q'.$i.'" value="1" />In 
          the past
          <br />
          <input type="radio" name="Q'.$i.'" value="2" 
          />Sometimes
          <br /> 
          <input type="radio" name="Q'.$i.'" value="4" 
          />Often
          <br />
          <br /> 
          ';
          $i++
       }
    }
    
    功能输入($counter){
    $i=0
    而($i<57){
    回声'
    从未
    
    在里面 过去
    有时
    经常

    '; $i++ } }
    您可以这样做,也许:

    function inputs( $i ){
        $answers=array(
            'Never','In the past','Sometimes','Often'
        );
        foreach( $answers as $key => $answer ) echo "<div><input type='radio' name='Q{$i}' value='{$key}' />{$answer}</div>";
    }
    
    <p>Do you eat cheese?<p>
    <?php inputs(0);?>
    
    <p>Do you eat bananas?<p>
    <?php inputs(1);?>
    

    这个函数可以满足您的需要

    <?php
     function inputs($counter){
             $labels = ['Never','In the past','Sometimes','Often'];
             $questions = array("Q1","Q2");
    
        for($n=0; $n < $counter; $n++){
    
        echo '<input type="radio" name="'. $questions[0] .'" value="'. $n .'" />  '. $labels[$n] .' <br /> ';
        }
      }
    ?>
    
    <!--  html -->
    
    <p> Do you eat cheese? <p> <br />
    <?php echo inputs(5); ?>
    
    //where 5 is the number of inputs you need
    
    
    你吃奶酪吗 
    //其中5是您需要的输入数量

    函数中存在一些错误的引号/转义-正如我所理解的,字符串中的字符串属性值使用单引号时,字符串中的值应该用双引号引起来(一般来说),PHP变量需要用单引号转义,然后是句点(即:
    “.$var.”
    )Hi,每次都要更改的是
    名称
    ,从“Q1”更改为“Q2”?分配给单选按钮的值是否重要?即:
    0,1,2,4
    ?@代码查看是的,这是我唯一想更改的部分change@RamRaider它们在index.php脚本中用于根据问题的答案计算分数。我也知道我发布的函数代码有一些问题,所以谢谢你,我会尝试用它来修复它。谢谢你的代码,但这不会特别适合我,因为我需要始终拥有所有标签。我添加了一个名为$values的数组(其中$values=array(0,1,2,4);)和done value=“”。$values[$n]”;yoy可以在这个数组中放置任何你想要的标签$labels,它将自动创建。太好了,谢谢![此外,对于任何其他读者来说,这段代码确实可以产生一个可以在表单中读取的值。我还将测试其他人]从我得到的答案来看,这就是我的答案。这对我来说很简单,而且效果很好。非常感谢你![此评论是为以后阅读此评论的任何其他读者提供的]@samehanwar您应该在
    $questions
    中用
    $n
    替换
    ,所以我会在下面有56个输入区域。我知道你在做什么,这很有用,所以谢谢你!我想我会改变的-->只需去掉while循环和$I,并使计数器等于问题编号。一旦我这样做,我就可以确认它是否符合我的要求,谢谢!我知道重复的代码太多了,但我一辈子都想不出另一种方法。谢谢!代码的第二部分非常适合我想做的事情!嗨,杰夫,谢谢你指出一些语法错误,我现在意识到这些是很重要的。至于代码,我理解函数部分,但不理解第二部分。如果我在我的问题下面输入,无线电圈就不会出现,我用WAMPServer页面顶部的2个零来测试它。重要的是
    回声输入(无论什么)
    。现在我们将返回html以进行回应,这将更加优雅。嗨,伊万,谢谢你回复我。据我所知,您的代码可以满足我的需要,但我也可以看到您正在尝试使用表。这可能是我编码的方式,但无线电圈并没有出现在任何表格中,但我没有;我真的不介意,因为它能用。再次感谢!该表用于替换您列出的

    标记。同样,通过这种方式,您可以在
    单元格
    上放置
    CSS
    样式,并且每个
    收音机
    都有单独的容器。还有一点,默认情况下,
    表格
    边框/线条是不可见的。这将打印56个表格。
    <?php
     function inputs($counter){
             $labels = ['Never','In the past','Sometimes','Often'];
             $questions = array("Q1","Q2");
    
        for($n=0; $n < $counter; $n++){
    
        echo '<input type="radio" name="'. $questions[0] .'" value="'. $n .'" />  '. $labels[$n] .' <br /> ';
        }
      }
    ?>
    
    <!--  html -->
    
    <p> Do you eat cheese? <p> <br />
    <?php echo inputs(5); ?>
    
    //where 5 is the number of inputs you need