1

I am creating API in php for android project. where I need to Create Product items API that gives first 10 records when request method pass 0 value, and gives 10 records more when value is 1. Thus API gives 0-10,11-20 records each time request is made with some int value. currently I am doing this:

$prod=$_REQUEST['prod']; 
$result= sql("SELECT * FROM products where pID='$prod'"); 
echo json_encode(array('item'=>$result));

But it gives me all products from table. how can I get 0-10, then 11-20 and so on.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
avi
  • 11
  • 1
  • 8

2 Answers2

0

I think you are looking for something like this:

"SELECT * FROM `products` WHERE `pID` > $prod ORDER BY `pID` ASC LIMIT 10"

This query fetches all products with pID higher than $prod, orders them by pID ascending, and limits the result to 10 rows. So the $prod you send will the pID of the last record you fetched.

I suggest you look some more into mySQL since this is fairly basic stuff. Here's a basic tutorial.

FrankSunnyman
  • 241
  • 3
  • 13
0

You want to use pagination then you learn about limit and offset what are limit and offset and how works in pagination.

 $limit=10;        ////per  page
 $start=$_REQUEST['your-requested-offset'];0,1,2,3, and so on 

$query="SELECT * FROM TABLE $limit, $start"; 

more you read this Artical about Limit and Offset Pagination using MySQL LIMIT, OFFSET

for how work pagination? read this artical

Paginate Data With PHP

Community
  • 1
  • 1
Abbbas khan
  • 306
  • 2
  • 15