php - 如何在PHP中动态放置div

标签 php html css

我有一个动态数组如下:

Array
(
[0] => stdClass Object
    (
        [student_id] => 8
        [student_name] => A
        [student_class] => 2
        [student_status] => 1
        [class_id] => 2
        [class_name] => C1
        [class_status] => 1
    )

[1] => stdClass Object
    (
        [student_id] => 10
        [student_name] => B
        [student_class] => 2
        [student_status] => 1
        [class_id] => 2
        [class_name] => C1
        [class_status] => 1
    )

[2] => stdClass Object
    (
        [student_id] => 9
        [student_name] => C
        [student_class] => 2
        [student_status] => 1
        [class_id] => 2
        [class_name] => C1
        [class_status] => 1
    )

[3] => stdClass Object
    (
        [student_id] => 11
        [student_name] => D
        [student_class] => 2
        [student_status] => 1
        [class_id] => 2
        [class_name] => C1
        [class_status] => 1
    )

[4] => stdClass Object
    (
        [student_id] => 4
        [student_name] => E
        [student_class] => 1
        [student_status] => 1
        [class_id] => 1
        [class_name] => C2
        [class_status] => 1
    )

[5] => stdClass Object
    (
        [student_id] => 6
        [student_name] => F
        [student_class] => 1
        [student_status] => 1
        [class_id] => 1
        [class_name] => C2
        [class_status] => 1
    )

[6] => stdClass Object
    (
        [student_id] => 5
        [student_name] => G
        [student_class] => 1
        [student_status] => 1
        [class_id] => 1
        [class_name] => C2
        [class_status] => 1
    )

[7] => stdClass Object
    (
        [student_id] => 7
        [student_name] => H
        [student_class] => 1
        [student_status] => 1
        [class_id] => 1
        [class_name] => C2
        [class_status] => 1
    )

[8] => stdClass Object
    (
        [student_id] => 7
        [student_name] => I
        [student_class] => 1
        [student_status] => 1
        [class_id] => 1
        [class_name] => C2
        [class_status] => 1
    )

[9] => stdClass Object
    (
        [student_id] => 7
        [student_name] => J
        [student_class] => 1
        [student_status] => 1
        [class_id] => 1
        [class_name] => C2
        [class_status] => 1
    )

)

我想要的是,使用PHP格式化如下

<div class="main_wrap">
    <h2>C1</h2>
    <div class="inner_wrap">
        <p>
            <span>A</span>
        </p>
        <p>
            <span>B</span>
        </p>
        <p>
            <span>C</span>
        </p>
    </div>
    <div class="inner_wrap">
        <p>
            <span>D</span>
        </p>
    </div>
</div> 

<div class="main_wrap">
    <h2>C2</h2>
    <div class="inner_wrap">
        <p>
            <span>E</span>
        </p>
        <p> 
            <span>F</span>
        </p>
        <p> 
            <span>G</span>
        </p>
    </div>
    <div class="inner_wrap">
        <p>
            <span>H</span>
        </p>
        <p>
            <span>I</span>
        </p>
        <p>
            <span>J</span>
        </p>
    </div>
</div>

我知道如何使用 foreach 循环它,但不知道如何使用 div 动态包装。

Fiddle

最佳答案

看看这是否是您要找的。我的解决方案使用存储数组,array_chunk() 将子数组分成 3 组,然后 implode() 创建 html:

// Storage array
$sections   =   array();
// Loop through your large array (I am calling it "all")
foreach($all as $rows) {
        // Store the class title
        $cName              =   $rows->class_name;
        // Store name (and whatever else you want)
        // You could store the key for the original object
        // so you can reference back on the next loop below
        $sections[$cName][] =   $rows->student_name;
    }
// Loop through the store
foreach($sections as $key => $values) {
        // Chunk down to whatever grouping you want (3 is what you mention)
        $grp    =   array_chunk($values,3);
        // Use implode for the chunks
        echo '<div class="main_wrap">'.PHP_EOL.'<h2>'.$key.'</h2>'.PHP_EOL;
        foreach($grp as $values) {
                echo '<div class="inner_wrap">'.PHP_EOL;
                echo "\t<p>".PHP_EOL."\t\t<span>".implode("</span>".PHP_EOL."\t</p>".PHP_EOL."\t<p>".PHP_EOL."\t\t<span>",$values)."</span>".PHP_EOL."\t</p>".PHP_EOL."</div>".PHP_EOL;
            }
        echo '</div>'.PHP_EOL;
    }

应该给你(或者至少它给了我):

<div class="main_wrap">
<h2>C1</h2>
<div class="inner_wrap">
    <p>
        <span>A</span>
    </p>
    <p>
        <span>B</span>
    </p>
    <p>
        <span>C</span>
    </p>
</div>
<div class="inner_wrap">
    <p>
        <span>D</span>
    </p>
</div>
</div>
<div class="main_wrap">
<h2>C2</h2>
<div class="inner_wrap">
    <p>
        <span>E</span>
    </p>
    <p>
        <span>F</span>
    </p>
    <p>
        <span>G</span>
    </p>
</div>
<div class="inner_wrap">
    <p>
        <span>H</span>
    </p>
    <p>
        <span>I</span>
    </p>
    <p>
        <span>J</span>
    </p>
</div>
</div>

请注意,您可以随意设置格式,但它应该是相同的布局,但在制表符方面您必须添加一对。

ALSO NOTE: I have not done any checks (like empty()) so if the object is not filled or what-have-you, there will be a foreach() warning.

关于php - 如何在PHP中动态放置div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33950947/

相关文章:

php - 在 php 中读取然后覆盖文件内容的最佳方法是什么?

php - 如何通过一个独特的php文件下载多个图像?

css - 在 :hover in IE6 - nothing happening 上显示隐藏的 div

jquery - CSS标准化文本背景

php - 如何根据时区设置WP Cron作业时间戳?

php - 从 PHP 创建新的 Apache 配置文件

javascript - 在 React 中更改 <img> 标签上的 src 时图像加载滞后

javascript - 从链接打开时,导航栏隐藏标题

css - 在 CSS 中缩放一种字体

css - 如何使 float 内部div与最高div的高度相同