我的表格正在发布,在该表格中,它显示了所有发布的帖子。 我正在尝试在帖子的底部制作页面导航链接。 函数generateenter code here_page_links完成所有工作。
代码似乎只显示“< - ”和“ - >”而不是链接号码。我认为该函数只是读取$ _GET ['posts'] == 1。
(此代码来自Oreilly PHP / MySQL书。我将其用作练习)
function generate_page_links($cur_page, $num_pages) {
$page_links = '';
// If this page is not the first page, generate the "previous" link
if ($cur_page > 1) { //cur_page is just a number that is gotten from the url.
$page_links .= '<a href="' . $_SERVER['PHP_SELF'] . '?page=' . ($cur_page - 1) . '"><--</a> ';
}
else {
$page_links .= '<- ';
}
// Loop through the pages generating the page number links
for ($i = 1; $i <= $num_pages; $i++) {
if ($cur_page == $i) {
$page_links .= ' ' . $i;
}
else {
$page_links .= ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $i . '"> ' . $i . '</a>';
}
}
// If this page is not the last page, generate the "next" link
if ($cur_page < $num_pages) {
$page_links .= ' <a href="' . $_SERVER['PHP_SELF'] . '?page=' . ($cur_page + 1) . '">-></a>';
}
if ($cur_page == $num_pages){ //the last page
$page_links .= ' ->';
}
return $page_links; //need to return this variable in the function
}
// Calculate pagination information
$cur_page = isset($_GET['page']) ? $_GET['page'] : 1;
$results_per_page = 3; // number of results per page
$skip = (($cur_page - 1) * $results_per_page);
$query = "SELECT * FROM posting ORDER BY date_added DESC";
$data = mysqli_query($dbc, $query);
$total = mysqli_num_rows($data);
$num_pages = ceil($total / $results_per_page);
//Query again to get just the subset of results
$query = $query . " LIMIT $skip, $results_per_page";
$result = mysqli_query($dbc, $query);
echo '<table>';
echo '<tr><td><b>Title</b></td><td><b>Date Posted</b></td></tr>';
while ($row = mysqli_fetch_array($result)) {
echo '<tr><td><a href="ad.php?
posting_id='.$row['posting_id'].'
">'.$row['title'].'</a></td>';
echo '<td>'.$row['date_added'].'</td>';
//echo '<td>'.$row['name'].'</td></tr>';
}
echo '</table>';
// Generate navigational page links if we have more than one page
if ($num_pages > 1) {
echo generate_page_links($user_search, $sort, $cur_page, $num_pages);
}
答案 0 :(得分:2)
你在这里遇到一些问题:
您使用4个变量调用该函数
echo generate_page_links($user_search, $sort, $cur_page, $num_pages);
但该功能只接受两个
function generate_page_links($cur_page, $num_pages)
如果那是你正在使用的代码,那么它可能会崩溃,因为这些变量是未定义的。
尝试删除“$ user_search,$ sort”,看看是否修复了它?