Display MySQL results in a single table

I created an interface in which the user can choose which results will be displayed. The parameters selected by the user are stored in the array, so the MySQL query goes through the various elements of this array.

So, I want to display the results in one table ... something like this:

Header 1 | Heading 2

Result 1-1 | Result 1-2

Result 2-1 | Result 2-2

And my code is as follows:

$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 am not sure that the second cycle is a good idea, but I do not know what to do to display the results as I want.

+6
source share
1 answer

First try to get all the columns, and then add it to the sql , no need to loop the database query.

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

Source: https://habr.com/ru/post/989604/


All Articles