我使用数据库创建了一个图库。所有图像都随机显示在主页上。现在我需要向导航栏添加一个新的“最新”index.php/latest.php
类别。
我正在使用无限滚动和一个随机排序的小技巧$rand=date("i");
。
问题: ORDER BY 1 DESC
不适用于 index.php/latest.php
,因为我已在主页。如何在不更改主页顺序的情况下在最新页面上按降序排序?
index.php
<?php
include ("sqli.php");
$rand=date("i");
$seed=($rand);
$per_page = 9;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
$start_from = ($page-1) * $per_page;
$query = "SELECT * FROM data ORDER BY RAND($rand) LIMIT $start_from, $per_page";
$result = mysqli_query ($con, $query);
<?php while ($row = mysqli_fetch_array($result)) { ?>
?>
<div id="gallery here"></div>
<?php> } ?>
latest.php
<?php
include ("sqli.php");
$per_page = 9;
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
$start_from = ($page-1) * $per_page;
$query = "SELECT * FROM data ORDER BY 1 DESC LIMIT $start_from, $per_page";
$result = mysqli_query ($con, $query);
?>
<?php while ($row = mysqli_fetch_array($result)) { ?>
?>
<div id="gallery here"></div>
<?php> } ?>
最佳答案
您必须在last.php中通过createdAt字段(通过您的日期字段)设置顺序:
$query = "SELECT * FROM data ORDER BY createdAt DESC LIMIT $start_from, $per_page";
关于php - MySQL 与 RAND() 和 1 DESC 相关的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30005475/