有人可以帮我分页这个脚本吗?我对它并不是很好,我一直搞砸了。
我已经尝试检查num_rows,然后让它验证和填充但它失败了10次:( 2天后我放弃了。我想从头开始。
<?php
$link = mysqli_connect("localhost", "lunar_lunar", "", "lunar_users");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$result = mysqli_query($link, "SELECT * FROM users ORDER BY username");
while($row = mysqli_fetch_assoc($result)) {
$id=$row['id'];
$username=$row['username'];
$email=$row['email'];
$firstname=$row['firstname'];
$lastname=$row['lastname'];
$motto=$row['motto'];
$bio=$row['bio'];
$result4 = mysqli_query($link, "SELECT * FROM photo where id='$id'");
$row4 = mysqli_fetch_assoc($result4);
$image=$row4['filename'];
$src = (empty($image)) ? "upload/your-photo.jpg" : "site_images/$id/$image";
$motto = (empty($motto)) ? "No motto" : $motto;
$bio = (empty($bio)) ? "No biography" : $bio;
echo "<div class='panel panel-default'>
<div class='panel-heading'>
<h3 class='panel-title'><a href='public.php?id=".$id."'>".$username."</h3></a>
</div>
<div class='panel-body'>
<div class='gravatar span3' style='padding:0px;margin:0px;'>
<img src='
".$src."' alt='' width='85' height='85'>
</div>
<br />
<div class='page-header'>
<br />
</div>
<p style='margin-right:450px;'>
".$bio."
</p>
</div>
<div class='panel-footer'>".$motto."</div>
</div>";
}
?>
答案 0 :(得分:0)
你必须做一些调整才能合并你的sql查询,但是这里有一个空白的导航栏(希望这会指向你按照要求“正确的方向”):
<?php
print create_navbar(400, 10);
function create_navbar($count, $items_per_page = 50) {
// Creates a navigation bar
$current_page = $_SERVER["PHP_SELF"];
// You could always pull this next line out and re-add $start_number as the first argument in
// the function...
$start_number = ($_GET && isset($_GET['start']) && !empty($_GET['start'])) ? (int) $_GET['start'] : 0;
if (($start_number < 0) || (! is_numeric($start_number))) {
$start_number = 0;
}
$navbar = "";
$prev_navbar = "";
$next_navbar = "";
if ($count > $items_per_page) {
$nav_count = 0;
$page_count = 1;
$nav_passed = false;
while ($nav_count < $count) {
// Are we at the current page position?
if (($start_number <= $nav_count) && ($nav_passed != true)) {
$navbar .= '<b><a href="' . $current_page . '?start=' . $nav_count . '">[' . $page_count . '] </a></b>';
$nav_passed = true;
// Do we need a "prev" button?
if ($start_number != 0) {
$prevnumber = $nav_count - $items_per_page;
if ($prevnumber < 1) {
$prevnumber = 0;
}
$prev_navbar = '<a href="' . $current_page . '?start=' . $prevnumber . '"> <<Prev - </a>';
}
$nextnumber = $items_per_page + $nav_count;
// Do we need a "next" button?
if ($nextnumber < $count) {
$next_navbar = '<a href="' . $current_page . '?start=' . $nextnumber . '"> - Next>> </a><br>';
}
}
else {
// Print normally.
$navbar .= '<a href="' . $current_page . '?start=' . $nav_count . '">[' . $page_count . '] </a>';
}
$nav_count += $items_per_page;
$page_count++;
}
$navbar = $prev_navbar . $navbar . $next_navbar;
return $navbar;
}
}
?>