dongliang2005 2016-01-01 11:35
浏览 101
已采纳

在php中的foreach循环中获取特定记录

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 !

  • 写回答

1条回答 默认 最新

  • douji9734 2016-01-01 11:38
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来