-2

I have a question on MySQL paging. A user's record is displayed on a table with many other user's record. and the table is sorted/paged. Now I need to display the page that containing the user's row directly after the user login. How can I achieve this?

create table t_users (id int auto_increment primary key, username varchar(100)); insert t_users(username) values ('jim'),('bob'),('john'),('tim'),('tom'), ('mary'),('elise'),('karl'),('karla'),('bob'), ('jack'),('jacky'),('jon'),('tobias'),('peter');

I searched the google but not found answer so please help

gprathour
  • 14,813
  • 5
  • 66
  • 90
vishal
  • 17
  • 6
  • You need to clarify your question a bit. I see a lot of details here, possibly irrelevant to the issue, and your question title doesn't match the question in the description. Are you asking how to lookup a user's data once they login? Or something else? – Daniel Rippstein Sep 18 '14 at 04:23
  • Please answer my question first (in normal English) before posting more code. We need to know the problem clearly first. Then we can discuss code later. – Daniel Rippstein Sep 18 '14 at 04:26
  • I have simple pagination script which shows sites with clause "where online>0" – vishal Sep 18 '14 at 04:28
  • So when user post its id using index.php?u=id i want to display that page which contains his site – vishal Sep 18 '14 at 04:30
  • U can see example http://u-on.eu/index.php?u=58983 here click this link i want to do exactly like this – vishal Sep 18 '14 at 04:33
  • I want to do exactly like http://u-on.eu/index.php?u=58983 when user clicks it lands on page where he has own site – vishal Sep 18 '14 at 04:35
  • Alright. So for other commenters sake, let me restate your question and tell me if it's wrong. – Daniel Rippstein Sep 18 '14 at 04:38
  • sorry i am new on stack so i deleted the comments – vishal Sep 18 '14 at 04:38
  • You have a list of users. This list is sorted and paginated. When you're given a certain user's ID, you want to jump to the page that shows the user's row of info. But you don't know how to determine what page he's on and would like some advice. – Daniel Rippstein Sep 18 '14 at 04:39
  • Yes your right .i tried lot of but cant achieve – vishal Sep 18 '14 at 04:40
  • One last question... is this list sorted and paginated using SQL? Such as in this example: http://stackoverflow.com/a/3799223/2391142 Or is it through some other method. – Daniel Rippstein Sep 18 '14 at 04:42
  • Yes its i am using mysqli and php – vishal Sep 18 '14 at 04:45
  • Its only pagination code but i want to determine page number by its id – vishal Sep 18 '14 at 04:50
  • Thanks it worked.i have code to get position of id but didnt found solution to get page number from it.it is my first question on any forum and i got solution quiqly.thanks again – vishal Sep 18 '14 at 07:27
  • Plz can you ellaborate 1st step of code.i am a noob .i worked with my own while loop to check position but i think would be faster to use sql built in query – vishal Sep 18 '14 at 08:07
  • Where its answer gone? If i deleted it how to get it back? – vishal Sep 18 '14 at 08:50
  • Why its comments are deleting automatically? – vishal Sep 18 '14 at 09:14

1 Answers1

2

There are two steps for this:

1. Determine the row's position in your sorted table.

Copied and tweaked from: https://stackoverflow.com/a/7057822/2391142

Use this 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;

...replacing the ORDER BY id ASC with whatever your actual sort order is. And replacing the number 1 in WHERE id=1 with the provided number in that index.php?u=id url.

2. Determine the page number based on the row's position.

Use this PHP to determine the needed page number...

$rows_per_page = 50;
$user_row_position = [result you got from step 1];
$page = ceil($user_row_position / $rows_per_page);

...replacing the 50 with whatever your real rows-per-page limit is, and putting the real SQL result in $users_row_position.

And voila. You'll have the destination page number in the $page variable and hopefully you can take it from there.

EDIT

After further discussion in the comments, use this bit of 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.
}
Community
  • 1
  • 1
  • $test=mysqli_query($link," select * from sites where online>0"); can you plz convert this into your above code – vishal Sep 18 '14 at 08:38
  • Why no one is replying? – vishal Sep 18 '14 at 12:45
  • Patience OP, some of us actually sleep. Now, I have to ask one more question before I can help you... what is the field name for the user ID in this "sites" table? – Daniel Rippstein Sep 18 '14 at 14:18
  • Sorry i am new so got confused. Field name for userid is 'id' – vishal Sep 18 '14 at 14:21
  • $start=0; $limit=10; $id=$_GET['u']; if(isset($_GET['page'])) { $page=$_GET['page']; $start=($page-1)*$limit; } $query_test=mysqli_query($link," select * from sites where online='0'"); $rows_test=mysqli_num_rows($query_test); $total_test=ceil($rows_test/$limit); $rowr=array(); while($rowr[]=mysqli_fetch_array($query_test)){}; $total1=$rows_test-1; for($i=0;$i<=$total1;$i++){ if ( $rowr[$i]['id']==$id){$number=$i;echo $number;$page_get=ceil($number/10);echo $page_get;break; }else{continue;} } i have done this it works perfect but when there are more rows it will slow because of while loop – vishal Sep 18 '14 at 14:47
  • I've updated to the answer to include a more specific code example. I haven't tested it myself yet, so it might need tweaking, but it should be very close to something that'll solve your problem. – Daniel Rippstein Sep 18 '14 at 16:20
  • Thanks it worked just there is syntax error .i edited it and it worked – vishal Sep 18 '14 at 16:56
  • Nice to know. What was the syntax error though? You got me curious. – Daniel Rippstein Sep 18 '14 at 17:53