PHP 每 4 个元素换行 [英] PHP Wrap every 4 elements

查看:38
本文介绍了PHP 每 4 个元素换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 php 代码,它把每 4 个元素包装在一个 div class="row" 中.

我的代码是这样的:

$counter = 1;echo '<div class="row">';foreach($items 作为 $item) {//这里有一些代码if($counter % 4 == 0) {echo '</div><div class="row">';}$计数器++}回声'</div>';

这有效,但显然在最后放置了一个额外的 div class="row".我如何防止这种情况发生?

更新

好的,设法找到一些东西.这就是我所在的地方.

$xml = simplexml_load_file('xml/list.xml');$count = count($xml);$计数器= 1;if(file_exists('xml/list.xml')) {echo '<div class="row">';foreach($xml-> 作为 $item) {回声 $counter;if($counter % 4 == 0) {echo '</div><div class="row">';}$计数器++;}回声'</div>';}

不知道为什么,但是对于 xml,此代码添加了一个额外的空 div.仅当 xml 节点数是 4 的倍数时才会出现这种情况.

示例 Xml

<work id="1"><title>此处为title</title><描述>这里的描述</description><img>图像名称</img><url>url</url></工作><work id="2"><title>此处为title</title><描述>这里的描述</description><img>图像名称</img><url>url</url></工作><work id="3"><title>此处为title</title><描述>这里的描述</description><img>图像名称</img><url>url</url></工作><work id="4"><title>此处为title</title><描述>这里的描述</description><img>图像名称</img><url>url</url></工作></list>

请帮忙.谢谢

解决方案

开始工作.

如果有人正在寻找使用 php 和 xml 进行 php 分页的代码,那么这里是:

$file = 'xml/list.xml';如果(文件存在($文件)){$xml = simplexml_load_file($file);$total = count($xml);$计数器= 0;$per_page = 12;$nPages = ceil($total/$per_page);$offset = ($page-1)*$per_page;$curr = 0;//xml 到数组$list = 数组();foreach($xml 作为 $item) {$list[] = $item;}//显示记录echo '

';foreach(array_reverse($list) as $work) {//每页只显示一些项目$curr++;if($curr > $offset && $curr <= $per_page*$page) {//内容在这里echo '这里有一些代码......';//每四个项目换行$计数器++;if($counter%4 == 0 && $counter < $per_page) {echo '</div><div class="work-row row">';}}}回声'</div>';//分页if($nPages > 1) {echo '<div class="row"><div class="col-md-12 text-right"><ul>';for($i=1; $i<=$nPages; $i++) {if($page == $i) {echo '<li><a href="page/'.$i.'" class="current">'.$i.'</a></li>';}别的 {echo '<li><a href="page/'.$i.'">'.$i.'</a></li>';}}回声'</ul></div>';}}

我的错误在这一行:if($counter%4 == 0 && $counter < count($items)) {.将其更改为 if($counter%4 == 0 && $counter < $per_page) { 现在它工作得很好.一个愚蠢的错误.

谢谢大家 =)

I have this php code that wraps every 4 elements in a div class="row".

My code is something like this:

$counter = 1;
echo '<div class="row">';
foreach($items as $item) {

  // some code here

    if($counter % 4 == 0) {
       echo '</div><div class="row">';
    }
    $counter++
}
echo '</div>';

This works but apparently puts an extra div class="row" in the end. How do I prevent this?

UPDATE

Ok, manage to find something. this is where i am.

$xml = simplexml_load_file('xml/list.xml');
$count = count($xml);
$counter = 1;

if(file_exists('xml/list.xml')) {
    echo '<div class="row">';
    foreach($xml->work as $item) {
        echo $counter;
        if($counter % 4 == 0) {
           echo '</div><div class="row">';
        }
        $counter++;
    }
    echo '</div>';
}

Dont know why but with the xml, this code puts an extra empty div. This only appears to happen when the number of xml nodes is multiple of 4.

Sample Xml

<list>
    <work id="1">
        <title>title here</title>
        <description>description here</description>
        <img>image name</img>
        <url>url</url>
    </work>
    <work id="2">
        <title>title here</title>
        <description>description here</description>
        <img>image name</img>
        <url>url</url>
    </work>
    <work id="3">
        <title>title here</title>
        <description>description here</description>
        <img>image name</img>
        <url>url</url>
    </work>
    <work id="4">
        <title>title here</title>
        <description>description here</description>
        <img>image name</img>
        <url>url</url>
    </work>
</list>

Please help. Thanks

解决方案

Got it to work.

if anyone looking for the code to make php pagination with php and xml here it goes:

$file = 'xml/list.xml';

if(file_exists($file)) {
    $xml = simplexml_load_file($file);
    $total = count($xml);
    $counter = 0;
    $per_page = 12;
    $nPages = ceil($total/$per_page);
    $offset = ($page-1)*$per_page;
    $curr = 0;

    // xml into array
    $list = array();
    foreach($xml as $item) {
        $list[] = $item;
    }

    // displays records
    echo '<div class="work-row row">';
        foreach(array_reverse($list) as $work) {

        // show only some items per page
        $curr++;
        if($curr > $offset && $curr <= $per_page*$page) {
            // content goes here
            echo 'some code in here...';

            // every four items wrap in row
            $counter++;
            if($counter%4 == 0 && $counter < $per_page) {
                echo '</div><div class="work-row row">';
            }
        }
                    }
    echo '</div>';

    // pagination
    if($nPages > 1) {
        echo '<div class="row"><div class="col-md-12 text-right"><ul>';
        for($i=1; $i<=$nPages; $i++) {
            if($page == $i) {
                echo '<li><a href="page/'.$i.'" class="current">'.$i.'</a></li>';
            }
            else {
                echo '<li><a href="page/'.$i.'">'.$i.'</a></li>';
            }
        }
        echo '</ul></div>';
    }
}

my mistake was in this line : if($counter%4 == 0 && $counter < count($items)) {. changed it to if($counter%4 == 0 && $counter < $per_page) { and now its working great. A stupid mistake.

Tks everyone =)

这篇关于PHP 每 4 个元素换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆