开发者

PHP Pagination not functioning correcetly, stops on 6th page

I've recently got back into doing some PHP coding but have got stuck on something with PHP Pagination, it would seem that anything I tried either results in it not counting the pages and displaying on one page of results, or just stops at Page 6 and goes no further, I know with the data that is stored in the MySQL database, it should be showing 52 pages of results at 12 per page.

So I'm in need of some help, I thought it was working until I hit the won't go further than Page 6 after putting in 100's of entires into the database.

The code is as follows

<?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.L开发者_开发知识库eagueMatchHomeGoals 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))
    {
?>

More to add, if I take out the first SELECT statement, I get the same result?


Try this out. It's just your code with some minor modifications.

<?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))
    {
?>

It's actually what @g.salakirov suggested but in the context of all the code you supplied. I haven't tested it, but it should work. (Famous last words. :D)


You need to use mysql_num_rows(resource $result) rather than mysql_fetch_row(resource $result) to get the number of rows in here: .

$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;

Alternatively, you can use a "count(*)" query in the beginning to get the number of entries, which will be significantly faster.

For example:

  $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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜