简体   繁体   English

如何计算 HTML 表格中的总数

[英]How to count total number in HTML Table

I have simple html table with value of 1 and 0. I want to count total number of 1's in single row.我有一个简单的 html 表,值为 1 和 0。我想计算单行中 1 的总数。 How can I count with using PHP.我如何使用 PHP 进行计数。 I search every method but all methods linked with database.我搜索每一种方法,但所有与数据库链接的方法。 I tried count function but its not working.I don't know which PHP function is better to use below is my code and html table values.我尝试了计数函数,但它不起作用。我不知道下面哪个 PHP 函数更适合使用我的代码和 html 表值。

Below is my table values下面是我的表值

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

I am not using any database.我没有使用任何数据库。 The full code is below:完整代码如下:

<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>

I really couldn't run your code (gives me so many errors), but do this anyway : define a counter $total=0;我真的无法运行你的代码(给了我很多错误),但无论如何都要这样做:定义一个计数器$total=0; before if(isset($_POST['submit'])) , declare it global $total;if(isset($_POST['submit'])) ,将其声明为global $total; below the line function Count($x,$y) , increase it $total++;function Count($x,$y)下方,增加它$total++; below the line echo "One" ;线下echo "One" ; , and replace echo count($value == 1); , 并替换echo count($value == 1); by echo $total;通过echo $total; , like this : , 像这样 :

            <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