php mysql数据库获得总价

时间:2017-08-16 07:09:46

标签: php mysql

我使用php代码和mysql数据库将数据显示到html表中,并且我在listview上使用过滤器,现在我已经过滤了并且获得了价格总和。我对PHP代码不够好,我错误地使用php或sql?

MySQL表:

id INT - 名称VARCHAR - 代码VARCHAR - 价格INT

到目前为止,这是我的代码:

<?php

if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `toy` WHERE CONCAT(`id`, `name`, `code`, `price`) 
LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);

}
else {
$query = "SELECT * FROM `toy` LIMIT 5";
$search_result = filterTable($query);
}

// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "user", "pass", 
"database");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}

?>

<!DOCTYPE html>
<html>
<head>
    <title>Wu</title>
    <link href="style.css" type="text/css" rel="stylesheet" />

</head>
<body>

    <div id="toys-grid"> 
        <div class="search-box">
        <br>            
        <form action="index.php" method="post">
        <input type="text" name="valueToSearch" placeholder="Name To Search">
        <br><br>
        <input type="submit" name="search" value="Filter"><br><br>
        </div>

        <table cellpadding="10" cellspacing="1">

    <thead>
            <tr>
                <th>Id</th>
                <th>First Name</th>
                <th>Code</th>
                <th>Price</th>
            </tr>
    </thead>
  <!-- populate table from mysql database -->
            <?php while($row = mysqli_fetch_array($search_result)):?>
            <tbody>
            <tr>
                <td><?php echo $row['id'];?></td>
                <td><?php echo $row['name'];?></td>
                <td><?php echo $row['code'];?></td>
                <td><?php echo $row['price'];?></td>
            </tr>
            </tbody>
            <?php endwhile;?>

            <thead>
            <tr>
            <th> Total </th>
        <th> 
        <?php
        $results = mysql_query('SELECT SUM(price) FROM toy');
        $results->execute();
        for($i=0; $rows = $results->fetch(); $i++){
        echo $rows['SUM(price)'];
        }
        ?>
        </th>
        </tr>
        </thead>
        </table>
        </form>
    </div>
</body>

感谢您的帮助......

1 个答案:

答案 0 :(得分:0)

无需执行查询即可获得总和。你可以用php

来做
<table cellpadding="10" cellspacing="1">

    <thead>
            <tr>
                <th>Id</th>
                <th>First Name</th>
                <th>Code</th>
                <th>Price</th>
            </tr>
    </thead>
  <!-- populate table from mysql database -->
    <?php $sum = 0; ?>
    <?php while($row = mysqli_fetch_array($search_result)):?>
    <tbody>
    <tr>
        <td><?php echo $row['id'];?></td>
        <td><?php echo $row['name'];?></td>
        <td><?php echo $row['code'];?></td>
        <td><?php echo $row['price'];?></td>
        <?php
        $sum += $row['price'];
        ?>
    </tr>
    </tbody>
    <?php endwhile;?>

    <thead>
      <tr>
        <th> Total </th>
        <th> 
        <?php
        echo $sum;
        ?>
        </th>
      </tr>
    </thead>
</table>