码迷,mamicode.com
首页 > Web开发 > 详细

php瀑布流,把一个数组分4个数组,按照时间排序

时间:2014-07-26 01:16:06      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   os   数据   io   2014   

简单介绍:把一个数组分成4个数组,取其中1的倍数

<?php
$arr = array(
    0 => 0,
    1 => 1,
    2 => 2,
    3 => 3,
            
    4 => 4,
    5 => 5,
    6 => 6,
    7 => 7,
            
    8 => 8,
    9 => 9,
    10 => 10,
    11 => 11,
            
    12 => 12,
            
);

foreach($arr as $k=>$val){
  for($i=0;$i<4;$i++){
    $a = $i*4+1;           // 其中的1是倍数,可以是0、1、2、3不能超过4;如果是7,则$i<7;
    if($k==$a){
      $item[] = $val;
    }
  }
}

?>

输出结果
Array ( [0] => 1 [1] => 5 [2] => 9 );

 

简单介绍:把一个数组分成4个数组,取其中3的倍数

<?php
$arr = array(
    0 => 0,
    1 => 1,
    2 => 2,
    3 => 3,
            
    4 => 4,
    5 => 5,
    6 => 6,
    7 => 7,
            
    8 => 8,
    9 => 9,
    10 => 10,
    11 => 11,
            
    12 => 12,
            
);
foreach($arr as $k=>$val){
  for($i=0;$i<4;$i++){
    $a = $i*4+1;           // 其中的1是倍数,可以是0、1、2、3不能超过4;如果是7,则$i<7;
    if($k==$a){
      $item[] = $val;    
    }    
  }
}
?>

输出结果
Array ( [0] => 3 [1] => 7 [2] => 11 )

 

 

与数据库结合

class IndexAction extends BaseAction
{
    public function index()
    {
        
        $m = M(article);
        $res = $m->select();
        $f = $this->d($res,2);
        print_r($f);
        
    }
    
    public function d($arr,$v){
        foreach($arr as $k=>$val){
            for($i=0;$i<4;$i++){
                $a = $i*4+$v;
                if($k==$a){
                    $item[] = $val;    
                }    
            }
        }
        return $item;
        
    }
    
 
}

输出结果
Array ( [0] => Array ( [id] => 3 [catid] => 1 [userid] => 1 [username] => yourphp [title] => 欧美风2014夏新款韩国名媛精致复合提花拼纱荷叶袖连衣裙 [title_style] => [keywords] => [copyfrom] => [fromlink] => [description] => 欧美风2014夏新款韩国名媛精致复合提花拼纱荷叶袖连衣裙 [content] => 欧美风2014夏新款韩国名媛精致复合提花拼纱荷叶袖连衣裙 [template] => 0 [thumb] => /b/Uploads/201407/53d220e49655a.jpg [posid] => 0 [status] => 1 [recommend] => 1 [readgroup] => [readpoint] => 0 [listorder] => 0 [url] => /b/index.php?m=Article&a=show&id=3 [hits] => 0 [createtime] => 1406279913 [updatetime] => 1406279913 [name] => [lang] => 1 ) )

 

实例分析:瀑布流

<div>
    <ul>
         <!--第一列-->
         <li></li>
        <!--第二列-->
         <li></li>
        <!--第三列-->
         <li></li>
         <!--第四列-->
         <li></li>
    </ul>
</div>                    

html代码

<!-- screeenA瀑布流 -->
    <div class="screenA">
        <ul>
            <li>
                <volist name="arr_0" id="vo"> 
                <div class="ka">
                    <div class="im">
                        <img src="{$vo.thumb}" />
                    </div>
                    <div class="text"><a href="{$vo.url}">{$vo.title}</a></div>
                    <div class="t_bot"><div class="zan" style="float:left"><span>赞&nbsp;325</span></div><div class="liulan">浏览(365635)</div></div>
                    <div class="line"></div>
                    <div class="ask">
                        <div class="ask_left"><img src="../Public/images/ico_03.jpg" /></div>
                        <div class="ask_rig"><h3>星空下的骑士</h3><span>连体短裤是夏季最凉快和轻便的着装,简单风格</span></div>
                        <div class="clear"></div>
                    </div>
                </div>
                </volist> 
            </li>
            <li>
                <volist name="arr_1" id="vo"> 
                <div class="ka">
                    <div class="im">
                        <img src="{$vo.thumb}" />
                    </div>
                    <div class="text"><a href="{$vo.url}">{$vo.title}</a></div>
                    <div class="t_bot"><div class="zan" style="float:left"><span>赞&nbsp;325</span></div><div class="liulan">浏览(365635)</div></div>
                    <div class="line"></div>
                    <div class="ask">
                        <div class="ask_left"><img src="../Public/images/ico_03.jpg" /></div>
                        <div class="ask_rig"><h3>星空下的骑士</h3><span>连体短裤是夏季最凉快和轻便的着装,简单风格</span></div>
                        <div class="clear"></div>
                    </div>
                </div>
                </volist> 
            </li>
            <li>
                <volist name="arr_2" id="vo"> 
                <div class="ka">
                    <div class="im">
                        <img src="{$vo.thumb}" />
                    </div>
                    <div class="text"><a href="{$vo.url}">{$vo.title}</a></div>
                    <div class="t_bot"><div class="zan" style="float:left"><span>赞&nbsp;325</span></div><div class="liulan">浏览(365635)</div></div>
                    <div class="line"></div>
                    <div class="ask">
                        <div class="ask_left"><img src="../Public/images/ico_03.jpg" /></div>
                        <div class="ask_rig"><h3>星空下的骑士</h3><span>连体短裤是夏季最凉快和轻便的着装,简单风格</span></div>
                        <div class="clear"></div>
                    </div>
                </div>
                </volist> 
            </li>
            <li>
                <volist name="arr_3" id="vo"> 
                <div class="ka">
                    <div class="im">
                        <img src="{$vo.thumb}" />
                    </div>
                    <div class="text"><a href="{$vo.url}">{$vo.title}</a></div>
                    <div class="t_bot"><div class="zan" style="float:left"><span>赞&nbsp;325</span></div><div class="liulan">浏览(365635)</div></div>
                    <div class="line"></div>
                    <div class="ask">
                        <div class="ask_left"><img src="../Public/images/ico_03.jpg" /></div>
                        <div class="ask_rig"><h3>星空下的骑士</h3><span>连体短裤是夏季最凉快和轻便的着装,简单风格</span></div>
                        <div class="clear"></div>
                    </div>
                </div>
                </volist> 
            </li>
            <div class="clear"></div>
        </ul>
    </div>
    <!-- screeenA瀑布流End -->

php代码

class IndexAction extends BaseAction
{
    public function index()
    {
        
        $m = M(article);
        $res = $m->select();
        //print_r($item);
        
        $arr[0] = $this->falls($res,0);
        $arr[1] = $this->falls($res,1);
        $arr[2] = $this->falls($res,2);
        $arr[3] = $this->falls($res,3);
        
        
        
        $this->assign(bcid,0);//顶级栏目 
        $this->assign(arr_0,$this->falls($res,0));
        $this->assign(arr_1,$this->falls($res,1));
        $this->assign(arr_2,$this->falls($res,2));
        $this->assign(arr_3,$this->falls($res,3));
        $this->assign(ishome,home);
        $this->display();
    }
    
    public function falls($arr,$v){
        foreach($arr as $k=>$val){
            for($i=0;$i<4;$i++){
                $a = $i*4+$v;
                if($k==$a){
                    $item[] = $val;    
                }    
            }
        }
        return $item;
        
    }
    
 
}

 

php瀑布流,把一个数组分4个数组,按照时间排序,布布扣,bubuko.com

php瀑布流,把一个数组分4个数组,按照时间排序

标签:des   style   blog   color   os   数据   io   2014   

原文地址:http://www.cnblogs.com/wesky/p/3868787.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!