PHP包装每4个元素

问题描述 投票:3回答:3

我有这个PHP代码包装div class="row"中的每4个元素。

我的代码是这样的:

$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>';

这有效,但显然最终会增加额外的div class="row"。我该如何防止这种情况?

UPDATE

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

$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>';
}

不知道为什么但是使用xml,这段代码会添加一个额外的空div。这仅在xml节点数为4的倍数时才会出现。

样品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>

请帮忙。谢谢

php xml loops
3个回答
2
投票

有很多方法可以做到这一点。如果没有表演问题,这将是我的方法。

<?php
$items = [1,2,3,4,5,6];
function itemsToRow($items) {
    $rows = [];
    $itemIndex = 0;
    foreach ($items as $item) {
        $rowIndex = (int) ($itemIndex / 4);
        $rows[$rowIndex][] = $item;
        $itemIndex++;
    }
    return $rows;
}

这将为您提供一个数组,其中包含显示它所需的结构。然后渲染它

foreach(itemsToRow($items) as $rowItems) {
    echo '<div class="row">';
    foreach($rowItems as $item) {
      echo $item;
    }
    echo '</div>';
}
?>

你可以在这里找到一个有效的例子https://eval.in/919896

有什么好处?您不必在循环内部或外部处理特殊情况。

这降低了代码复杂性并增强了可读性。


2
投票

问题是echo在循环外部,你可以添加一个条件,避免在最后一个元素上添加一个新的div,如下所示:

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

  // some code here

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

0
投票

得到它的工作。

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

$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>';
    }
}

我的错误在于这一行:if($counter%4 == 0 && $counter < count($items)) {。把它变成了if($counter%4 == 0 && $counter < $per_page) {,现在它的工作很棒。一个愚蠢的错误。

Tks每个人=)

© www.soinside.com 2019 - 2024. All rights reserved.