dongxing2263 2018-12-05 09:05
浏览 104
已采纳

PHP - 将表链接到HTML并进行乘法运算

I have the following php code that generates a 10X10 table:

<?php
echo "<table border =\"1\" style='border-collapse: collapse'>";
for ($row=1; $row <= 10; $row++) { 
    echo "<tr> 
";
    for ($col=1; $col <= 10; $col++) { 
       $a = "$row * $col";
       echo "<td><a href = '$a'>$a</a></td> 
";
    }
    echo "</tr>";
}
echo "</table>";
?>
  1. How to recreate this table in HTML, such that links will work?

  2. Every field in a table needs to do a multiplication, e.g. field '5*6' gives result '30'. How to write a php class that will do this operation? So, for row*column, return variable result.

  • 写回答

2条回答 默认 最新

  • doulidai6316 2018-12-05 09:53
    关注

    in your Calculator.php

    <?php
    Class Calculator {
    
        public function calculate($row, $col){
            return $result = $row * $col;
        }
    }
    

    Change index.php as follow.

    <?php include_once('Calculator.php'); ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <style type="text/css">
            a{
                cursor: pointer;
            }
        </style>
    </head>
    <body>
        <div>
            <input type="text" value="<?php if( (isset($_GET['col']) && $_GET['row'])){ echo Calculator::calculate($_GET['row'],$_GET['col']); } ?>">
        </div>
        <?php
        echo "<table border =\"1\" style='border-collapse: collapse'>";
        for ($row=1; $row <= 10; $row++) { 
            echo "<tr> 
    ";
            for ($col=1; $col <= 10; $col++) { 
               $a = "$row * $col";
               echo "<td><a href=?row=$row&col=$col>".$a;
               //if( (isset($_GET['col']) && $_GET['row'] && $row==$_GET['row'] && $col==$_GET['col'])){ echo Calculator::calculate($row,$col); }else{ echo $a ;}
               echo "</a></td> 
    ";
            }
            echo "</tr>";
        }
        echo "</table>";
        ?>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 通联支付网上收银统一下单接口
  • ¥15 angular有偿编写,
  • ¥15 centos7系统下abinit安装时make出错
  • ¥15 hbuildex运行微信小程序报错
  • ¥15 关于#python#的问题:我知道这个问题对你们来说肯定so easy
  • ¥15 wpf datagrid如何实现多层表头
  • ¥15 为啥画版图在Run DRC会出现Connect Error?可我Calibre的hostname和计算机的hostname已经设置成一样的了。
  • ¥20 网站后台使用极速模式非常的卡
  • ¥20 Keil uVision5创建project没反应
  • ¥15 mmseqs内存报错