0

我的 mysql 数据库中有更多数据。我想显示数据,每页 10 数据只需要显示,为此我编写了分页代码。它工作得很好,但我想自动运行该分页,这意味着几秒钟后页面会自动转到第二页,然后是第三页等......但我不知道如何实现请帮助任何人。下面是示例代码供参考:

    <?php
include "config.inc";

$sql = "SELECT COUNT(*) FROM test";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];


$rowsperpage = 3;

$totalpages = ceil($numrows / $rowsperpage);


if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {

$currentpage = (int) $_GET['currentpage'];
} else {

$currentpage = 1;
}


if ($currentpage > $totalpages) {

$currentpage = $totalpages;
} 
if ($currentpage < 1) {

$currentpage = 1;
} 


$offset = ($currentpage - 1) * $rowsperpage;


$sql = "SELECT * FROM test LIMIT $offset, $rowsperpage";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);


while ($list = mysql_fetch_array($result)) {

echo $list['mark_cut_weld'] . " : " . $list['mark_cut_inves'] . "<br />";
} 


$range = 3;


if ($currentpage > 1) {

echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";

$prevpage = $currentpage - 1;

echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} 


for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {

if (($x > 0) && ($x <= $totalpages)) {

if ($x == $currentpage) {

 echo " [<b>$x</b>] ";

} else {

 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} 
} 
} 


if ($currentpage != $totalpages) {

$nextpage = $currentpage + 1;

echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";

echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
} 
?>

上面的代码,我只是通过 php 从 mysql 数据库中获取数据。然后将每页的数据设置为 3。我只是得到总数,然后除以行数到每页的行数......然后它会自动显示数据。

我的目标是显示数据库中的数据。每页 10 个数据,然后自动移动到下一页以获取下一个 10 个数据,无需任何操作单击或提交...因为它是状态板程序..我们将在工厂的大电视上显示...所以工人可以看到这台大电视的工作状态。

4

2 回答 2

2

您可以设置标题重定向以重定向到下一页。

例如,以下代码将在 10 秒内将您重定向到下一页。

header('Refresh: 10; URL='.$_SERVER['PHP_SELF'].'?page='.$next_page);

确保在 PHP 中回显任何内容之前设置标题。

于 2013-03-15T07:29:32.603 回答
0

您将希望使用 javascript设置超时,以每隔一段时间将位置重定向到下一页。

例如,将其添加到 HTML 正文的底部:

<script type="text/javascript">
    function switchPage(){
        window.location = "<?php echo $next_page?>"; // set the next page of results to view.
    }

    setTimeout(switchPage,60*1000); // call callback every minute
</script>

该变量$next_page需要是使用 PHP 的下一组结果的 URL。

要让它重复,您需要在 PHP 端有一个模数,当结果结束时,它会翻转回第 0 页。

<?php
$next_page_count = ++$currentpage % $totalpages;
$next_page = $_SERVER['PHP_SELF'] . '?currentpage=' . $next_page_count;
于 2013-03-15T07:18:30.733 回答