【问题标题】:How to Add Pagination to my Table with PHP [duplicate]如何使用 PHP 向我的表格添加分页 [重复]
【发布时间】:2016-04-04 08:49:54
【问题描述】:

我对编程很陌生,这个网站是朋友推荐给我的。我为我的库存创建了一个表,我想在这个表中添加分页。我怎样才能做到这一点?感谢您的帮助,并提前致谢!

<?php include ("Connection.php"); ?>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="Design.css">

    <script type=" text/javascript">
    function deleteRecord(id) {
      if (confirm("Are you sure you want to delete this record?")) {
        window.location.replace("delete.php?id=" +id);
      } 
    } 
    </script>
  </head>

  <body>

    <?php
    $qry = $con->prepare("SELECT * FROM tableinvplastic");
    $qry->execute();
    $rows = $qry->fetchAll(PDO::FETCH_ASSOC);
    ?>

    <table>
      <th><center>ID</center></th>
      <th><center>Item Name</center></th>
      <th><center>Brand</center></th>
      <th><center>Initial Price</center></th>
      <th><center>Sales Price</center></th>
      <th><center>Quantity</center></th>
      <th><center>Dealer</center></th>
      <th><center>Edit</center></th>
      <th><center>Delete</center></th>
      <th><center>Add</center></th>
      <th><center>Minus</center></th>

      <?php
      foreach($rows as $row) {
        print "<tr>";
        print "<td>" .$row['ID']. "</td>";
        print "<td>" .$row['ItemName']. "</td>";
        print "<td>" .$row['Brand']. "</td>";
        print "<td>" .$row['InitialPrice']. "</td>";
        print "<td>" .$row['SalesPrice']. "</td>";
        print "<td>" .$row["Quantity"]. "</td>";
        print "<td>" .$row["Dealer"]. "</td>";
        print "<td><a href='UpdatePlastic.php?id=" .$row["ID"]. "'>Edit</a></td>";
        print "<td><a href='#!' onclick = 'deleteRecord(" .$row["ID"]. ");'>Delete</a></td>";
        print "<td><a href='Add.php?id=" .$row["ID"]. "'>Add</a></td>";
        print "<td><a href='Minus.php?id=" .$row["ID"]. "'>Minus</a></td>";
        print "</tr>";
      }
      ?>
    </table>

    <style>
    table {
      width: 60%;
      position: absolute;
      top: 200px;
      left: 100px;
    }

    table, th, td {
      border: 1px solid black;
      border-collapse: collapse;
    }

    th {
      padding: 5px;
      text-align: left;
      background-color: #4CAF50;
    }
    </style>
  </body>
</html> 

【问题讨论】:

  • “添加分页”是什么意思?对不起,我是意大利人,我不明白:P 但如果你解释一下,也许我可以帮助你;)
  • 我想将一次可以显示的行数限制为 20,然后当我单击下一个按钮时,其他行将替换当前视图中的行我不知道从哪里开始,所以我寻求帮助谢谢。
  • 这是最简单的方法。取 2 $var - 限制和偏移,初始化它们并从 URL 中读取它们。单击下一个或上一个按钮时,将值传递给 URL,页面将重新加载,从 url 读取限制和偏移量并运行新查询以获取更新的数据。您的查询应该类似于SELECT * FROM table LIMIT $offset,$limit

标签: php pagination


【解决方案1】:

如果你想为你的表格分页,你可以这样做:

<?php
 include ("Connection.php");
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="Design.css">

<script type=" text/javascript">
function deleteRecord(id) {
if(confirm("Are you sure you want to delete this record?")) {
window.location.replace("delete.php?id=" +id);
} 
} 
</script>

</head>
<body>
<table>
<th><center>ID</center> </th>
<th><center>Item Name</center> </th>
<th><center>Brand</center> </th>
<th><center>Initial Price</center> </th>
<th><center>Sales Price</center> </th>
<th><center>Quantity</center> </th>
<th><center>Dealer</center> </th>
<th><center>Edit</center> </th>
<th><center>Delete</center> </th>
<th><center>Add</center> </th>
<th><center>Minus</center> </th>
  <?php 
  $query = "SELECT * FROM tableinvplastic ORDER BY id DESC LIMIT 1";
  $stmt = $con->query($query);
  $data = $stmt->fetch(PDO::FETCH_ASSOC);
  $num_rows = $data['id'] / 20;
<?php
function get_result(index,initial,limit)
{
   $qry = $con->prepare("SELECT * FROM tableinvplastic LIMIT $initial,$limit");
   $qry -> execute();
   if($qry->rowCount() > 0)
   {
      return $rows;
   }
else return  'empty';
}
?>
<?php
     for($i=0;$i<$num_rows;$i++)
{
 if($i = 0) 
{
$limit = 0;
$initial = 20;
}
else 
{
$initial = ($i*20+1)
limit = $i*20*2);
}
$rows = get_result($i,$initial,$limit);
print "<tr>";
    print "<td>" .$row['ID']. "</td>";
    print "<td>" .$row['ItemName']. "</td>";
    print "<td>" .$row['Brand']. "</td>";
    print "<td>" .$row['InitialPrice']. "</td>";
    print "<td>" .$row['SalesPrice']. "</td>";
    print "<td>" .$row["Quantity"]. "</td>";
    print "<td>" .$row["Dealer"]. "</td>";
    print "<td><a href='UpdatePlastic.php?id=" .$row["ID"]. "'>Edit</a></td>";
    print "<td><a href='#!' onclick = 'deleteRecord(" .$row["ID"]. ");'>Delete</a></td>";
    print "<td><a href='Add.php?id=" .$row["ID"]. "'>Add</a></td>";
    print "<td><a href='Minus.php?id=" .$row["ID"]. "'>Minus</a></td>";
    print "</tr>";
}

</table>

<style>
table {
width:60%;
position : absolute;
top : 200px;
left : 100px;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th {
padding: 5px;
text-align: left;
background-color: #4CAF50;
}
</style>
</body>
</html>

这样的东西应该可以工作...对不起,我现在没有本地服务器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-07
    • 2019-07-22
    • 2016-12-31
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多