2015-12-30 65 views
0

我對編程相當陌生,並且本網站由朋友推薦給我。我爲我的廣告資源創建了一個表格,並且希望爲此表格添加分頁。我怎樣才能做到這一點?我感謝幫助,並提前致謝!如何使用PHP將分頁添加到我的表中

<?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> 
+0

什麼意思是「添加分頁」?對不起,我是意大利人,我不明白這一點:P但如果你解釋也許我可以幫你;) –

+0

我想限制一次可以顯示多少行20,然後其他人可以替換當前的行查看當我點擊下一個按鈕我不知道從哪裏開始,所以我要求幫助謝謝。 – SonicFreak

+0

接縫,您的問題已經在這裏得到解答: [http://stackoverflow.com/questions/19605078/how-to-use-pagination-on-html-tables](http://stackoverflow.com/questions/ 19605078/how-to-use-pagination-on-html -tables) –

回答

2

如果你想你的分頁表,你可以這樣做:

<?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> 

像這樣的東西應該工作...我有一個本地服務器,現在我很抱歉。

相關問題