简体   繁体   中英

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. How can I count with using 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.

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; before if(isset($_POST['submit'])) , declare it global $total; below the line function Count($x,$y) , increase it $total++; below the line echo "One" ; , and replace echo count($value == 1); by 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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