2

With the help of to this question, "Divide numbers into equal parts to create Sitemap from mysql", I got this result:

Array ( [0] => 0,500 
        [1] => 500,1000 
        [2] => 1000,1500 
        [3] => 1500,2000 
        [4] => 2000,2500 
        [5] => 2500,2990 )

My question is, I want to pass the above Array values to select query limit. like this:

 "SELECT * FROM `table_name` WHERE `rec_id`='1' limit 0 OFFSET 500 " 

and so on upto limit 2500, 2990.

How to do this? I am new to development. please help me with the solution.

Thank you in advance.

4
  • Is this some sort of pagination? Commented Dec 14, 2016 at 12:22
  • while looping in outside array, u can explode innner array or u can multiply the index with 500 and u will have starting index. Commented Dec 14, 2016 at 12:24
  • @RiggsFolly not like pagination. I want to send mails in group. so for this i have created group of 500 contacts. and fetching 500 email address from table . but i am confused how to use it in query? Commented Dec 14, 2016 at 12:26
  • You need to start by creating the array correctly so you get [0] => 0,500, [1] => 501,500,...... Commented Dec 14, 2016 at 13:44

2 Answers 2

2

Actually you want a pagination with 500 results begining at an index, right ? So limit should always be 500, only offset value should change.

$queries = [];
$array = Array ( [0] => 0,500 
    [1] => 500,1000 
    [2] => 1000,1500 
    [3] => 1500,2000 
    [4] => 2000,2500 
    [5] => 2500,2990 );

foreach ( $array as $value ){
    $offsets = explode(',', $value);
    $queries[] = "SELECT * FROM `table_name` WHERE `rec_id`='1' limit 500 OFFSET $offsets[0]";
}
Sign up to request clarification or add additional context in comments.

Comments

1

Here is the code

foreach($arr as $k => $v){

  list($limit, $offset) = explode(','$v);

   $query_arr[] = "SELECT * FROM `table_name` WHERE `rec_id`='1' limit $limit OFFSET $offset";
}

And then fire $query_arr as you want

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.