我对表格进行分页,以显示表格中数据库的数据。这工作正常,我试图添加一个只有管理员才能看到的按钮。如果用户不是管理员,他们将看不到此按钮。此功能有效,但一旦我这样做,分页只显示一行数据,而每页最多10行。
这是我的代码:
public function dataview($query)
{
$stmt = $this->db->prepare($query);
$stmt->execute();
if($stmt->rowCount()>0) // display records if there are records to display
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
$poll_id = $row['poll_id'];
$question = $row['question'];
?>
<tr>
<td><?php echo $row['poll_id']; ?></td>
<td><?php echo $row['question']; ?></td>
<td><a href="openPoll.php?poll_id=<?php echo $poll_id; ?>">Open Poll</a></td>
<td><a href="openResults.php?poll_id=<?php echo $poll_id; ?>">Results</a></td>
<?php
$stmt = $this->db->prepare("SELECT * FROM users WHERE user_id=:user_id");
$stmt->execute(array(':user_id'=>$_SESSION['user_session']));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() > 0){
$admin = $userRow['admin'];
if($admin == 1){
?>
<td><a href="deletePoll.php?poll_id=<?php echo $poll_id; ?>">Delete Poll</a></td>
<?php
?>
</tr>
<?php
}
}
}
}
else
{
?>
<tr>
<td>Nothing here...</td>
</tr>
<?php
}
}
这是html页面上使用分页
的代码 <table align="center" border="1" width="100%" height="100%" id="data">
<?php
$query = "SELECT * FROM polls";
$records_per_page=10;
$newquery = $paginate->paging($query,$records_per_page);
$paginate->dataview($newquery);
$paginate->paginglink($query,$records_per_page);
?>
</table>
答案 0 :(得分:1)
您在使用它的循环中重用$stmt
之类的变量。所以这样做:
$stmt = $this->db->prepare($query);
$stmt->execute();
if($stmt->rowCount()>0) // display records if there are records to display
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
$poll_id = $row['poll_id'];
$question = $row['question'];
?>
<tr>
<td><?php echo $row['poll_id']; ?></td>
<td><?php echo $row['question']; ?></td>
<td><a href="openPoll.php?poll_id=<?php echo $poll_id; ?>">Open Poll</a></td>
<td><a href="openResults.php?poll_id=<?php echo $poll_id; ?>">Results</a></td>
<?php
$stmt2 = $this->db->prepare("SELECT * FROM users WHERE user_id=:user_id");
$stmt2->execute(array(':user_id'=>$_SESSION['user_session']));
$userRow=$stmt2->fetch(PDO::FETCH_ASSOC);
if($stmt2->rowCount() > 0){
$admin = $userRow['admin'];
if($admin == 1){
?>
<td><a href="deletePoll.php?poll_id=<?php echo $poll_id; ?>">Delete Poll</a></td>
<?php
?>
</tr>
<?php
}
}
}
}
else
{
?>
<tr>
<td>Nothing here...</td>
</tr>
<?php
}
我已将$stmt
中的$stmt2
替换为{{1}}。