如何计算HTML表中的总数

时间:2016-05-04 16:42:28

标签: javascript php html arrays

我有一个简单的html表,其值为1和0.我想计算单行中1的总数。我怎么能用PHP来计算。我搜索每个方法,但所有方法都链接到数据库。 我尝试了count函数,但它不起作用。我不知道下面使用哪个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>

1 个答案:

答案 0 :(得分:0)

我真的无法运行你的代码(给我这么多错误),但无论如何都要这样做:在$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> ,如下所示:

int userid;
SqlConnection sqlConnection1 = new SqlConnection("Your Connection String");
SqlCommand cmd = new SqlCommand();

cmd.CommandText = "SELECT [ID] FROM [users] WHERE ([email] = @myuser)";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();

using (SqlDataReader reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        userid =  Convert.ToInt16(reader.GetValue(reader.GetOrdinal("ID")));
    }
}

sqlConnection1.Close();