2012-12-14 44 views
-4

如何在此代碼中添加分頁?如何在此代碼中添加分頁

我想在一個頁面中有限的數據,我想在底部爲:

1 2 3 4 5 6 8 9 10 

如果我點擊page 2然後我更多結果顯示該網頁上。

我該怎麼做?

<?php 

//connect to database 

    mysql_connect('localhost','root','pasword'); 
    mysql_select_db('root'); 


/* Get the letter user clicked on and assign it a variable called $sort */ 
$sort = $_REQUEST['letter']; 

/* Let's check if variable $sort is empty. If it is we will create a query to display all customers alphabetically ordered by last name. */ 
if($sort == ""){ 


$qry= "SELECT * FROM tree ORDER BY keywords ASC " ; 
}else{ 
/* if varible $sort is not empty we will create a query that sorts out the customers by their last name, and order the selected records ascendingly. */ 
$qry = "SELECT * FROM tree WHERE keywords LIKE '$sort%' ORDER BY keywords ASC" ; 
} 
/* Notice the use of '%' wilde card in the above query "LIKE '$sort%'". */ 

//next step is to execute the query. 
$execute = mysql_query($qry) or die(mysql_error()); 

/* Before we display results let's create our alphabetical navigation. The easiest way to create the navigation is to use character codes and run them through the "for" loop. */ 
echo "<p>" ; 
for ($i = 65; $i < 91; $i++) { 
    printf('<a href="%s?letter=%s">%s</a> | ', 
    $PHP_SELF, chr($i), chr($i)); 
} 
echo "</p>" ; 

/* now we are ready to display the results. Since out tbl_customers table has only three fileds we will display the results in a paragraphs. In the real world you may need to display the results in a table. 
To display the results we will use "do while" loop to fetch the results. If no customers are found we will display an error message. */ 
if(mysql_num_rows($execute)>0){ 
do{ 
echo "<p>" .$result['id']. " " .$result['keywords']. " " .$result['meaning']. "</p>" ; 
}while($result = mysql_fetch_assoc($execute)); 
}else{ 
echo "<p>No customer found.</p>" ; 
} 
?> 
+0

或者,你有沒有嘗試過什麼?但爲了讓你有個方向:'LIMIT $ iOffset,10'或'for($ i = 0; $ i <$ iResults; $ i ++){if($ i%10 == 0){// stuff stuff here} }'。 – Ben

+0

ben fransen在那裏我把你的代碼放到我的代碼 – user1542571

+0

出於好奇,你有沒有考慮過使用jQuery插件,如DataTables(可以做分頁,限制和排序)? –

回答

1

首先選擇沒有DATAS的絕不每頁頁

mysql_num_rows($execute); 

使用限制命令生成的行TOTAL_NUMBER頁碼來限制這樣的

$qry= "SELECT * FROM tree ORDER BY keywords ASC limit 0,10 " ; 

然後將數據對於每頁頁碼增量限制索引

+0

現在頁面限制problm解決但如何創建分頁 – user1542571

0

您可以使用下面的代碼

$page = $_GET['page']; 

if ($page < 1) 
{  
    $page = 1; 
} 

$conc=mysql_connect('localhost','root',''); 
mysql_select_db('employer',$conc); 

$resultsPerPage = 5; 

$startResults = ($page - 1) * $resultsPerPage; 

$query = mysql_query('SELECT * FROM employee ') or die(mysql_error()); 

$numberOfRows=mysql_num_rows($query); 

$totalPages = ceil($numberOfRows/$resultsPerPage); 

$query = mysql_query("SELECT * FROM employee LIMIT $startResults,$resultsPerPage"); 

while ($output = mysql_fetch_assoc($query)) 
{ 
$result[]=$output; 
} 


for($i = 1; $i <= $totalPages; $i++) 
{ 

    if($i == $page) 
    echo '<strong>'.$i.'</strong>&nbsp'; 
else 
    echo '<a href="?page='.$i.'">'.$i.'</a>&nbsp'; 
} 
0
$page = intval(abs($_GET['page'])); // get security ONE 

if(mysql_num_rows($sql)) // get page number to empty location index.php 
{ 
    header('Location: index.php'); 
}