-1

我已经尝试了一些事情,但似乎无法正确实现分页,有人可以指出我正确的方向并给我一些代码示例,说明如何在我的代码中正确实现分页

<?php
session_start();

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<link rel="stylesheet" type="text/css" href="stylesheet.css">
<link href="/lightbox/css/lightbox.css" rel="stylesheet" />
<link rel="stylesheet" href="img/reveal.css">


<script src="/lightbox/js/jquery-1.7.2.min.js"></script>
<script src="/lightbox/js/lightbox.js"></script>
<script src="img/jquery.min.js" type="text/javascript"></script>
<script src="img/jquery.reveal.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"     type="text/javascript"></script>

<style type="text/css">

#divspace {
position: relative;
width: 100%;
height: 50px;
z-index: -9999;
vertical-align bottom;


 }

 #divspace1 {
position: relative;
width: 100%;
height: 50px;
z-index: -9999;
vertical-align: bottom;
padding-left: 400px;


}


#search1 {
position: absolute;
width: 203px;
height: 30px;
z-index: -9999;
vertical-align: bottom;
left: 832px;
top: 1px;
z-index: 9999;
}

 #text1 {
position: absolute;
width: 203px;
height: 30px;
z-index: -9999;
vertical-align: bottom;
top: 13px;
z-index: 9999;
left: 268px;
font-family: Tahoma, Geneva, sans-serif;
font-weight: bold;
color: #FFF;
font-style: italic;
font-size: 22px;
 }

 #apDiv1 {
position:absolute;
width:560px;
height:27px;
z-index:1;
left: 93px;
top: 247px;
}

#cat {
position:absolute;
width:227px;
height:500px;
z-index:1;
left: 3px;
top: 48px;
background-color: #FFF;
font-family:"Lucida Console", Monaco, monospace;
 }


 .container1 {
position:relative;
padding: 0 20px 0 20px;
margin: auto;
width: 1000px;
background-image: url(images/skyblue.png);
margin-top: 20px;
margin-bottom: 20px;
padding-bottom: 300px;
-moz-border-radius: 8px;
border-radius: 8px;

 }


 .label{
text-align:right;
  }
  #submit{
text-align:center;
   }

</style>
<script type = "text/javascript">

function myfunction(url)
  {
 window.location.href = url;
 }
</script>

<script type="text/javascript">
$(document).ready(function(){    
$(".expanderHead").click(function(){
    var $exsign = $("#expanderSign");
    $(this).find("#expanderContent").slideToggle();
    $exsign.html($exsign.text() == '+' ? '-': '+');   
    // simplify your if/else into one line using ternary operator
    // if  $exsign.text() == "+" then use "-" else "+"
 });    
});
</script>



</head>
<body>
<div id="header">
  <div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=439699742746900";
  fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));</script>


<div id="search">
  <center>
<form method="GET" action="search.php" style= "padding: 1px;">
<input name="search" id="s" type="text" value="<?php echo $_GET['search']; ?>" size="20" />

<select name="category" id="category" >
                <?php if(isset($_GET['submit'])) { ?>
                <option value="<?php echo $_GET['category']; ?>" selected="selected"><?php echo $_GET['category']; ?></option>
                <?php }else{ ?>
                <option value=""> -- select -- </option>
                <?php } ?>
                <option value="">All</option>
                <option value="Books">Books</option>
                <option value="Textbooks">TextBooks</option>
                <option value="Tickets">Tickets</option>
                <option value="Electronics">Electronics</option>
                <option value="Clothing">Clothing</option>
                <option value="Accessories">Accessories</option>
                <option value="Furniture">Furniture</option>
                <option value="Imagery">Imagery</option>
                <option value="Business">Business</option>
                <option value="Clothing">Clothing</option>
                <option value="Multi">Multimedia</option>
    </select>

    <select name="university" id="university" >
                <option value="">Aston University</option>
    </select>       
    <input id="searchSubmit" type="submit" value="" name="submit"/>

  </form>
  </center>
</div>


<div id="imagelogo" onclick = "window.location.href = 'index.php'" > 

  <p> Buy and sell stuff around University</p>
</div>

<ul id="navigation" name="navigation">
  <li id="nav-home"><a href="index.php">Home | </a><a href="search.php">Search | </a><a href="#">Selling | </a><a href="#">Buying | </a><a href="#">FAQ | </a><a href="#">Contact</a>    </li>
</ul>

<div id="account">

  <?php
if( isset( $_SESSION['username'] ) ){

   echo "<a href='securedpage1.php'>My Account</a><img src='images/uni-icon.png' width='30' height='18' style='vertical-align: middle;'/>";

}else{

      echo "<a href='login.php' >Login</a><img src='images/uni-icon.png' width='30' height='18' style='vertical-align: middle;'/>";
}
        ?>

    </div>

    <div id="registerlogout">
 <?php
 if( isset( $_SESSION['username'] ) ){
   echo "<a href='logout.php'>Logout</a>";

}else{

  echo "<a href='register.php'> Register</a>";
    }
        ?>
</div>






<center>
<center>

</div>
<div class="container1">
<div id="cat">
  <table width="225" border="1" cellspacing="10" cellpadding="10">
<tr>
<td>Accessories</td>
  </tr>
  <tr>
    <td>Accommodation</td>
  </tr>
  <tr>
        <td>Books</td>
  </tr>
  <tr>
    <td>Business</td>
  </tr>
  <tr>
    <td>Clothing</td>
  </tr>
  <tr>
    <td>Electronics</td>
  </tr>
  <tr>
    <td>Furniture</td>
  </tr>
  <tr>
        <td>Imagery</td>
  </tr>
  <tr>
    <td>Multimedia</td>
  </tr>
  <tr>
<td>Services</td>
      </tr>
<tr>
<td>Tickets</td>
  </tr>
</table>
</div>

<div id="text1">Items For Sale:

</div>

<div id="search1">
<form method="GET" action="search.php" style= "padding: 1px;">
<select name="price" id="price" >
                <?php if(isset($_GET['submit'])) { ?>
                <option value="<?php echo $_GET['price']; ?>" selected="selected"><?php echo $_GET['price']; ?></option>
                <?php }else{ ?>
                <option value=""> -- select -- </option>
                <?php } ?>
                <option value=""></option>
                <option value="DESC">Highest to Lowest</option>
                <option value="ASC">Lowest to Highest</option>
    </select>
     <input id="searchSubmit" type="submit" value="" name="submit"/>
</form>
    </div>

<div id="divspace"></div>
<div style=" padding-left: 240px" ">


<?php

// Include database connection settings
include('config.php');
include('config.inc');



    // Check and set username
$username = (isset($_SESSION['username']) ? $_SESSION['username'] : 'guest');

// Check and set category
    $category = (!empty($_GET['category']) ? $_GET['category'] : null);

// Check and set search
if(!empty($_GET['search'])){
$search = $_GET['search'];
}else{
$search = null;
}

 // Check that $_GET['price'] is ASC if not set to DESC
// as static values its ok to directly put in the query 
 if(isset($_GET['price']) && $_GET['price'] == 'ASC'){
$price = 'ASC';
 }else{
$price = 'DESC';
}

if ($search !== null){

$sql = "SELECT * FROM people WHERE MATCH (lname,fname) AGAINST (:search IN BOOLEAN MODE)";

$q   = $conn->prepare($sql) or die("failed!");
// Bind the params to the placeholders
$q->bindParam(':search', $search, PDO::PARAM_STR);
$q->execute();
}

if ($search !== null && $category !== null){

$sql = "SELECT * FROM people WHERE MATCH (lname,fname) AGAINST (:search IN BOOLEAN MODE) AND category = :category";

$q   = $conn->prepare($sql) or die("failed!");
// Bind the params to the placeholders
$q->bindParam(':search', $search, PDO::PARAM_STR);
$q->bindParam(':category', $category, PDO::PARAM_STR);
$q->execute();
}

if ($category !== null && $search !== null && isset($price)){

$sql = "SELECT   *
        FROM     people
        WHERE    MATCH (lname,fname) AGAINST (:search IN BOOLEAN MODE)
        AND      category = :category
        ORDER BY price ".$price;

$q = $conn->prepare($sql);
// Bind the params to the placeholders
$q->bindParam(':search', $search, PDO::PARAM_STR);
$q->bindParam(':category', $category, PDO::PARAM_STR);
$q->execute();
}

if ($category == null && $search !== null && isset($price)){

$sql = "SELECT   *
        FROM     people
        WHERE    MATCH (lname,fname) AGAINST (:search IN BOOLEAN MODE)

        ORDER BY price ".$price;

$q = $conn->prepare($sql);
// Bind the params to the placeholders
$q->bindParam(':search', $search, PDO::PARAM_STR);
$q->execute();
}

if ($q){

//declaring counter
    $count=0;
    while($r = $q->fetch(PDO::FETCH_ASSOC)){
    $row = $r;

    $fname = $row['fname'];
    $lname = $row['lname'];
    $firstname = $row['firstname'];
    $surname = $row['surname'];
    $expire = $row['expire'];

    $oDate = strtotime($row['expire']);
    $sDate = date("d/m/y",$oDate);

//counter equals
$count++;

//insert an image every 5 rows
if($count==5){
$count=0;
   echo "<table width='50%' style='border-bottom:1px solid #000000;'>";
echo "<tr>";
echo "<td>";
echo "<div id='page-wrap'>";
echo "<div class='discounted-item freeshipping'>";

echo "<a href='images/box1.png' rel='lightbox'><img src='images/box1.png' width='160px' height='200px' /></a>";


echo "<div class='reasonbar'><div class='prod-title' style='width: 70%;'>AN AD CAN GO HERE</div><div class='reason' style='width: 29%;'><b>Ad Company</b></div></div>";

echo "<div class='reasonbar'><div class='prod-title1' style='width: 70%;'>Description about the advert from a company</div><div class='reason1' style='width: 29%;'>Category: Advert</div></div>";

echo "<div class='reasonbar'><div class='prod-title2' style='width: 70%;'>HELLO, User</div><div class='reason2' style='width: 29%;'></div></div>";


echo "</td>";
echo "</tr>";
echo "</td>";
echo "</tr>";
echo "</table>";
}

echo "<table width='50%' style='border-bottom:1px solid #FFFFFF'>";
echo "<tr>";
echo "<td>";
echo "<div id='page-wrap'>";
echo "<div class='discounted-item freeshipping'>";

echo "<a href='./img/users/" . $row['category'] . "/" . $row['username'] . "/" . $row['filename'] . "' rel='lightbox'><img src=\"./img/users/" . $row['category'] . "/" . $row['username'] . "/" . $row['filename'] . "\" alt=\"\" width='80px' height='100px' /></a>";


    echo "<div class='expanderHead'>";

echo "<div class='reasonbar'><div class='prod-title'>" .$row['fname'] . "</div><div class='reason' style='width: 29%;'><b>". $row['firstname'] . " " . $row['surname'] ."</b></div></div>";


echo "<div id='expanderContent' style='display:none'><div class='reasonbar'><div class='prod-title1'>" . $row['lname'] . "</div><div class='reason1' style='width: 29%;'>Category:<br /> ". $row['category'] . "</div></div>";

    echo "<div class='reasonbar'><div class='prod-title2' style='width: 70%;'><form action='adclick.php' method='post'><input type='hidden' name='username' value='" . $row['username'] . "'/><input type='submit' name='submit' value='Reply To this ad'></form></div><div class='reason2' style='width: 29%;'></div></div></div>";


echo "<div class='reasonbar'><div class='prod-title2' style='width: 70%;'>Expires: $sDate</div><div class='reason2' style='width: 29%;'>Price: &pound;". $row['price'] . "</div></div>";



echo "</td>";
echo "</tr>";
echo "</td>";
echo "</tr>";
echo "</div>";
echo "</table>";




}


}

else
    echo "No results found for \"<b>$search</b>\"";


//disconnect
mysql_close();

?>
</div>  
</div> 
<div class="footer">
<p> <a href="">Private Policy</a> | <a href="">Terms and Conditions</a> | <a href="">FAQ</a>   </p>
</div> 
</body>
</html>
4

1 回答 1

1

示例代码:

$start是起始编号,$limit是结束编号(一页)..$count是记录总数

echo "<h5>Showing ".$start." to ".($start+$limit)." Records of ".$count." Records</h5>";
if($start<=($count-$limit))
{
    echo '<a style="float:right" href="'.$_SERVER['PHP_SELF'].'?start='.($start+$limit).'&limit='.$limit"><t1>Next</t1></a>';
}


$prev = $start-$limit;
if ($prev >= 0)
{
    echo '<a style="float:left" href="'.$_SERVER['PHP_SELF'].'?start='.$prev.'&limit='.$limit"><t2>Previous</t2></a>';
}


$i=0;
$l=1;
echo "<p align='center'>";
for($i=0;$i < $count;$i=$i+$limit)
{
    if($i <> $start)
    {
        echo "<a href='listing_test.php?start=$i&limit=$limit'><font face='Verdana' size='2'><b>&nbsp;$l&nbsp;</b></font></a> ";
    }
    else
    {
        echo "<font face='Verdana' size='4' color=#2E9AFE ><b>&nbsp;$l&nbsp;</b></font>";
    }        
$l=$l+1;
}
echo "</p>";

编辑:

$result = mysql_query("SELECT * FROM table_name LIMIT ".$start.",".$limit);
    $total=mysql_query("SELECT * FROM table_name");
    $count=mysql_num_rows($total);
于 2012-10-03T08:48:04.850 回答