如何在获取asoc的同时制作表格 [英] How can i make a table while fetch assoc

查看:109
本文介绍了如何在获取asoc的同时制作表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我是一个(初学者)php后端开发人员,并且我正在dj面板上工作,但是它无法以我尝试的尽可能多的方式正确工作,但是我无法使其正常工作..

Hello i'm a (beginning) php backend dev and i'm working on a dj panel but it doesnt work the right way i tried as many things as i could but i cant get it to work..

    $active_ids = '1, 3, 4';

    $query = "SELECT * FROM users WHERE id IN ({$active_ids})";
    $result = $mysqli->query($query);   

    $query2 = "SELECT dj, count(*) AS n FROM timetable WHERE dj IN ({$active_ids}) GROUP BY dj";
    $result2 = $mysqli->query($query2);

        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()){
                echo "<tr>";
                echo "<td>", $row['username'] ,"</td>";
            }

            if ($result2->num_rows > 0) {
                while($row2 = $result2->fetch_assoc()){
                echo "<td>", $row2['n'] ,"</td>";
                echo "</tr>";
                }
            }
        }

这就是它的显示

ZOMBOY
Hater
ZOMBOY2 3
1
1

这就是它需要成为的方式,但我找不到解决方法

and this is how it needs to become but i cant find a way to do it

ZOMBOY    3
Hater     1
ZOMBOY2   1

推荐答案

您可以使用join代替查询两个表

You can use join instead to querying two table

$active_ids = '1, 3, 4';

$query = "SELECT u.username, count(*) AS n FROM users u, timetable tt WHERE u.id=tt.dj and u.id IN ({$active_ids}) GROUP BY tt.dj";
$result = $mysqli->query($query);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()){
        echo "<tr>";
        echo "<td>", $row['username'] ,"</td>";
        echo "<td>", $row['n'] ,"</td>";
        echo "</tr>";
    }
}

这篇关于如何在获取asoc的同时制作表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆