0

有人可以帮我分页这个脚本吗?我对它不是很好,我一直在搞砸。

我已经尝试检查 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>";
       }
?>
4

1 回答 1

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 . '"> &lt;&lt;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&gt;&gt; </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;
  }
}
?>
于 2013-10-13T03:22:45.413 回答