I'm new to php and I wonder about the best way to retrieve mysql data into html table. Currently I can't provide a well formated code because I'm on phone but let's say the case was like this:
I had mysql table with this structure:
Row -> PK
Column -> PK
Value
I had around 10 Column and a dinamic Rows which need to be displayed in webpage.
My idea is either to loop a small query and echo it, or store it in a big array and scan it rapidly.
First idea:
For ($y = 0; $y < $rowCount; $y++){
Echo "<tr>";
For ($x = 0; $x < 10; x++){
$row = Mysqli_query($db,"SELECT Value FROM table WHERE Row='".$y."' AND Column='".$x."'");
$row = mysqli_fetch_assoc($row);
Echo "<td>".$row['Value']."<td>";
}
Echo "</tr>";
}
Second idea:
$data = Mysqli_query($db,"SELECT * from Table");
For ($y = 0; $y < $rowCount; $y++){
Echo "<tr>";
For ($x = 0; $x < 10; $x++){
While($key = mysqli_fetch_assoc($data)){
If($key['Row']==$x && $key['Column']==$y)
Echo "<td>".$row['Value']."<td>";
}
}
Echo "</tr>";
}
Sorry if I had wrong syntax because I'm typing it without my syntax cheatsheet and my point is to tell the technique, not asking to debug.
The first Idea are using mysqli query 'WHERE' to look for both primary (Row and Column). Then spam the query in a loop.
The second idea is to store all table result in variable ($data), then keep scanning the variable with While loop.
Note that the the number of Rows is dinamic and could add up to 500 data. That's also the reason why I consider spamming query instead of store everything in variable ($data). But I also wonder if spamming query isn't the best idea because the program would have to keep making connection.
I hope someone could come up with the third idea.
In the real case, my table are more complicated than this and consist around 5 primary key. But the concept are almost equal to the example above.
Outside the loop, execute a SELECT
with an ORDER BY
. Have the loop fetch the rows. Note that it will fetch them in the order already specified, so you don't need that messy if
.