0

My website (thanks to help from many of you here on SO) is set up to display a fixed number of updates (52) where each Friday, one rotates out, and a new one rotates in. The 52 updates are displayed 4 per page across 13 pages. It works perfectly.

However, the new update appears at the END of the displayed updates. I would like the new update to be the first one people will see.

Here is my query:

$conn = dbConnect('query');
$updates = "SELECT update_id, update_title, update_desc, path
    FROM updates
    WHERE flag_live = ?
    ORDER BY update_id DESC
    LIMIT ?, ?";
$stmt = $conn->prepare($updates);
$stmt->bind_param('sii', $uLive, $offset, $limit);
$stmt->bind_result($uId, $uTitle, $uDesc, $uPath);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();

I want the items to display in reverse order as they currently do. If I change the ORDER BY from DESC to ASC it picks a different set of data which I do not want. Thus, from my research I have seen that either array_reverse() or rsort would do the trick. I need to know what syntax is correct to use array_reverse() properly here.

4

3 回答 3

0

我和 PHP 相处得不太好,但是像这样?

$results = $stmt->fetchAll();
$results = array_reverse( $results );
foreach ($results as $row)
{
  /* $row will contain each row in the reversed array one at a time. do your usual process-one-row logic here */
}

顺便说一句,当你不知道调用 PHP 函数时返回什么时,有一个非常有用的调试函数 var_dump。你可以在任何东西上调用它,它会递归地打印它的组成部分。

http://au1.php.net/manual/en/function.var-dump.php

于 2013-01-24T23:45:57.063 回答
0

也许尝试这样的事情:

$results = $stmt->fetchAll();
$results = array_reverse( $results );

下一部分可以通过几种不同的方式完成,因此这实际上取决于您将如何使用结果并对它们进行分页。

您可以使用for循环:http
://us3.php.net/manual/en/control-structures.for.php 或foreachhttp ://us3.php.net/manual/en/control-structures.foreach.php

于 2013-01-24T22:54:08.593 回答
0

好的,经过大量的头撞后,我能够完成这项任务,尽管不是以我发布这个问题时考虑的方式。我最终做的是编写一个子查询,它允许我颠倒每个页面上显示的更新的顺序,正如你所知,这只会颠倒页面上可见的四个更新的顺序,而不是整体顺序选择的 52 个(见原始问题)。

下一步是“反转”我的分页系统......这不是一项小任务......重写逻辑,测试代码等。因此,通过使用子查询反转事物并反转我的分页,我实现了我的目标。

你所有的建议她都对最终结果有所帮助,尽管不是以预期的方式。如果这不像回答我自己的问题那样简洁,我提前道歉。

于 2013-01-25T05:09:34.450 回答