如何在获取关联时创建表

时间:2015-10-28 10:35:41

标签: php mysql sql mysqli

你好我(开始)php后端开发,我正在开发一个dj面板,但它没有正常工作,我尝试了尽可能多的东西,但我不能让它工作..

    $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

这就是它需要变成的方式,但我找不到办法

ZOMBOY    3
Hater     1
ZOMBOY2   1

3 个答案:

答案 0 :(得分:2)

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

$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>";
    }
}

答案 1 :(得分:1)

您可以这样做,但必须看Joins

$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);
$columnOne = Array();
$columnTwo = Array();
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()){
        $columnOne[]= $row['username'];
    }

    if ($result2->num_rows > 0) {
        while($row2 = $result2->fetch_assoc()){
            $columnTwo[] = row2['n'];
        }
    }
}
echo '<table>';
for($i=0;$i<count($columnOne);$i++){
    echo '<tr><td>' . $columnOne[$i] . '</td><td>' . $columnTwo[$i] . '</td></tr>';
}
echo '</table>';

答案 2 :(得分:1)

你可以尝试这样的东西(不像其他人那么优雅):

# Escape your characters
$active_ids = "'1', '3', '4'";

# Tidy up the querys to reduce the change of reserved words being used
$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);

# Count your results
$c1 = count($result); 
$c2 = count($result2);
#Set the counter to be the larger of the 2
$counter = (($c1 > $c2) ? $c1 : $c2);
if ($result->num_rows > 0 && $result2->num_rows > 0)
{
    # Print the table opener
    print '<table class="your_class">';
    # Loop through your results
    for ($i = 0; $i < $counter; $i++)
    {
        # Print the data needed
        print '<tr><td>' . $result[$i]['username'] . '</td><td>' . $result2[$i]['n'] . '</td></tr>';
    }
    # End the table
    print '</table>';
}