我如何在获取 assoc 时制作表格

Posted

技术标签:

【中文标题】我如何在获取 assoc 时制作表格【英文标题】:How can i make a table while fetch assoc 【发布时间】:2015-10-28 10:35:41 【问题描述】:

您好,我是(开始)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

【问题讨论】:

您需要进行join 查询才能显示结果。 它被分组了,所以我不知道如何正确地做到这一点@Kamran 【参考方案1】:

您可以使用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>";
    

【讨论】:

【参考方案2】:

你可以这样做,但必须看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>';

【讨论】:

我说它是分组的,所以我不知道如何正确加入它 在页面源中有, 第二个问题是查询返回任何结果测试数组print_r ($columnOne);print_r ($columnTwo);它 echo 的 array() array()
【参考方案3】:

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

# 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>';

【讨论】:

以上是关于我如何在获取 assoc 时制作表格的主要内容,如果未能解决你的问题,请参考以下文章

如何制作表格视图的多个实例?

如何在 ngFor 循环 [Angular] 中制作可扩展的表格行?

如何在可扩展的表格视图单元中制作动态集合视图

如何制作动态表格

如何在wordpress中制作联系表格调用另一个php文件

如何制作 django 酥脆的表格来隐藏特定的字段?