PHP分页,首先查看某个页面

时间:2011-09-01 11:49:52

标签: php mysql sql

当用户点击联盟时,这是我网站上的分页脚本 然后联盟回到屏幕上,如果联盟超过3排,那么它会将其分成几页。
我正在做的事情取决于用户在联盟中的位置(SQL查询使用ORDER BY表中的总点数列),例如,如果用户在联赛表的第一页上,那么就是首先显示该页面,但如果用户在联赛表的第3页上,则首先显示该页面。

有没有人知道如何让我实现这个目标?

//Recently updated from answer

$sql="SELECT members.username, members.total_points FROM members, members_leagues WHERE members.username = members_leagues.username AND 
        members_leagues.sub_league = '$chosenleague' ORDER BY members.total_points DESC";

$result=mysql_query($sql);

$i = 0;
$found = false;
$team_position = 0;

while (!$found && $row = mysql_fetch_row($result)){
   if ($row[username] == $_SESSION[username]) {
   $team_position = $i;
   $found = true;
   }
 $i++;
}

$rowsPerPage = 3;

$pageNum =  ceil($i/$rowsPerPage);
//end of recently updated

if(isset($_GET['page']))
    $pageNum = $_GET['page'];

$offset = ($pageNum - 1) * $rowsPerPage;
$counter = $offset + 1;

$query = " SELECT members.username, members.teamname, members.total_points, FROM members, members_leagues WHERE members.username = members_leagues.username AND members_leagues.sub_league = '$chosenleague' ORDER BY members.total_points DESC " . " LIMIT $offset, $rowsPerPage";

$result = mysql_query($query) or die('Error, query failed');

echo "<h3 style=\"color:red;\">$chosenleague</h3>";
echo "<table>";
echo "<tr><th>Position</th>";
echo "<th>Team</th>";
echo "<th>Points/Overall</th>";
echo "<th>Points/Last Race</th>";
echo "<th>Team Setup</th></tr>";

while($row = mysql_fetch_array($result))
{
    if($row[username] == $_SESSION[username])
        echo "<tr style=\"color:red;\"><td>";
    else
        echo "<tr><td>";

    echo $counter;
    echo "</td><td>";
    echo $row[teamname];
    echo "</td><td>";
    echo $row[total_points];
    echo "</td><td>";
    echo "</td><td>";
    echo "</td></tr>";
    $counter++;
}
echo "</table>";

$query   = "SELECT COUNT(members.username) AS numrows FROM members, members_leagues WHERE members.username = members_leagues.username 
AND members_leagues.sub_league = '$chosenleague'";

$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

$maxPage = ceil($numrows/$rowsPerPage);

$self = $_SERVER['PHP_SELF'];
$nav  = '';

if ($pageNum > 1)
{
    $page  = $pageNum - 1;

    $prev  = " <a href=\"$self?league=". rawurlencode($chosenleague) . "&page=". rawurlencode($page) . "\"><< Prev</a> ";
    $first = " <a href=\"$self?league=". rawurlencode($chosenleague) . "&page=". rawurlencode(1) . "\">First</a> ";
}
else
{
    $prev  = ''; 
    $first = ''; 
}

if ($pageNum < $maxPage)
{
    $page = $pageNum + 1;
    $next = " <a href=\"$self?league=". rawurlencode($chosenleague) . "&page=". rawurlencode($page) . "\">Next >></a> ";
    $last = " <a href=\"$self?league=". rawurlencode($chosenleague) . "&page=". rawurlencode($maxPage) . "\">Last</a> ";
}
else
{
    $next = '';
    $last = ''; 
}

echo "<div id=\"pagenum\">Page $pageNum of $maxPage &nbsp;". $first . $last . $prev . $next ."</div>";

1 个答案:

答案 0 :(得分:0)

你可以通过mysql或php来实现:

使用PHP:

在数组中找到所请求记录的位置,然后计算您必须提取的页面并执行相应的查询。像。的东西。

$i = 0;
$found = false;
$team_position = 0;

while (!$found && $row = mysql_fetch_row) {
  if ($row['team'] == 'team_your_searching_for') {
    $team_position = $i;
    $found = true;
  }
  $i++;
}

//calculate $top and $bottom
...

$sql = "SELECT * FROM members LIMIT $top, $bottom;";
...

使用MySQL:

您可以创建一个生成自动增量值的查询,另一个可以从另一个结果中选择。我的意思是

-- get the the selected member's position
SELECT team, pos FROM (SELECT team, points, @position = @position + 1 AS pos FROM members ORDER BY points) WHERE team = @the_team_your_searching_for;

-- get the nr of members
SELECT COUNT(*) FROM members;

...

-- calculate the page you wanna extract (@top, @bottom), and extract it
SELECT * FROM members LIMIT @top, @bottom;