将数据分组到同一组

时间:2015-01-05 08:09:33

标签: php mysql

我试图通过名为' groups'的列来实现对数据进行分组。我一直在这里查看这篇文章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>';
    }
}
除了我的想法之外,还有其他任何可行的解决方法吗?

1 个答案:

答案 0 :(得分:1)

假设您的第二个查询检索所需的数据,请尝试以下版本的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表格。

我希望它可以帮助你。