【发布时间】:2016-12-27 20:15:19
【问题描述】:
我对 PHP 很陌生。我正在尝试连接到sqlite 数据库,并且我已经成功地做到了。由于某种原因,我的分页不起作用。
$page 的值不会超过 2。谁能帮帮我,我敢肯定这可能是一个简单的错误。 (所以目前它确实从第一页变为下一页。
<?php
try
{
//open the database
$db = new PDO('sqlite:client.db');
//create the database
$db->exec("CREATE TABLE IF NOT EXISTS Client (id INTEGER PRIMARY KEY AUTOINCREMENT, first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(50), gender VARCHAR(50))");
$page = 1;
if(!empty($_GET['page'])) {
$page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT);
if(false === $page) {
$page = 1;
}
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//something posted
if (isset($_POST['Previous'])) {
print 'current value of $page = ' . $page;
print "<br>";
if($page <= 0) {
$page = 1;
}else {
$page = $page - 1;
}
} else if(isset($_POST['Next'])) {
print 'current value of $page = ' . $page;
print "<br>";
$page = $page + 1;
}
}
// set the number of items to display per page
$limit = 10;
// build query
$offset = ($page - 1) * $limit;
if($offset <= 0) {
$offset = 0;
}
print '$page = ' . $page;
print "<br>";
print '$offset = ' . $offset;
//now output the data to a simple html table...
print "<table border=1>";
print "<tr><td>Id</td><td>First Name</td><td>Last Name</td><td>Age</td><td>Gender</td></tr>";
$sql = "SELECT * FROM Client LIMIT " . $offset . "," . $limit;
$result = $db->query($sql);
//$rows = count($result);
//print $rows;
//checks if table has data
//$count = $result->fetchColumn();
foreach($result as $row)
{
print "<tr><td>".$row['id']."</td>";
print "<td>".$row['first_name']."</td>";
print "<td>".$row['last_name']."</td>";
print "<td>".$row['email']."</td>";
print "<td>".$row['gender']."</td></tr>";
}
print "</table>";
print "<br>";
//print "<button type=\"button\" name=\"button\"><< Previous </button>";
//print "<button type=\"button\" name=\"button\">Next >></button>";
print "<form class=\"\" action=\"\" method=\"post\">";
print "<button type=\"submit\" name=\"Previous\">Previous</button>";
print "<br><br><button type=\"submit\" name=\"Next\">Next</button>";
print "</form>";
// close the database connection
$db = NULL;
}
catch(PDOException $e)
{
print 'Exception : '.$e->getMessage();
}
?>
【问题讨论】:
-
您将
$page变量作为表单操作URL 的参数发送?它必须在上面,因为这是您下次发送表格时希望找到的地方。即$_GET['page]是通过 URL 查询参数设置的。 -
嗨瑞恩,感谢您的回复。我是PHP的新手,我不知道该怎么做。我刚刚开始,但有其他编程语言的经验,任何建议或帮助将不胜感激
-
<form class=\"\" action=\"\"?page=\"42\"将页码 42 传递给 PHP 脚本并使其显示为$_GET['page'];并等于 42。也许有趣?教程:html.net/tutorials/php/lesson10.php -
嗨 Ryan,谢谢,但页码取决于用户是单击下一个还是上一个,例如 [单击下一个 => page= page + 1] 或 [单击上一个 => page= page - 1] 并感谢教程链接:-)
-
我知道页码保存在 $page number 中。我期待你,,,没关系 :) 做:
'<form class="" action=?page="'. $page ,'"';或者类似的事情。
标签: php html sql sqlite pagination