繁体   English   中英

如何计算 HTML 表格中的总数

[英]How to count total number in HTML Table

我有一个简单的 html 表,值为 1 和 0。我想计算单行中 1 的总数。 我如何使用 PHP 进行计数。 我搜索每一种方法,但所有与数据库链接的方法。 我尝试了计数函数,但它不起作用。我不知道下面哪个 PHP 函数更适合使用我的代码和 html 表值。

下面是我的表值

Number   Value  
   1    0
   0    0
   0    1
   1    1
   1    0
<?php 
 echo "Total number of 1's are".count(value == 1);
?>

我没有使用任何数据库。 完整代码如下:

<table width="600" border="1" align="center" cellspacing="5" bgcolor="#F0F0F0">
    <tr>
        <th>Number</th>
        <th>Value</th>
        <th>Find 1's</th>
    </tr> 
    <?php
        if(isset($_POST['submit']))
        {
            $x = $_POST['firstint'];
            $y = $_POST['secondint'];

            Count($x,$y);
        }

        function Count($x,$y)
        {
            for($i=$x; $i<=$y; $i++)
            {
                $value = $i/strlen($i);

                ?> 
                <tr>
                    <td width="68" align="center"><?php echo $i; ?></td>
                    <td width="68" align="center"><?php echo $value; ?></td>
                    <td width="68" align="center">
                    <?php 
                        if($value == 1)
                        { 
                            echo "One" ; 
                        }
                        else
                        { 
                            echo "Zero"; 
                        }
                    ?>
                    </td>
                </tr>
                <?php
            }
        ?>
        <tr>
            <td colspan="2" align="center"></td>
            <td align="left"><b>Total Count:</b></td>
            <td align="left"><b>
            <?php
                echo count($value == 1);
            ?>
            </b></td>
        </tr>
        <?php
        }
    ?>  
</table>

我真的无法运行你的代码(给了我很多错误),但无论如何都要这样做:定义一个计数器$total=0; if(isset($_POST['submit'])) ,将其声明为global $total; function Count($x,$y)下方,增加它$total++; 线下echo "One" ; , 并替换echo count($value == 1); 通过echo $total; , 像这样 :

            <table width="600" border="1" align="center" cellspacing="5" bgcolor="#F0F0F0">
        <tr>
        <th>Number</th>
        <th>Value</th>
        <th>Find 1's</th>
        </tr> 
        <?php
        $total = 0; // <==========================================
        if(isset($_POST['submit']))
        {
        $x = $_POST['firstint'];
        $y = $_POST['secondint'];

        Count($x,$y);
        }

        function Count($x,$y)
        { global $total; // <==========================================
        for($i=$x; $i<=$y; $i++)
        {
        $value = $i/strlen($i);

        ?> 
        <tr>
        <td width="68" align="center"><?php echo $i; ?></td>
        <td width="68" align="center"><?php echo $value; ?></td>
        <td width="68" align="center">
        <?php 
        if($value == 1){ 
        echo "One" ; 
        $total++; // <==========================================
        }else{ 
        echo "Zero"; 
        } ?>
        </td></tr>
        <?php
        }
        ?>
        <tr>
        <td colspan="2" align="center"></td>
        <td align="left"><b>Total Count:</b></td>
        <td align="left"><b>
        <?php
        echo $total; // <==========================================
        ?>
        </b></td>
        </tr>
        <?php
        }
        ?>  
        </table>

暂无
暂无

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

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