在表中输入Fibonacci生成PHP

时间:2018-01-22 08:35:13

标签: php fibonacci

您好我有以下脚本来生成表

<?php  error_reporting(null); ?>
<form action="" method="POST" autocomplete="off">
Row<br>
<input type="text" value="<?php echo $_POST['row'] ?>"  name="row"><br/>

Kolom<br>
<input type="text" value="<?php echo $_POST['kolom'] ?>" name="kolom">
<input type="hidden" name="aksi" value="gen">
<br/><br/>
<input type="submit" value="Submit">
</form>

<?php 
if($_POST['aksi']=="gen"){
    echo "<table border=1>";    
        for ($i=1; $i<=$_POST['row']; $i++) { ?>
        <tr>
                <?PHP for ($y=1; $y<=$_POST['kolom']; $y++) { ?>
                        <td>Data</td>
                <?php } ?>
        </tr>
    <?php }
echo "</table>";
}
?>

我有一个生成斐波那契数字的脚本

<?php

$first=0;
$second=1;


echo "$first $second";

for ($i=0; $i<10; $i++)
{

  $third = $second + $first;
  echo " $third";


  $first = $second;
  $second = $third;
}

?>

我想加入2个剧本。

当我生成一张桌子时,需要将斐波那契放在桌子内,我不知道如何解决这个问题。

这样的结果

enter image description here

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题。无法测试,如果有任何错别字,请保留它们:)

<?php  error_reporting(null); ?>
<form action="" method="POST" autocomplete="off">
Row<br>
<input type="text" value="<?php echo $_POST['row'] ?>"  name="row"><br/>

Kolom<br>
<input type="text" value="<?php echo $_POST['kolom'] ?>" name="kolom">
<input type="hidden" name="aksi" value="gen">
<br/><br/>
<input type="submit" value="Submit">
</form>

<?php 
if($_POST['aksi']=="gen"){

  $first=0;
  $second=1;
?>    
<table border=1>
<?php
  for ($i=0; $i<$_POST['row']; $i++) {
?>
  <tr>
<?PHP 
    for ($y=0; $y<$_POST['kolom']; $y++) {

      /* first two should be 0 and 1*/
      if($y < 2 && $i == 0) {
?>
    <td>0</td>
    <td>1</td>
<?php 
        $y += 2;
      } else {

        $third = $second + $first;
?>
    <td><?php echo($third); ?></td>
<?php 

        $first = $second;
        $second = $third;
      }
    }
?>
  </tr>
<?php
  }
}
?>
</table>