【问题标题】:Can this query be altered to perform two functions?可以更改此查询以执行两个功能吗?
【发布时间】:2012-12-15 23:43:29
【问题描述】:

我决定在这里重新编写和详细说明,而不是再次发布。

我从 jtheman 收到了以下代码,它允许我从我的数据库中选择 52 个更新,并每周添加一个新的并删除旧的。它绝对完美。

他的代码:

$starttime = strtotime("28 December 2012"); // a recent Friday
  $week = 7 * 24 * 60 * 60; // time value of a week
  $posts = 185; // number of posts in your db
  $limit = 52; // number of shown posts
  $offset = floor((time()-$starttime)/$week); // rounds down difference in weeks from startdate until now
  while ($offset>$posts-$limit) $offset = $offset - ($posts-$limit); 
  // this will start over when you have reached the end of the cycle ie offset 148...

我的查询:

// retrieve all update details
$conn = dbConnect('query');
$sql = "SELECT *
FROM updates
WHERE flag_live = 'Y'
ORDER BY update_id DESC
LIMIT ".$offset.",".$limit;
$result = $conn->query($sql) or die(mysqli_error());
$uInfo = $result->fetch_assoc();

同样,这非常有效。新问题是我在一页上获得 52 个更新,我希望能够设置,例如每页 4 个,并滚动浏览 13 页而不是一页长。

所以,我想要做的是更改此查询,以便选择 52 个更新(并且每周五添加一个新更新并删除一个旧更新),但我只能显示一次,例如 4 个在页面上。我意识到这不是一个分页问题,​​而是编写查询基本上执行两个功能。

这可以通过子查询来完成吗?我可以只使用 jQuery 滑块或等效项来进行分页,但我真的想避免这种情况。

非常感谢!

【问题讨论】:

  • 呃...比如如何让它工作。如何获取上面的代码并提取 52 条记录,然后翻阅它们。
  • 最好的方法是在客户端进行这些操作看看datatables.net
  • 尽管我讨厌不得不使用jQuery,但这看起来确实很有希望。但是我不能让显示看起来像您引用的页面上的示例表。我有文字、照片和链接。

标签: php mysql database pagination


【解决方案1】:

要在 PHP 代码中解决它(在服务器端),您可以添加一个 url 参数 ?page=x 其中 x 是您的页码。

在计算中几乎相同:

$starttime = strtotime("28 December 2012"); // a recent Friday
$week = 7 * 24 * 60 * 60; // time value of a week
$posts = 185; // number of posts in your db
$totallimit = 52; // number of shown posts (on all pages)
$limit = 4; // number of posts on each page.
$offset = floor((time()-$starttime)/$week); // rounds down difference in weeks from startdate until now
while ($offset>$posts-$totallimit) $offset = $offset - ($posts-$totallimit); 
// this will start over when you have reached the end of the cycle ie offset 148...

// get the page number (or set it to 0 if not set)
if (isset($_GET['page']) && intval($_GET['page'])) $page=intval($_GET['page']);
else $page = 0;

$offset = $offset + ($page*$limit); // correct the offset according to the page number

然后使用您的数据库查询而不做任何更改。

然后在您的视图中添加页面链接(如果之前执行上述代码,则此代码有效):

<?php for($p=0;$p<ceil($totallimit/$limit);$p++): ?>
   <a href="mypage.php?page=<?php echo $p; ?>" <?php if ($p==$page) echo 'class="active"'; ?>>Page <?php echo $p+1; ?></a> |
<?php endfor; ?>

(将mypage.php 替换为您的脚本的正确文件名)

我为当前选定页面的页面锚点 active 添加了一个类,但您可以随意执行此操作。

【讨论】:

  • 你太棒了,谢谢你!我通过使用子查询进行了分页,昨晚从我的另一个帖子中获得了该信息,但我今天肯定会尝试这个。非常感谢!
  • 太流畅了!工作绝对完美。我不能感谢你!
  • 很高兴它的工作原理。在您的情况下,数据表示例可能有点矫枉过正,但用于分页的 jquery/ajax 解决方案也很好。但服务器端解决方案往往坚如磐石,在某些情况下可能会很好。
  • 我非常渴望学习 ajax...只是需要时间。再次感谢你!
【解决方案2】:

我认为这就是您的要求.. 使用此“功能”您可以“切换”更多选项,但我认为您不会真的需要它.. 只需更改 $limit= 数字以满足您的需求。

<?php

  $starttime = strtotime("28 December 2012"); // a recent Friday
  $week = 7 * 24 * 60 * 60; // time value of a week
  $posts = 200; // number of posts in your db
  //$limit = 52; // number of shown posts

if($posts > 100) // or you can enter here $posts == 200, but it is better 2 leave it on "automatic"..
{ 
$limit = 52; //or as many you like like 30
} 
else
{ 
$limit = 6; // or as many you like ie 10 
}

  $offset = floor((time()-$starttime)/$week); // rounds down difference in weeks from     startdate until now
  while ($offset>$posts-$limit) $offset = $offset - ($posts-$limit); 
  // this will make the cycle start over when you have reached the end (ie offset 148)...

  ?>

【讨论】:

  • Xfile,看起来好像可以工作。我明天去看看,然后告诉你。非常感谢!
  • 我看了一遍,决定再详细说明这篇文章。更改 $limit 在这里不起作用,因为它会影响滚动 52 更新。非常感谢您的建议!
  • 是的wordman,这个限制的改变会打破原来的52个帖子的循环......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-24
  • 2012-06-27
  • 1970-01-01
  • 2019-06-10
  • 1970-01-01
  • 1970-01-01
  • 2013-05-18
相关资源
最近更新 更多