2011-08-30 84 views
1

我最近又回到了一些PHP編程,但已經陷入與PHP分頁的東西,似乎我試過的任何事情都會導致它不計算頁面和在結果的一頁上顯示,或者只是在第6頁停下來,並且不再進一步,我知道存儲在MySQL數據庫中的數據,它應該顯示52頁結果,每頁12個。PHP分頁不能正常工作,在第6頁停止

所以我需要一些幫助,我認爲這是有效的,直到我打入數據庫之後,將不會超過第100頁。

的代碼如下

<?php 
    // 
    //How to print date 
    // 
    if($print_date == 1) 
    { 
     $print_date = '%d.%m.%Y'; 
    } 
    elseif($print_date == 2) 
    { 
     $print_date = '%m.%d.%Y'; 
    } 
    elseif($print_date == 3) 
    { 
     $print_date = '%W, %M %D %Y'; 
    } 
    if (isset($_GET['pageno'])) { 
     $pageno = $_GET['pageno']; 
    } else { 
     $pageno = 1; 
    } // if 

    $get_matches = mysql_query("SELECT 
     O.OpponentName AS hometeam, OP.OpponentName AS awayteam, 
     LM.LeagueMatchHomeGoals AS goals_home, 
     LM.LeagueMatchAwayGoals AS goals_away, 
     LM.LeagueMatchID AS id, 
     DATE_FORMAT(LM.LeagueMatchDate, '$print_date') AS date 
     FROM tplls_leaguematches LM, tplls_opponents O, tplls_opponents OP 
     WHERE O.OpponentID = LM.LeagueMatchHomeID AND 
     OP.OpponentID = LM.LeagueMatchAwayID AND 
     LeagueMatchSeasonID LIKE '$defaultseasonid' 
     ORDER BY LM.LeagueMatchDate"); 

    $get_matches = @mysql_query($SQL) or die("Error Processing Fixtures"); 
    $query_data = mysql_fetch_row($get_matches); 
    $numrows = $query_data[0]; 
    $rows_per_page = 12; 
    $lastpage  = ceil($numrows/$rows_per_page); 
    $pageno = (int)$pageno; 
    if ($pageno > $lastpage) { 
     $pageno = $lastpage; 
    } // if 
    if ($pageno < 1) { 
     $pageno = 1; 
    } // if 
    $limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page; 

    $get_matches2 = mysql_query("SELECT 
     O.OpponentName AS hometeam, OP.OpponentName AS awayteam, 
     LM.LeagueMatchHomeGoals AS goals_home, 
     LM.LeagueMatchAwayGoals AS goals_away, 
     LM.LeagueMatchID AS id, 
     DATE_FORMAT(LM.LeagueMatchDate, '$print_date') AS date 
     FROM tplls_leaguematches LM, tplls_opponents O, tplls_opponents OP 
     WHERE O.OpponentID = LM.LeagueMatchHomeID AND 
     OP.OpponentID = LM.LeagueMatchAwayID AND 
     LeagueMatchSeasonID LIKE '$defaultseasonid' 
     ORDER BY LM.LeagueMatchDate, hometeam $limit",$connection) 
     or die(mysql_error()); 
    while($row = mysql_fetch_array($get_matches2)) 
    { 
?> 

更多以添加,如果我拿出第一個SELECT語句,我得到了同樣的結果?

回答

0

嘗試了這一點。這只是你的代碼,稍作修改。

<?php 
    // 
    //How to print date 
    // 
    switch ($print_date) 
    { 
    case 1: 
     $print_date = '%d.%m.%Y'; 
     break; 
    case 2: 
     $print_date = '%m.%d.%Y'; 
     break; 
     case 3: 
     $print_date = '%W, %M %D %Y'; 
    } 

    if (isset($_GET['pageno'])) { 
     $pageno = $_GET['pageno']; 
    } else { 
     $pageno = 1; 
    } // if 

    $clause = " 
     FROM tplls_leaguematches LM, tplls_opponents O, tplls_opponents OP 
     WHERE O.OpponentID = LM.LeagueMatchHomeID AND 
     OP.OpponentID = LM.LeagueMatchAwayID AND 
     LeagueMatchSeasonID LIKE '$defaultseasonid' 
     ORDER BY LM.LeagueMatchDate"; 

    $query = "SELECT COUNT(*) as num $clause"; 
    $result = @mysql_query($query) or trigger_error("SQL", E_USER_ERROR); 
    $r = mysql_fetch_row($result); 
    $numrows = $r[0]; 

    $rows_per_page = 12; 
    $lastpage  = ceil($numrows/$rows_per_page); 
    $pageno = (int)$pageno; 
    if ($pageno > $lastpage) { 
     $pageno = $lastpage; 
    } // if 
    if ($pageno < 1) { 
     $pageno = 1; 
    } // if 
    $limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page; 

    $query = "SELECT 
     O.OpponentName AS hometeam, OP.OpponentName AS awayteam, 
     LM.LeagueMatchHomeGoals AS goals_home, 
     LM.LeagueMatchAwayGoals AS goals_away, 
     LM.LeagueMatchID AS id, 
     DATE_FORMAT(LM.LeagueMatchDate, '$print_date') AS date $clause $limit"; 

    $get_matches = mysql_query($query, $connection) 
     or die(mysql_error()); 
    while($row = mysql_fetch_array($get_matches)) 
    { 
?> 

它實際上是@ g.salakirov建議的,但是在您提供的所有代碼的上下文中。我還沒有測試過,但它應該工作。 (着名的遺言。:D)

+0

注意:如果您正在從事生產工作,您應該查看[mysqli](http://us.php.net/manual/en/book.mysqli.php)和[預先準備的語句](http:// us3 .php.net /手動/ EN/pdo.prepared-statements.php)。大多數教程吸吮!我發現[這篇文章在SitePoint上](http://www.sitepoint.com/perfect-php-pagination/)。我還沒有讀過它,但它看起來很有希望。 – Herbert

+0

這似乎並不奏效,它引發了一個致命的錯誤。 致命錯誤:位於/home/vhosts/binfieldfc.com/httpdocs/leaguetables/firstteam/index2.php中的SQL行1688 – OuterHaven

+0

這是行 1688 $ result = @mysql_query($ query)或trigger_error(「SQL」, E_USER_ERROR); – OuterHaven

0

您需要使用mysql_num_rows(resource $ result)而不是mysql_fetch_row(resource $ result)來獲取這裏的行數:。

$get_matches = @mysql_query($SQL) or die("Error Processing Fixtures"); 
$query_data = mysql_fetch_row($get_matches); 
$numrows = $query_data[0]; 
$rows_per_page = 12; 
$lastpage  = ceil($numrows/$rows_per_page); 
$pageno = (int)$pageno; 

或者,您可以在開始時使用「count(*)」查詢來獲取條目數,這將顯着加快。

例如:

$get_matches = mysql_query("SELECT 
    count(*) as resultCount 
    FROM tplls_leaguematches LM, tplls_opponents O, tplls_opponents OP 
    WHERE O.OpponentID = LM.LeagueMatchHomeID AND 
    OP.OpponentID = LM.LeagueMatchAwayID AND 
    LeagueMatchSeasonID LIKE '$defaultseasonid' 
    ORDER BY LM.LeagueMatchDate"); 
+0

這兩個,只是返回0頁。 – OuterHaven