在php中获取foreach循环中的特定记录(Fetch specific record in foreach loop in php)

编程入门 行业动态 更新时间:2024-10-23 01:33:36
在php中获取foreach循环中的特定记录(Fetch specific record in foreach loop in php)

我想在foreach循环中获取两个记录。

获取记录后,接下来的两个记录为分页

$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 $item=2; $total_page=ceil($total/$item); $cur_page=isset($_GET['page'])?$_GET['page']:1; $k=($cur_page-1)*$item; foreach($post_img_id as $id) { if($co<=10){ $post_img_q="select * from $post where id=$id LIMIT $k,$item"; $post_img_res=mysql_query($post_img_q) or die(mysql_error()); while($post_img_row=mysql_fetch_assoc($post_img_res)) { echo 'record'; } } ?>

如何解决这个问题呢 ? 请帮帮我 !

提前谢谢!

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 $item=2; $total_page=ceil($total/$item); $cur_page=isset($_GET['page'])?$_GET['page']:1; $k=($cur_page-1)*$item; foreach($post_img_id as $id) { if($co<=10){ $post_img_q="select * from $post where id=$id LIMIT $k,$item"; $post_img_res=mysql_query($post_img_q) or die(mysql_error()); while($post_img_row=mysql_fetch_assoc($post_img_res)) { echo 'record'; } } ?>

how to solve this problem ? please help me !

Thanx for Advance !

最满意答案

请使用array_chunk()参考此链接: http ://php.net/manual/en/function.array-chunk.php

即: print_r(array_chunk($post_img_id, 2, true));

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

在你的代码中检查这个,它将你的数组分成2个部分,你将获得2个记录的数组,这样它将更容易帮助你分页记录。

please use array_chunk() refer this link :http://php.net/manual/en/function.array-chunk.php

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.

更多推荐

本文发布于:2023-07-25 05:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1256752.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Fetch   foreach   php   loop   record

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!