在一个foreach中创建3个div

时间:2015-04-10 18:09:55

标签: php

好的我正在使用codeIgniter从数据库中获取结果

class Video extends CI_Model{

public function getVideo()
{
    $this->db->select('*');
    $this->db->from('video');
   // $this->db->where('category',$category);
    return $this->db->get();
}


}

我试图以这种方式显示数据库的结果

<div>
first 10 results
</div>
<div>
 second 10 results
</div
<div>
rest from query
</div>

有人可以用逻辑来帮助我

foreach($results as r)
{
   echo '<div>'.$r->video.'</div>';
}

2 个答案:

答案 0 :(得分:0)

这样的东西?

$divisions = array(10,20);
for($i=0;$i<count($results);$i++){
    if($i===0){
        echo "<div>";
    }else if(in_array($i,$divisions){
        echo "</div><div>";
    }else if($i===count($results)-1){
        echo "</div>";
    }
    echo "<div>".$results[$i]->video."</div>";
}

答案 1 :(得分:-1)

你走了。经过测试和工作。

<?php

//let's make a fake results array, just to show it works.
$results = array();


$c = 0;
while ($c < 100) {
        array_push($results, array('video' => 'a'));
        $c++;
}


//now for the actual answer.
$count = 0;
foreach ($results as $r) {
        if ($count === 0) {
                echo '<div>';
        }

        echo $r['video'] . ' ';

        if ($count === 9 || $count === 19) {
                echo '</div><div>';
        }

        $count++;
}

echo '</div>';

?>