打印表格内的函数值?

时间:2014-07-25 13:30:42

标签: php function html-table

如何在表格中打印函数的值?我很难解决这个问题,在谷歌查找,观看一些Youtube PHP教程并且仍然没有提出任何问题,并且还有一个错误告诉我$ cols,$ rows,$ rNumber未定义。输出应该看起来像一个半金字塔。

<?php
do {
    $rNumber = mt_rand(3, 11);
} while ($rNumber % 2 == 0);

echo '<strong>Looping Statements Exercises</strong>';
echo '<br>';
echo '<br>';
echo 'This page shows eight (8) figures programatically generated by looping statements. Number of rows are random odd integer between 3 and 11.';
echo '<br>';
echo '<br>';


echo '<table border = "1">';
echo '<tr>';
echo '<td colspan = "4"><center>Size:<strong>', $rNumber, ' x ', $rNumber, '</strong>';
echo '</tr>';
echo '<tr>';
echo '<td> Figure A';
echo '<td> Figure B';
echo '<td> Figure C';
echo '<td> Figure D';
echo '</tr>';
echo '<tr>';
echo '<td> Figure A here';
echo '<td>', figure_b($cols, $rows), '</td>';
echo '<td> Figure C here';
echo '<td> Figure D here';
echo '</tr>';
echo '<tr>';
echo '<td> Figure E';
echo '<td> Figure F';
echo '<td> Figure G';
echo '<td> Figure H';
echo '</tr>';
echo '<tr>';
echo '<td> Figure E here';
echo '<td> Figure F here';
echo '<td> Figure G here';
echo '<td> Figure H here';
echo '</tr>';


function figure_b($rows, $cols)
{
    if ($rNumber == 5) {
        for ($rows = 0; $rows <= 5; $rows++) {
            for ($cols = 0; $cols <= 5; $cols++) {

                return $cols;
            }
            return $rows;
        }
    }
}

echo '</table>';                

3 个答案:

答案 0 :(得分:0)

你的职能:

function figure_b($rows, $cols)
{
if($rNumber == 5)
{
for($rows = 0; $rows<=5; $rows++)
{
for ($cols = 0; $cols<=5; $cols++)
{

return $cols;
}
return $rows;
}
}
}

是完全错误的。

应该是:

function figure_b($rNumber)
{
    if ($rNumber == 5) {
        for ($rows = 0; $rows <= 5; $rows++) {
            for ($cols = 0; $cols <= 5; $cols++) {
                echo $cols.' '; 
            }
            echo $rows.'<br />';
        }
    }
}

如果您想在表格中显示任何内容并使用rNumber变量(似乎此处不需要传递$rows$cols个变量)

但是你的代码非常糟糕,你不应该用这种方式混合PHP代码和HTML代码。如果您需要,您应该将代码的某些部分移动到其他文件而不要使用那么多echo因为它也会影响您的性能。

答案 1 :(得分:0)

你可以先尝试改变这个

 $rNumber = mt_rand(3,11);
}while ($rNumber % 2 == 0);

到此:

    $rNumber = mt_rand(3,11);                               
   while ($rNumber % 2 == 0);

和你的职能:

    function figure_b($rNumber) 
   {
               if($rNumber == 5) 
                 {
                     for($rows = 0; $rows<=5; $rows++) 
                       {
                           for ($cols = 0; $cols<=5; $cols++) 
                                {

                                    echo $cols;
                                 }
                                   echo $rows;
                          }
                  }
 }

答案 2 :(得分:-1)

<?php

 do{
                                        $rNumber = mt_rand(3,11);
                                        }while ($rNumber % 2 == 0);

                                        echo '<strong>Looping Statements Exercises</strong>';
                                        echo '<br>';
                                        echo '<br>';
                                        echo 'This page shows eight (8) figures programatically generated by looping statements. Number of rows are random odd integer between 3 and 11.';
                                        echo '<br>';
                                        echo '<br>';


                                        echo '<table border = "1">';
                                        echo '<tr>';
                                        echo '<td colspan = "4"><center>Size:<strong>',$rNumber,' x ',$rNumber,'</strong>';
                                        echo '</tr>';
                                        echo '<tr>';
                                        echo '<td> Figure A';
                                        echo '<td> Figure B';
                                        echo '<td> Figure C';
                                        echo '<td> Figure D';
                                        echo '</tr>';
                                        echo '<tr>';
                                        echo '<td> Figure A here';
                                        echo '<td>', figure_b(5, 5,$rNumber),'</td>';
                                        echo '<td> Figure C here';
                                        echo '<td> Figure D here';
                                        echo '</tr>';
                                        echo '<tr>';
                                        echo '<td> Figure E';
                                        echo '<td> Figure F';
                                        echo '<td> Figure G';
                                        echo '<td> Figure H';
                                        echo '</tr>';
                                        echo '<tr>';
                                        echo '<td> Figure E here';
                                        echo '<td> Figure F here';
                                        echo '<td> Figure G here';
                                        echo '<td> Figure H here';
                                        echo '</tr>';


                                        function figure_b($rows, $cols,$rNumber) 
                                        {
                                            if($rNumber == 5) 
                                            {
                                                for($rows = 0; $rows<=5; $rows++) 
                                                {
                                                    for ($cols = 0; $cols<=5; $cols++) 
                                                    {

                                                    return $cols;
                                                    }
                                                return $rows;
                                                }
                                            }
                                        }
                                        echo '</table>';

?>