2013-01-01 54 views
0

我决定在这里重新编写并详细说明,而不是再发布一次。此查询是否可以更改为执行两个功能?

我收到了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滑块或等价物来完成分页,但我真的想避免这种情况。

非常感谢!

+2

那么你卡在哪里? – hafichuk

+0

呃...就像如何使它工作。如何获取上面的代码并将52条记录拖到页面上。 – wordman

+0

最好的办法是在客户端做这些操作看看http://www.datatables.net/ – Moes

回答

1

到PHP代码(服务器端)内解决它,你可以其中x是页码添加URL参数?page=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,但无论如何您都可以这样做。

+0

你真棒,谢谢你!我通过使用子查询得到分页,昨晚从我的另一篇文章中获得了这些信息,但我今天肯定尝试了这一点。非常感谢! – wordman

+0

这是如此的轻松!作品绝对完美。我不知道该怎么感谢你才足够! – wordman

+0

为你感到高兴,它的工作原理。数据表的例子可能有点矫枉过正,但jquery/ajax分页解决方案也不错。但是服务器端解决方案往往是坚如磐石的,在某些情况下可能会更好。 – jtheman

0

我认为这是你要求..使用这个“功能”,你可以“切换”更多的选择,但我不认为你真的需要它..只需更改$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)... 

    ?> 
+0

Xfile,看起来好像它可能工作。我明天会看看并告诉你。非常感谢! – wordman

+0

当然会..享受;) – Xfile

+0

我看了这个,并决定详细阐述这个职位一些。更改'$ limit'不会在这里工作,因为它会影响滚动更新。非常感谢您的建议! – wordman