php - 过滤文件中的查询结果

标签 php mysql

我正在开发一个网站。 这是我当前代码的一部分:

$num_cat_rec = mysqli_query($mysqli, "SELECT * FROM tb_revisiones_tor GROUP BY tipo_tor")
              or die (mysqli_error($dbh));

//display the results
while ($row_tor = mysqli_fetch_array($num_cat_rec))
{
    $puntos_tor_conseguidos =0; 
    ?>
    <div class="col-lg-4 col-xs-4">
        <!-- small box -->
        <div class="small-box bg-purple">
            <div class="inner">
                <?php 
                $usuario_tor = $row_tor['evaluado'] ;
                $tipo_tor = $row_tor['tipo_tor'];
                $puntos_tor_conseguidos = $puntos_tor_conseguidos+get_puntos_tor_conseguidos($usuario_tor,$tipo_tor);
                ?>
                <h3><?php echo $puntos_tor_conseguidos?></h3>
                <H4><?php echo $row_tor['nombre']?></h4>
            </div>
            <div class="icon">
                <i class="ion ion-ribbon-a"></i>
            </div>
            <a href="otros_rubros_reporte.php" class="small-box-footer">
                <?php echo $lang['MORE_INFO']?> <i class="fa fa-arrow-circle-right"></i>
            </a>
        </div>
    </div><!-- ./col -->
    <?php
}

这是当前的输出:

enter image description here

这是数据库结构和一些数据。

enter image description here

正如您所见,我按字段nombre显示数据组,如图中蓝色矩形所示。

我需要的是显示每个nombre组的字段puntos的总和,但仅限于某些用户(字段evaluado)强>)

所选用户的条件必须是:

get_unit($row_tor['evaluado']= $_POST['un'];

get_unit(x) 是 PHP 文件中的一个函数。

我该怎么做?

最佳答案

  1. 将您的查询修改为:

    SELECT SUM(puntos) as puntos_sum,* FROM tb_revisiones_tor GROUP BY tipo_tor
    

    获取每组nombrepuntos总和

  2. 添加显示总和的条件:

    if ($row_tor['evaluado']==$_POST['un']){
       echo $row_tor['puntos_sum'];
    }
    

    注意:使用==进行比较,而不是=

关于php - 过滤文件中的查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47118404/

相关文章:

php - Apache、PHP、MySQL、PDO、权限被拒绝、laravel

php - 如何为 JavaScript 和 JSON 正确编码 UTF-8?

php - 更新数据库并更新变量

php - 如何编写 MySQL PHP 嵌套查询

php - 使用 SimpleXML 读取参数名称

php - Laravel PayPal 支付包

PHP 页面首先显示 else $errormessage 而不检查代码

php - Laravel 的多个数据库配置文件

MySQL:两个表共享公共(public) "date"列,如何在 JOIN 后将日期一起排序?

php - "MySQL server has gone away"后跟 "Got a packet bigger than ' max_allowed_pa​​cket ' bytes"