PHP paging code in a variety of program development are required to use, in the development of the Web site is required to select one.
To write the paging code, you first need to understand the SQL query statement: SELECT * from goods limit 2, 7. The core of the PHP paging code is spread around this statement, SQL statement Description: Query goods data table starting from the 2nd data to take out 7 data. In the paging code, 7 indicates how many items each page displays, 2 calculates the number of pages by formula, and then filters out different data by replacing the value of "2" by passing in different parameters.
index.php:
Include ' conn.php '; The introduction of database operation Class
$conn =new conn (); Instantiate the database operation class
$total = $conn->getone (' Select COUNT (*) as total from goods ');
$total = $total [' Total ']; Goods table data total data Bar number
$num = 6;//per page display bar count
$totalpage =ceil ($total/$num); Count pages
if (isset ($_get[' page ')) && $_get[' page ']<= $totalpage) {//Here's a judgment, If get to data and the data is less than the total number of pages to pay the current page parameter, otherwise jump to the first page
$thispage =$_get[' page '];
else{
$thispage =1;
}
<br>//note the Red section of the following SQL statement, calculated to determine starting from the first few data, the current number of pages minus 1 and then multiplied by each page display data bar
$sql = ' Select Goods_id,goods_name,shop_ The price from goods is goods_id limit '. <span style= "COLOR: #ff0000" > ($thispage-1) * $num </span>. ', '. $num. '';
$data = $conn->getall ($sql);
foreach ($data as $k => $v) {
echo ' <li> '. $v [' goods_id ']. ', '. $v [' Goods_name ']. ' ---¥ '. $v [' Shop_price ']. </li> ';
}
<br>//Displays the paging number list for
($i =1; $i <= $totalpage; $i + +) {
echo ' <a href= ' page= '. $i. ' > '. $i. ' </a > ';
}
The code above implements one of the simplest PHP paging effects:
Only by clicking the page number to display different paging data, you can further improve on this basis, as long as the basic principle of understanding, follow-up work is easier to develop.
conn.php Code:
* * Connect to the database for related query operations * *
class conn{public
function __construct () {
include_once (' config.php ');
try{
$this->pdo = new PDO (' Mysql:host=localhost;dbname=test ', ' root ', ' 123456 ');
$this->pdo->setattribute (Pdo::attr_errmode, pdo::errmode_exception);
$this->pdo->exec (' Set names UTF8 ');
} catch (Pdoexception $e) {
echo ' database connection failed: '. $e->getmessage ();
Exit ();
}
Gets a row of data public
function GetOne ($sql) {
$rs = $this->pdo->query ($sql)->fetch (PDO::FETCH_ASSOC);
return $rs;
}
Get Multiline data Results public
function GetAll ($sql) {
$rs = $this->pdo->query ($sql)->fetchall (Pdo::fetch_ ASSOC);
return $rs;
}
The conn.php function is completes the database connection, and realizes the fetching data operation method, here I use is PDO, here may according to everybody Custom to organize the code.
config.php:
*
* Configure Database Information * * *
$cfg _dbhost= ' localhost ';
$cfg _dbname= ' test ';
$cfg _dbuser= ' root ';
$cfg _dbpw= ' 123456 ';
This example is only to illustrate the basis of the paging principle, away from the real use of a lot of changes in the place.
The above is a small series for everyone to bring the PHP page discussion of a simple PHP page code to achieve the full content of the simple, I hope you like Oh ~
If you want to learn more games and game introduction, please continue to pay attention to this site, this site will be the first time to bring you the most beautiful, most fun, the most fresh game information. More exciting content, do in JB51 game channel!