简体   繁体   English

将循环输出返回到表(PHP)

[英]Return loop output to table (PHP)

I'm probably making a very basic error but I'm quite new to this. 我可能犯了一个非常基本的错误,但是我对此很陌生。

I have a table where I need to edit what is displayed in each box using variables but I'm having trouble with getting the outputs into the table. 我有一个表,我需要在其中使用变量来编辑每个框中显示的内容,但是在将输出内容放入表时遇到了麻烦。 Experimentation helped me work out the first box but I can't get the second one working because I think the function is written incorrectly. 实验帮助我确定了第一个框,但是我无法使第二个框正常工作,因为我认为函数编写不正确。 I need a conditional loop that displays all even numbers between 10 and 20 (the code below doesn't have anything to do with even numbers at the moment I'm just trying to get it to work) 我需要一个条件循环,显示所有10到20之间的偶数(下面的代码与偶数无关,我只是想使其正常工作)

<?php

$random = rand() . srand(3034);

function loop() {
    for ($i = 10; $i <= 20; $i++) {
      $loop = $i;
      return $loop;
    }
}

echo "<table border='1'>
      <tr>
       <td>Box 1 - ".$random."</td>
       <td>Box 2 - ".$loop."</td>
      </tr>

     </table> ";
?>

Any help is much appreciated. 任何帮助深表感谢。

You need to loop on the tags itself, because the return condition in the loop breaks it to only 1 iteration. 您需要循环标记本身,因为循环中的返回条件会将其中断为1次迭代。

So you should do it like this: 因此,您应该这样做:

echo "<tr>
    <td>Box 1 - ".$random."</td>";
for ($i = 10; $i <= 20; $i++) {
    echo "<td>Box 2 - ".$i."</td>";
}
echo"</tr>";

Additionally to my comment here is some more code: 除了我的评论,这里还有一些其他代码:

<?php
$random = rand() . srand(3034);

function loop($randomNumber) {
    for ($loop = 10; $loop <= 20; $loop++) {
        echo
            '<tr>' .
            '<td>Box 1 - ' . $randomNumber . '</td>' . 
            '<td>Box 2 - ' . $loop . '</td>' . 
            '</tr>';
    }
}

echo
    '<table border="1">' . 
    loop($random) . 
    '</table>';

Try this: 尝试这个:

<?php 

    function loop(){
         $return = '';
         for($i = 10; $i <=20; $i++){
              $random = rand() . srand(3034);
              if($i%2==0){
                 $return.='<tr>
                           <td>Box 1 - '.$random.'</td>
                           <td>Box 2 - '.$i.'</td>
                          </tr>';
              }
         }
        return $return;
    }

    echo '<table>'.loop().'</table>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM