Vijay Chauhan Vijay Chauhan - 4 years ago 194
PHP Question

Fetch specific record in foreach loop in php

I want to fetch two record in foreach loop.

After fetch record, next two record for pagination

$post_img_id=array(21,24,25,28,40,44,46,47,52,78,88,89,92,101); //this is id of table data

$total=count($post_img_id,COUNT_RECURSIVE); //total record





foreach($post_img_id as $id)
$post_img_q="select * from $post where id=$id LIMIT $k,$item";
$post_img_res=mysql_query($post_img_q) or die(mysql_error());


echo 'record';

how to solve this problem ? please help me !

Thanx for Advance !

Answer Source

please use array_chunk() refer this link :

i.e : print_r(array_chunk($post_img_id, 2, true));

$nameofArray = array_chunk($post_img_id, 2, true);
foreach($nameofArray as $Array)
//your code here...

check this in your code its devide your array in portion of 2 where you will get array of 2 records so it will help you more easily to paginate record.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download