这似乎是一个简单的问题,但我似乎无法解决这个问题。下面,我有一个从表单返回的结果集。在大多数情况下,一切都按预期工作。最后一步是我想通过变量$ gbl_timestamp对结果进行排序,以便最新的条目是第一个。思考?
<?php
include($_SERVER["DOCUMENT_ROOT"]."/wrappers/code-s.wrp.php");
include($_SERVER["DOCUMENT_ROOT"]."/php/mobile-switch.php");
require_once("../tim/includes/db_connection.php"); ?>
<?php
// 2. Perform database query
$query = "SELECT * ";
$query .= "FROM assessment_info ";
$result = mysqli_query($connection, $query);
// Test if there was a query error
if (!$result) {
die("Database query failed.");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>Results</title>
</head>
<body>
<ol>
<?php
// 3. Use returned data (if any)
while($results = mysqli_fetch_assoc($result)) {
// output data from each row
?>
<li><?php print "<strong>Timestamp:</strong>" . $gbl_timestamp?>
<br />
<?php print "<strong>Department:</strong> ". $results["department"] . "
<br /><strong>Participant</strong>: " . $results["participant"] . " <br /> <strong>Activity Performed:</strong> " . $results["activity"] . "
<br /><strong>Location:</strong> " . $results["location"] . " <br /><strong>Average number of Injuries:</strong> " . $results["recordable_injuries"] . " in the " . $results["recordable_injuries_timeframe"] . ""; ?></li>
<?php
}
?>
</ol>
<?php
// 4. Release returned data
mysqli_free_result($result);
?>
</body>
</html>
<?php
// 5. Close database connection
mysqli_close($connection);
?>
答案 0 :(得分:0)
一种解决方案是对变量使用order by子句(如果它存在于表中。
)另一个解决方案是将每个{"num_pages": 13196, "num_results": 13196, "results": [{"is_automated": false, "name": "ubuntu", "is_trusted": false, "is_official": true, "star_count": 4681, "description": "Ubuntu is a Debian-based Linux operating system based on free software."}], "page_size": 1, "query": "ubuntu", "page": 1}
放在一个数组中,并将变量作为索引并对数组进行排序。
答案 1 :(得分:0)
这是解决方案。我在数据库中添加了一个新列,并使用phpMyAdmin中的内置时间戳字段。所以现在我需要添加到我的查询中:
$query = "SELECT * FROM assessment_info ORDER BY timestamp desc limit 0,1";