2014-11-23 105 views
0

我想問是否有解決方案來查詢MySQL數據庫通過PHP從一組結果只加載1頁,但在分頁仍然顯示像[1] 2 3 4 5 ...所以當用戶點擊頁面2像1 [2] 3 4 5 ...然後加載第二頁等?僅限頁面顯示

我的數據庫非常龐大,一次加載1000頁會消耗大量的帶寬,並且需要大約50秒的過程,這太多了。

我需要找到一個解決方案來降低加載時​​間。我的實際查詢的樣子:

$start = ($page - 1) * $per_page; 
$query = ' 
     SELECT *, (MATCH(url) AGAINST('.$qo1.''.$out.''.$qo2.' IN BOOLEAN MODE) * 0.015 + MATCH(title) AGAINST ('.$qo1.''.$out.''.$qo2.' IN BOOLEAN MODE) * 0.015 + MATCH(description,body) AGAINST('.$qo1.''.$out.''.$qo2.' IN BOOLEAN MODE) * 0.040 + MATCH(keywords) AGAINST ('.$qo1.''.$out.''.$qo2.' IN BOOLEAN MODE) * 0.030) AS relevance 
     FROM table 
     WHERE MATCH(url, title, description, body, keywords) AGAINST ('.$qo1.''.$out.''.$qo2.' IN BOOLEAN MODE) 
     ORDER BY relevance '.$ord.' LIMIT '.$start.','.$per_page.''; 

問候

+0

這與基本分頁有什麼不同? – Strawberry 2014-11-23 09:52:06

+0

它一次加載所有頁面,每頁限制20個結果,但用戶不會滿意等待,直到加載完成,所以我的意思是隻加載20個結果,但要檢查多少結果,並且僅在用戶點擊它時加載第二頁,否則不加載所有10.000結果。 – 2014-11-23 10:06:05

+0

只需看看更多分頁示例。例如,谷歌如何做到這一點? – Strawberry 2014-11-23 10:15:45

回答

0

這個腳本指出我開出右側方向爲建設我的分頁,只允許通過顯示的頁面可以收到效果。

<?php 
// Script and tutorial written by Adam Khoury @ developphp.com // Line by line explanation : youtube.com/watch?v=T2QFNu_mivw 
include_once("mysqli_connection.php"); 
// This first query is just to get the total count of rows 
$sql = "SELECT COUNT(id) FROM testimonials WHERE approved='1'"; 
$query = mysqli_query($db_conx, $sql); 
$row = mysqli_fetch_row($query); 
// Here we have the total row count 
$rows = $row[0]; 
// This is the number of results we want displayed per page 
$page_rows = 10; 
// This tells us the page number of our last page 
$last = ceil($rows/$page_rows); 
// This makes sure $last cannot be less than 1 
if($last < 1){ $last = 1; } 
// Establish the $pagenum variable 
$pagenum = 1; 
// Get pagenum from URL vars if it is present, else it is = 1 
if(isset($_GET['pn'])){ $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']); } 
// This makes sure the page number isn't below 1, or more than our 
$last page if ($pagenum < 1) 
{ $pagenum = 1; } 
else if ($pagenum > $last) 
{ $pagenum = $last; } 
// This sets the range of rows to query for the chosen $pagenum 
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows; 
// This is your query again, it is for grabbing just one page worth of rows by applying $limit 
$sql = "SELECT id, firstname, lastname, datemade FROM testimonials WHERE approved='1' ORDER BY id DESC $limit"; 
$query = mysqli_query($db_conx, $sql); 
// This shows the user what page they are on, and the total number of pages 
$textline1 = "Testimonials (<b>$rows</b>)"; $textline2 = "Page <b>$pagenum</b> of <b>$last</b>"; 
// Establish the $paginationCtrls variable 
$paginationCtrls = ''; 
// If there is more than 1 page worth of results 
if($last != 1){ 
/* First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page. */ 
if ($pagenum > 1) 
{ $previous = $pagenum - 1; 
$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> &nbsp; &nbsp; '; 
// Render clickable number links that should appear on the left of the target page number 
for($i = $pagenum-4; $i < $pagenum; $i++) 
{ if($i > 0) 
{ $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; '; 
} 
} 
} 
// Render the target page number, but without it being a link 
$paginationCtrls .= ''.$pagenum.' &nbsp; '; 
// Render clickable number links that should appear on the right of the target page number 
for($i = $pagenum+1; $i <= $last; $i++) 
{ $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; '; 
if($i >= $pagenum+4){ break; } 
} 
// This does the same as above, only checking if we are on the last page, and then generating the "Next" 
if ($pagenum != $last) 
{ $next = $pagenum + 1; 
$paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> '; 
} 
} 
$list = ''; 
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) 
{ $id = $row["id"]; 
$firstname = $row["firstname"]; 
$lastname = $row["lastname"]; 
$datemade = $row["datemade"]; 
$datemade = strftime("%b %d, %Y", strtotime($datemade)); 
$list .= '<p><a href="testimonial.php?id='.$id.'">'.$firstname.' '.$lastname.' Testimonial</a> - Click the link to view this testimonial<br>Written '.$datemade.'</p>'; 
} 
// Close your database connection 
mysqli_close($db_conx); 
?>