我实际上正在为某个人提出问题的答案,直到我遇到一些奇怪的事情。问题是关于编写分页系统。用户想要在他当前的系统中添加额外的东西。所以我写了下面的代码:
<?php
// Database Settings
$dbhost = 'localhost';
$dbuser = '';
$dbpass = '';
$dbname = '';
// Establish Connection to the Database
$dbh = new PDO('mysql:host='. $dbhost .';dbname='. $dbname, $dbuser, $dbpass, array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
// Selecting the data from table but with limit
$query = 'SELECT * FROM table_name ORDER BY column_name ASC LIMIT :start, :page';
// Prepare query
$pre = $dbh->prepare($query);
// Binding values
$pre->bindParam(':start', $start_from);
$pre->bindParam(':page', $per_page);
// Results per page
$per_page=2;
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page=1;
}
// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;
?>
<!-- Start building HTML table -->
<table>
<?php
// Execute query
try {
$pre->execute();
// Fetch all results
$results = $pre->fetchAll(PDO::FETCH_ASSOC);
// Loop through results
foreach($results as $data){
// Display results in HTML table
echo "<tr>";
// Add/Remove your column names here
echo "<td>". $data['column_name'] ."</td>";
echo "<td>". $data['column_name'] ."</td>";
echo "<td>". $data['column_name'] ."</td>";
// Close HTML table row
echo "</tr>";
}
} catch (PDOException $e) {
echo 'MySQL query error: ' . $e->getMessage();
}
?>
<!-- End building HTML table -->
</table>
<div>
<?php
// Now select all data from table
$query = 'SELECT * FROM users';
// Prepare the query
$pre = $dbh->prepare($query);
// Execute the query
try {
$pre->execute();
// Count the results
$total_records = $pre->rowCount();
// Keep a record of total number of rows
$total_rows = $total_records;
// Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
// Going to first page
echo "<center><a href='pagination.php?page=1'>First Page</a> ";
// Showing number of pages in between last page
for ($i=1; $i<=$total_pages; $i++){
echo "<a href='pagination.php?page=". $i ."'>". $i ."</a> ";
}
// Going to last page
echo "<a href='pagination.php?page=". $total_pages .">Last Page</a></center> ";
} catch (PDOException $e) {
echo 'MySQL query error: ' . $e->getMessage();
}
// Calculate first and last item on current page
$first = $page * $per_page - $per_page;
$last = $page * $per_page;
// Showing the results
echo "<br />";
echo "Showing ". $first ." to ". $last ." in total record of ". $total_rows;
?>
</div>
代码没有返回错误,似乎显示数据就好了。但是,从第112行(// Going to last page
下面)回显的所有内容都不会显示在页面上。它确实在HTML源代码中显示正确的值。但它在Firefox和Chrome中都完全被标记为红色。
我以前从未遇到过这种情况而且我在徘徊是什么导致了这一点?
答案 0 :(得分:2)
这类问题通常意味着您的HTML格式不正确。
看看那一行:
echo "<a href='pagination.php?page=". $total_pages .">Last Page</a></center> ";
在>Last Page
;)
应该是
echo "<a href='pagination.php?page=". $total_pages ."'>Last Page</a></center> ";