views:

362

answers:

6

I'm working with php and I want to do a next button that's step to the next 10 records (like when you browse question with stackoverflow) I don't know how to do that but I'm thinking to do it with Top n record ? Do you think that's a good idea ? Any suggestion?

A: 

I know in MySQL you can use LIMIT X, Y where X is the lower bound of the return and Y is the upper bound. So if you wanted to return 10-20 you would use LIMIT 10, 20. Not sure what the MS-SQL equivalent is.

Suroot
wrong, x = offset and y = amount of rows
Karsten
A: 

doesn't mssql have something like LIMIT in mysql? so you could do:

select xxx from yyy LIMIT 0,10

for first 10 results, then do LIMIT 10,20 for next 10 results etc.

dusoft
No, it doesn't. TOP lets you do LIMIT 0,10 but it doesn't let you do LIMIT 10,20.
ceejayoz
? He is using limit in the answer, not top.
Ólafur Waage
+3  A: 

As for doing it in PHP, you can easily make the button send a POST or GET request for the starting amount. For instance, a user would make the initial request and that is just yoursite.com/search.php, and the next button would send them to the same page with the same search criteria only send an additional field of "start", (i.e. yoursite.com/search.php?start=10). And in the code, you can simply check for it:

if(isset($_POST['start'])) {
    //code to add to the search string to start at $_POST['start']
}

Edit 1: This article is the best I could find as to how to replicate MySQL's LIMIT function. Also, this one has a more definitive query to reference, but it's the same idea.

Mike
A: 

You can use MySQL's limit

Set a variable called:

$limit = 10;
$pl = $_GET["page"] * $limit;
if(!isset($_GET["page"]) || $_GET["page"] == 1)
{
    $pl = 0;
}

and in your query do

$sql = sprintf("SELECT * FROM table LIMIT %d,%d" 
mysql_real_escape_string($pl),
mysql_real_escape_string($limit));

Btw this is from memory but i think it works.

Ólafur Waage
A: 

Would this be any help to you?

$count=$_POST[page]*10;
Sukasa
argh, apparently the MSSQL query is causing stackoverflow to not accept the answer. One sec, I'll get it posted.
Sukasa
Rather, refer to this since stackoverflow will simply nto accept the query (I have no idea why): http://stackoverflow.com/questions/603724/how-to-implement-limit-with-microsoft-sql-server
Sukasa
A: 

for MySQL:

$rowsPerPage = 10;
$offset = ((int)$_GET['page'] - 1) * $rowsPerPage;

$result = mysql_query(
    sprintf('select xxx from yyy LIMIT %d,%d', $offset, $rowsPerPage)
);
Karsten