首页 文章

如何在php中对具有相似值的数据进行分组?

提问于
浏览
1

我在数据库中有一个名为'transactions'的表 . 我想列出这个表中的值 . 结果应以HTML表格格式显示 . 结果表中的行应按金额分组 . “transactions”表中有一个名为“amount”的列 .

这是我的代码:

$s1 = DB::table('transactions')
        ->select(DB::raw("GROUP_CONCAT(selected_seats SEPARATOR '') as selected_seats"),'userid','amount','show_id')
        ->where("show_id","=",$s)  
        ->groupBy('userid')
        ->groupBy('amount')
        ->orderBy('userid','ASC')
        ->orderBy('amount', 'DESC')
        ->get();

我正在通过循环从$ s1变量中检索值 .

以下是输出:

enter image description here

我想要这样的结果:

enter image description here

请点击上面给出的链接 . 我是这个论坛的新手所以它不允许我在问题中添加图像 . 请帮助 .

这是整个代码:

if($s1 != null)

{

echo "<div class='panel-body'>";
    echo "<table class='table table-responsive'>"; $c = 1;
    echo "<th>Drama Title</th>";
    echo "<th>Show Date</th>";
    echo "<th>Seat booked</th>";
    echo "<th>Amount</th>";
    echo "<th>User</th>";


    for($i=0;$i<count($s1);$i++)
    {



        echo "<tr><td>".$shows1[0]->show_title."</td>";
        echo "<td>".$shows[0]->show_date."</td>";
        echo "<td>";
        echo $s1[$i]->selected_seats; 
        echo "</td>";
        /*echo $s1[$i]->userid."
"; echo $s1[$i]->amount."
";*/ $transactions = DB::table('transactions')->where('userid',$s1[$i]->userid)->where('show_id',$s1[$i]->show_id)->get(); //var_dump($transactions); $total_amount = 0; //echo "<pre>Users
"; var_dump($transactions); foreach ($transactions as $transaction) { //echo "userid&nbsp;".$s1[$i]->userid."&nbsp;"."show id:&nbsp;".$transaction->show_id."&nbsp;&nbsp;"."
"; // echo "amount:&nbsp;".$transaction->amount."&nbsp;"; $amount = $transaction->amount; $total_amount = $total_amount + $amount; //echo $amount."
"; $c = $c+1; //echo "no. of seats:".$c; $users = DB::table('users')->where('id',$s1[$i]->userid)->get(); } echo "<td>".$total_amount."</td>"; //echo $s1[$i]->userid."
"; //echo "<td>".$users[0]->name."</td>"; //echo "<pre>"; var_dump($users); echo "</td>"; if(isset($users[0])) { //echo "values are set"; echo "<td>".$users[0]->name."</td></tr>"; } else { //echo "null value"; continue; } } /*echo $shows[0]->show_date."
"; echo $shows[0]->show_title;*/ //echo "<pre>"; var_dump($s1); //echo "<td>".$users[0]->name."</td>"; //echo "</tr>"; echo "</table>"; echo "</div>"; ?>

} else {echo“找不到记录”;

}

1 回答

  • 0

    我认为最简单的方法是计算循环中的数量,即打印数据或准备要发送到视图的数据 . 由于您的数据按user_id排序,因此当user_id发生变化时,您总是可以在循环中更改amount变量 . 您可以使用 count(explode($seat_variable)) 获取循环的单次运行中的席位数 . 请看下面的示例代码 .

    $num_seat = 0; $amount = 0; $running_user_id = -1; foreach ($row as $entry) { ... if ($running_user_id != $entry['user_id']) { // check if the same user $running_user_id = $entry['user_id']; $num_seat = 0; $amount = 0; // resetting variable values for seperate user. } $num_seat += count(explode($entry['selected_seats'])); $amount += $entry['amount']; ... }

    我假设您在查询中丢失了电子邮件等数据 .

    Code update after questioner added his code.

    if($s1 != null) {
        echo "<div class='panel-body'>";
        echo "<table class='table table-responsive'>"; $c = 1;
        echo "<tr>";
        echo "<th>Drama Title</th>";
        echo "<th>Show Date</th>";
        echo "<th>Seat booked</th>";
        echo "<th>Amount</th>";
        // echo "<th>User</th>";
        echo "</tr>";
    
    $category = ""; $num_seats = 0;
    for ($i=0; $i<count($s1); $i++) {
        if ($category != $amount) { 
            if ($i > 0) { // print totals 
                echo "<tr>
                <td colspan='3' align='right'>Total</td>
                <td>".$num_seats."</td>
                <td>".($num_seats * $amount)."</td>
            </tr>";
    
                echo "<tr><td colspan='5'>&nbsp;</td></tr>"; // empty row after totals printed
                $num_seats = 0; //resetting number of seats per category
            }
            echo "<tr><td colspan='5'><h2>Category : ".$amount."</h2></td></tr>"; // printing category line in the given table
            $category = $amount; // this logic is to prevent category line printing for each row
        }
        $transactions = DB::table('transactions')->where('userid',$s1[$i]->userid)->where('show_id',$s1[$i]->show_id)->get();
        echo "<tr>";
    
        $users = DB::table('users')->where('id', $s1[$i]->userid)->get();
        // Check below values are correct or not
        echo "<td>".$users[0]->name."</td>";
        echo "<td>".$s1[$i]->userid."</td>";
        echo "<td>".$s1[$i]->email."</td>"; // get user's email property here
        echo "<td>".$s1[$i]->selected_seats."</td>";
    
        $num_seats += count(explode(',', $s1[$i]->selected_seats));
        echo "<td>&nbsp;</td></tr>"; // empty colomn for amount and the row end
    }
        echo "</table>";
        echo "</div>";
    }
    

相关问题