这有两个步骤:
1.确定该行在排序表中的位置。
复制并调整自:https://stackoverflow.com/a/7057822/2391142
使用这个 SQL...
SELECT z.rank FROM (
SELECT id, @rownum := @rownum + 1 AS rank
FROM t_users, (SELECT @rownum := 0) r
ORDER BY id ASC
) as z WHERE id=1;
...用您的实际排序顺序替换ORDER BY id ASC。并将WHERE id=1 中的数字 1 替换为该 index.php?u=id url 中提供的数字。
2。根据行的位置确定页码。
使用这个 PHP 来确定需要的页码...
$rows_per_page = 50;
$user_row_position = [result you got from step 1];
$page = ceil($user_row_position / $rows_per_page);
...用实际的每页行数限制替换 50,并将真正的 SQL 结果放入 $users_row_position。
然后瞧。您将在 $page 变量中获得目标页码,希望您可以从那里获取它。
编辑
在 cmets 中进一步讨论后,使用这点 PHP:
$page = 0;
$limit = 10;
// If a user ID is specified, then lookup the page number it's on.
if (isset($_GET['u'])) {
// Check the given ID is valid to avoid SQL injection risks.
if (is_numeric($_GET['u'])) {
// Lookup the user's position in the list.
$query = mysqli_fetch_array(mysqli_query($link, "SELECT z.rank FROM (SELECT id, @rownum := @rownum + 1 AS rank FROM sites, (SELECT @rownum := 0) r WHERE online='0') as z WHERE id=" . $_GET['u']));
$position = $query[0];
if (is_numeric($position)) {
// Convert the result to a number before doing math on it.
$position = (int) $position;
$page = ceil($position / $limit);
}
}
}
// If a page number is specified, and wasn't already set by looking a user, then lookup the real starting row.
if ($page == 0 && isset($_GET['page'])) {
// Check your given page number is valid too.
if (is_numeric($_GET['page'])) {
$page = (int) $_GET['page'];
}
}
// Notice that if anything fails in the above checks, we just pretend it never
// happened and keep using the default page and start number of 0.
// Determine the starting row based off the page number.
$start = ($page - 1) * $limit;
// Get the list of sites for the provided page only.
$query = mysqli_query($link, "SELECT * FROM sites WHERE online='0' LIMIT " . $start . ", " + $limit);
while ($row = mysqli_fetch_array($query)) {
// Stuff to render your rows goes here.
// You can use $row['fieldname'] to extract fields for this row.
}