php - 将数据分组到同一组

标签 php mysql

我正在尝试通过名为“组”的列对数据进行分组。我一直在这里看这篇文章Categorizing mysql data into seperate html tables?

这也适用于我的情况,但相同的组会分开

原来是这个样子

 _____________________________________
|_____________Group ZXY_______________|
|   Month       |  Bet  |  Win |Payout|
|_______________|_______|______|______|
|Name|Group|Desc|       |      |      |
|_______________|_______|______|______|
|Name|Group|Desc|       |      |      |
|_______________|_______|______|______|
|_____Total_____|_______|______|______|
 _____________________________________
|_____________Group ZXY_______________|
|   Month       |  Bet  |  Win |Payout|
|_______________|_______|______|______|
|Name|Group|Desc|       |      |      |
|_______________|_______|______|______|
|Name|Group|Desc|       |      |      |
|_______________|_______|______|______|
|_____Total_____|_______|______|______|

如您所见,返回的数据属于同一组,但被分开了。在这种情况下必须只使用 1 个表

我的想法总结

组循环 添加

<table><th>..</th></table>

变量内的标签

数据获取循环 添加

<tr><td>..</td></tr>

变量内的标签

这是代码

$sql='SELECT DISTINCT `groups` FROM `kiosk_data` WHERE `groups` IS NOT NULL ORDER BY `groups` ASC';
$grpData='';
$grpHr[]=array();

//this is the code I used to fill im th contents of the dropdown box
$result = $conn->query($sql);
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
 if(!empty($row['groups']))
 {
    $selected = (isset($_GET['grp']) && $_GET['grp']==$row['groups'])?'selected="selected"':'';
    $grpData.='<option value="'.$row['groups'].'"'.$selected.'>'.$row['groups'].'</option>';
    $grpHr[]=$row['groups'];//took this opportunity to store the values of groups in grpHr[] Array
 }
}

$sql = "SELECT COALESCE(kd.`kiosk_name`,'Total') AS 'Kiosk', 
FORMAT(SUM(`casino_bets`),2) AS 'CBTotal', FORMAT(SUM(`bet_win`),2) AS 'BWTotal'
,FORMAT((`casino_bets`-`bet_win`)/`casino_bets`*100,2) AS 'Payout', groups  FROM 
`kiosk_data` kd LEFT JOIN `kiosk_status` ks ON kd.`kiosk_id`=ks.`kiosk_id` WHERE 
`kiosk_name` IS NOT NULL AND `casino_bets` IS NOT NULL AND `bet_win` IS NOT NULL";
$result = $conn->prepare($sql);
$result ->execute();


foreach ($grpHr as $key => $value) {//iterate til the last group name 
    $key = $row['groups'];//I think I need to do this before entering the fetch loop
   echo '<table><tr><td colspan="6">'.$row['groups'].'</td></tr><tr><th colspan="3">'.date("m",strtotime($row['date'])).'Month</th><th>Bet</th><th>Bet-Win</th><th>Payout %</th></tr>';
    while ($row = $result->fetch(PDO::FETCH_ASSOC))
    {
       echo '<tr><td>'.$row['Kiosk'].'</td><td>'.$row['groups'].'</td><td>'.$row['city'].'</td><td>'.$row['CBTotal'].'</td><td>'.$row['BWTotal'].'</td><td>'.$row['Payout'].'</td></tr>';
    }
}

除了我的想法之外,还有其他更好的解决方法吗?

最佳答案

假设您的第二个查询检索到所需的数据,请尝试使用以下版本的 PHP 代码:

$sql='SELECT DISTINCT `groups` FROM `kiosk_data` WHERE `groups` IS NOT NULL ORDER BY `groups` ASC';
$grpData='';
$grpHr[]=array();

//this is the code I used to fill im th contents of the dropdown box
$result = $conn->query($sql);
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
    if(!empty($row['groups']))
    {
        $selected = (isset($_GET['grp']) && $_GET['grp']==$row['groups'])?'selected="selected"':'';
        $grpData.='<option value="'.$row['groups'].'"'.$selected.'>'.$row['groups'].'</option>';
        $grpHr[]=$row['groups'];//took this opportunity to store the values of groups in grpHr[] Array
    }
}

$sql = "SELECT COALESCE(kd.`kiosk_name`,'Total') AS 'Kiosk', 
FORMAT(SUM(`casino_bets`),2) AS 'CBTotal', FORMAT(SUM(`bet_win`),2) AS 'BWTotal'
,FORMAT((`casino_bets`-`bet_win`)/`casino_bets`*100,2) AS 'Payout', groups  FROM 
`kiosk_data` kd LEFT JOIN `kiosk_status` ks ON kd.`kiosk_id`=ks.`kiosk_id` WHERE 
`kiosk_name` IS NOT NULL AND `casino_bets` IS NOT NULL AND `bet_win` IS NOT NULL";
$result = $conn->prepare($sql);
$result ->execute();

$grpsArr = array();    
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
    if( ! isset($grpsArr[$row['groups']])) {
        $grpsArr[$row['groups']] = array();
    }
    $grpsArr[$row['groups']][] = $row;
}

foreach ($grpsArr as $grpName => $grpRowArray) {    
    echo '<table><tr><th colspan="6">'.$grpName.'</td></tr><tr><th colspan="3">'
         .date("m",strtotime($row['date']))
         .' Month</th><th>Bet</th><th>Bet-Win</th><th>Payout %</th></tr>';

    foreach($grpRowArray as $grpRow) {
        echo '<tr><td>'.$grpRow['Kiosk'].'</td><td>'.$grpRow['groups'].'</td><td>'.$grpRow['city'].'</td><td>'.$grpRow['CBTotal'].'</td><td>'.$grpRow['BWTotal'].'</td><td>'.$grpRow['Payout'].'</td></tr>';
    }

    echo '</table>';
}

改变的是while/foreach循环。请在下面找到解释。

while循环

$grpsArr = array();    
while ($row = $result->fetch(PDO::FETCH_ASSOC))
{
    if( ! isset($grpsArr[$row['groups']])) {
        $grpsArr[$row['groups']] = array();
    }
    $grpsArr[$row['groups']][] = $row;
}

此循环遍历上次查询返回的所有行,将数据放入 $grpsArr 中嵌套数组。部分:

        if( ! isset($grpsArr[$row['groups']])) {
            $grpsArr[$row['groups']] = array();
        }

检查是否至少有一行来自 $row['groups']在整个迭代过程中进行分组 - 如果到目前为止还没有这样的行,则为 $row['groups'] 创建子数组组名;

行:

        $grpsArr[$row['groups']][] = $row;

将当前迭代行添加到 $row['groups'] 中指示的组的子数组中.

从记录集中获取最后一条记录后,$grpsArr数组应如下所示:

grpsArr = array {
                "group_1" => array {
                                    array { 
                                            "Kiosk"    => "KioskVal_1",
                                            "groups"   => "group_1",
                                            "city"     => "city_1",
                                            "CBTotal"  => 123.45
                                            "BWTotal"  => ...
                                            "Payout"   => ...
                                          } ,
                                    ( ......... ) , 
                                    array { 
                                            "Kiosk"    => "KioskVal_31",
                                            "groups"   => "group_1",
                                            "city"     => "city_11",
                                            "CBTotal"  => 123.45
                                            "BWTotal"  => ...
                                            "Payout"   => ...
                                          }
                                  }
                ( .................... ),
                "group_m" => array {
                                    array { 
                                            "Kiosk"    => "KioskVal_m",
                                            "groups"   => "group_m",
                                            "city"     => "city_m",
                                            "CBTotal"  => 123.45
                                            "BWTotal"  => ...
                                            "Payout"   => ...
                                          } ,
                                    ( .................... ) , 
                                    array { 
                                            "Kiosk"    => "KioskVal_m2",
                                            "groups"   => "group_2",
                                            "city"     => "city_mm",
                                            "CBTotal"  => 123.45
                                            "BWTotal"  => ...
                                            "Payout"   => ...
                                          }
                                  }
                }

foreach循环

foreach ($grpsArr as $grpName => $grpRowArray) {  
    echo '<table><tr><th colspan="6">'.$grpName.'</td></tr><tr><th colspan="3">'
         .date("m",strtotime($row['date']))
         .' Month</th><th>Bet</th><th>Bet-Win</th><th>Payout %</th></tr>';

    foreach($grpRowArray as $grpRow) {
        echo '<tr><td>'.$grpRow['Kiosk'].'</td><td>'.$grpRow['groups'].'</td><td>'.$grpRow['city'].'</td><td>'.$grpRow['CBTotal'].'</td><td>'.$grpRow['BWTotal'].'</td><td>'.$grpRow['Payout'].'</td></tr>';
    }

    echo '</table>';
}

主循环foreach ($grpsArr as $grpName => $grpRowArray)遍历 $grpsArr 的第一层表,获取当前处理组的组名,然后为该特定组构建带有标题行的 HTML 表开始标记。

内部循环 foreach($grpRowArray as $grpRow)遍历包含属于当前处理组的行的数组,并根据获得的数据构建 HTML 表格行。

有一个问题:您在代码中使用了.date("m",strtotime($row['date'])).为小组写下正确的日期/时间。但是没有 date字段在最后一个查询的结果中 - 尽管如此,我也把它放在我的代码中。

这种方法应该为每个组提供一个 HTML 表格。

希望对你有所帮助。

关于php - 将数据分组到同一组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27775554/

相关文章:

mysql - GET DATA SQL 查询函数出错

php - 付款成功后无法从 Paypal 获得有效响应

php - session_register 工作但 $_SESSION 不 :/

php - Mysql group_concat 与 unique 和 where 给出奇怪的结果

php - 无法在 Ubuntu 16.04 上安装 php5-pgsql

php - 自定义数据库连接在 php 中不起作用

mysql - 如何在 Ubuntu 19.04 (Disco Dingo) 中编辑 php.ini 文件

条件语句中的 MySQL 子查询

php - 索引 "server_name"不存在

mysql - 如何在一定时间间隔后更新表