我最近又开始做一些PHP编码,但是在PHP分页上陷入了困境,似乎我尝试执行的任何操作要么导致它不计算页面数而不显示结果的一页,要么仅停留在第6页而且,我知道使用MySQL数据库中存储的数据,它应该显示52页的结果,每页12个。
因此,我需要一些帮助,我认为这是可行的,直到将100份完整的内容放入数据库后,我的努力不会超出第6页。
代码如下
<?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语句,是否得到相同的结果?
最佳答案
试试看只是您的代码做了一些小的修改。
<?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)