在HTML表格中显示mysql数据 - 最新条目显示在@表格的顶部

时间:2022-09-25 16:22:00

I have a requirement to display mysql data in HTML Table -

我要求在HTML表格中显示mysql数据 -

But the latest entry that is inserted to mysql is to be shown @ top of html table

但插入到mysql的最新条目是显示在html表的顶部

I have some code, that is used to display mysql data in HTML table,but I find that the data is appended to the end of the table, each time it is executed.

我有一些代码,用于在HTML表中显示mysql数据,但我发现每次执行时都会将数据附加到表的末尾。

1 个解决方案

#1


1  

It depends how much data you are planning to show. I'll start with the most general technique, and probably the simplest - make sure your data includes a time added.

这取决于您计划显示的数据量。我将从最常用的技术开始,也许最简单 - 确保您的数据包含添加的时间。

Then your query is

然后你的查询是

select * from table_name order by time_added desc

Adapt it - table name, column names, what to select... Desc is for descending or you can use asc.

适应它 - 表名,列名,选择内容...描述是降序还是你可以使用asc。

To display, loop as usual, to only display the first few results, limit the query as usual.

要像往常一样循环显示,只显示前几个结果,像往常一样限制查询。

If for some reason you can't do that, the alternative is to set up the display in the reverse order. It isn't the best - but knowing you don't have to display the results as they come out is always good.

如果由于某种原因你无法做到这一点,另一种方法是按相反的顺序设置显示。这不是最好的 - 但是知道你不必显示结果,因为它们出来总是好的。

To do this, you don't change your query, the data comes out in reverse order, and you then loop through it but prepare the display backwards:

要做到这一点,你不要改变你的查询,数据以相反的顺序出现,然后你循环它,但向后准备显示:

$result = mysqli_query($con,"SELECT * FROM Persons");
$display = "";
while($row = mysqli_fetch_array($result))
{
    $display = $row['FirstName'] . " " . $row['LastName'] + "<br>" + $display;
}
echo $display;

As I said, I think it's not the best technique, but there's a good thing in this example: don't echo as you loop. Prepare a result, then echo it at the end. It gives you more flexible control over the display.

正如我所说,我认为这不是最好的技术,但在这个例子中有一件好事:不要在你循环时回音。准备一个结果,然后在结束时回显它。它使您可以更灵活地控制显示屏。

#1


1  

It depends how much data you are planning to show. I'll start with the most general technique, and probably the simplest - make sure your data includes a time added.

这取决于您计划显示的数据量。我将从最常用的技术开始,也许最简单 - 确保您的数据包含添加的时间。

Then your query is

然后你的查询是

select * from table_name order by time_added desc

Adapt it - table name, column names, what to select... Desc is for descending or you can use asc.

适应它 - 表名,列名,选择内容...描述是降序还是你可以使用asc。

To display, loop as usual, to only display the first few results, limit the query as usual.

要像往常一样循环显示,只显示前几个结果,像往常一样限制查询。

If for some reason you can't do that, the alternative is to set up the display in the reverse order. It isn't the best - but knowing you don't have to display the results as they come out is always good.

如果由于某种原因你无法做到这一点,另一种方法是按相反的顺序设置显示。这不是最好的 - 但是知道你不必显示结果,因为它们出来总是好的。

To do this, you don't change your query, the data comes out in reverse order, and you then loop through it but prepare the display backwards:

要做到这一点,你不要改变你的查询,数据以相反的顺序出现,然后你循环它,但向后准备显示:

$result = mysqli_query($con,"SELECT * FROM Persons");
$display = "";
while($row = mysqli_fetch_array($result))
{
    $display = $row['FirstName'] . " " . $row['LastName'] + "<br>" + $display;
}
echo $display;

As I said, I think it's not the best technique, but there's a good thing in this example: don't echo as you loop. Prepare a result, then echo it at the end. It gives you more flexible control over the display.

正如我所说,我认为这不是最好的技术,但在这个例子中有一件好事:不要在你循环时回音。准备一个结果,然后在结束时回显它。它使您可以更灵活地控制显示屏。