san san san san - 1 year ago 60
JSON Question

dynamically encode data in json with limit using php for mobile app like androd , ios

I have record of 1000 and more records. I have to provide only 50 records per request like
0-50, 50-100, 100-150 like this. I'm using the following code:

public function get_database($data)

$start = $data['start'];
$limit = $data['limit'];
$alumni_details = array();

$query1 = "select * from alumni where
status='Active',limit '".$start."','".$limit."' ";

$query_run = mysql_query($query1);

while($row = mysql_fetch_assoc($query_run))
$row['date_of_birth'] = date('d M, Y', strtotime($row['date_of_birth']));

$alumni_detail['alumni_details'] = $row;
$alumni_details[] = $alumni_detail;

echo json_encode($alumni_details);

But I need to take only user_id based on that I need to encode data in json dynamically with limit.

Answer Source

Your code as below:

$start = $data['start'];

$query = SELECT * FROM `alumni` WHERE `status` = 'Active' ORDER BY `alumni_id` LIMIT $start,5"

It should be dynamic 5 would not be taken as constant. take it in some variable, and yes it should be fixed, but as per requirement in future it can be change so take it in variable.