php - Php SQLITE 中的分页

标签 php html sql sqlite pagination

我对 PHP 还很陌生。我正在尝试连接到 sqlite 数据库,并且我已经成功完成了。由于某种原因,我的分页不起作用。

$page 的值不会超过 2。有人可以帮助我吗,我确信这可能是一个简单的错误。 (所以目前它确实从第一页更改为下一页。

A screenshot of the database (as it is currently)

    <?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();
  }
?>

最佳答案

所有功劳都归功于 Ryan-Vincent 帮助我解决了这个问题。

基本上,我一切正常,但表单操作的属性不正确。

这是我唯一改变的,它工作得很好(这是 html 中的打开表单标签,注意页面 url 参数从 php 页面变量获取其值。

  print "<form class=\"\" action=\"?page=$page \" method=\"POST\">";

希望这对其他 php 新手有帮助。

关于php - Php SQLITE 中的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39059865/

相关文章:

php - 从数组创建 SQL

javascript - PHP AJAX 脚本无法在所有条件下正常工作

php - 将日期转换为整数 PHP

html - 有测试网站吗?

javascript - 当 Div 内的宽度增加时,如何防止 Button 缩小高度?

sql - 安全递增 ID 所需的事务隔离级别

php - 在 Node 和 PHP 之间与 Redis 共享 session

php - 通过单击链接显示 mySQL 条目

javascript - 如何在不提交表单的情况下获取输入框的值并将计算出的值放入其他输入框?

sql - 自动提取数据 - Oracle SQL Developer