显示MySQL会导致一个表

时间:2022-05-11 08:04:10

I created an interface in which the user can choose which results are going to be displayed. The options selected by the user are saved in an array, so the MySQL query goes through the different elements of this array.

我创建了一个界面,用户可以在其中选择将要显示的结果。用户选择的选项被保存在一个数组中,因此MySQL查询遍历这个数组的不同元素。

So I want to display the results in a single table... something like this:

所以我想把结果显示在一个表中……是这样的:

Header 1 | Header 2

标题1 |标题2

Result 1-1 | Result 1-2

结果1-1 |结果1-2

Result 2-1 | Result 2-2

2-1 |结果2-2

And my code is the following:

我的代码如下:

$mark = $_POST['mark'];
if (isset($_POST['mark']) && is_array($_POST['mark'])) {
    echo "<table border='1'>";
    echo "<tr>";
    for ($i = 0; $i < count($mark); $i++) {
            echo "<th>" . $mark[$i] . "</th>";
    }
    echo "</tr>";

    for ($i = 0; $i < count($mark); $i++) {
        $markQuery = "SELECT $mark[$i] FROM marks LIMIT 10";
        $result = mysqli_query($DB_connection, $markQuery);
        echo "<tr>";
        while($row= mysqli_fetch_assoc($result)){
            echo "<td>" .  $row[$mark[$i]] . "</td>";
            }
        echo "</tr>";
        }
        echo "</table>";
}

I'm not sure about if the second loop is a good idea, but I don't know what should I do to display results as I want.

我不确定第二个循环是否是一个好主意,但是我不知道我应该做什么来显示我想要的结果。

1 个解决方案

#1


0  

Try getting all the columns out first, then add it to the sql query, no need to loop the database query.

尝试先将所有列取出,然后将其添加到sql查询中,不需要循环数据库查询。

$mark = $_POST['mark'];
if (isset($_POST['mark']) && is_array($_POST['mark'])) {
    echo "<table border='1'>";
    echo "<tr>";
    for ($i = 0; $i < count($mark); $i++) {
            echo "<th>" . $mark[$i] . "</th>";
    }
    echo "</tr>";

    $sql = implode(',', $_POST['marks']);

    $markQuery = "SELECT ".$sql." FROM marks LIMIT 10";
    $result = mysqli_query($DB_connection, $markQuery);
    echo "<tr>";
    while($row= mysqli_fetch_assoc($result)){
        echo "<td>" .  $row[$mark[$i]] . "</td>";
    }
    echo "</tr>";
    echo "</table>";
}

#1


0  

Try getting all the columns out first, then add it to the sql query, no need to loop the database query.

尝试先将所有列取出,然后将其添加到sql查询中,不需要循环数据库查询。

$mark = $_POST['mark'];
if (isset($_POST['mark']) && is_array($_POST['mark'])) {
    echo "<table border='1'>";
    echo "<tr>";
    for ($i = 0; $i < count($mark); $i++) {
            echo "<th>" . $mark[$i] . "</th>";
    }
    echo "</tr>";

    $sql = implode(',', $_POST['marks']);

    $markQuery = "SELECT ".$sql." FROM marks LIMIT 10";
    $result = mysqli_query($DB_connection, $markQuery);
    echo "<tr>";
    while($row= mysqli_fetch_assoc($result)){
        echo "<td>" .  $row[$mark[$i]] . "</td>";
    }
    echo "</tr>";
    echo "</table>";
}