开发者

PHP paging, viewing a certain page first

开发者 https://www.devze.com 2023-04-01 22:38 出处:网络
This is my script for the paging on my site when the user clicks on a league. The league is then echoed to the screen, and if the league is over 3 rows then it splits it up in to several pages.

This is my script for the paging on my site when the user clicks on a league.

The league is then echoed to the screen, and if the league is over 3 rows then it splits it up in to several pages.

What I am doing after is depending on where the user is in the league (the SQL query is using ORDER BY the total points column in the table), e.g if the user is on page one of the league table then for it to display that page first, but if the user is on page 3 of the league table then for that page to displayed first.

Does anyone know a way in order for me to achieve this?

//Recently updated from answer

$sql="SELECT members.username, members.total_points FROM members, members_leagues WHERE members.username = m开发者_如何学JAVAembers_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>";


You can do it via mysql or php:

With PHP:

Found the position of the requested record in the array, then calculate the page you have to extract and execute the corresponding query. Something like.

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

With MySQL:

You can create a query that generates an autoincrement value and another that selects from the other's result. I mean

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

精彩评论

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