将数据分组到同一组

grouping datas with same group

我正在尝试按名为 'groups' 的列对数据进行分组。我一直在看这个 post 这里 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

我的想法总结

组循环 添加

<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)遍历第一层$grpsArrtable,得到当前处理的组的组名,然后构建HTMLtable 此特定组的起始标记为 header 行。

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

有一个问题:您在代码中使用 .date("m",strtotime($row['date'])). 为组编写正确的 date/time。但是,上次查询的结果中没有 date 字段 - 尽管如此,我也将它放在我的代码中。

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

希望对你有所帮助。