1 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊
你絕對(duì)應(yīng)該為你的項(xiàng)目使用一些模板引擎,因?yàn)殡S著時(shí)間的推移它會(huì)變得更加復(fù)雜,并且混合 PHP 和 HTML 會(huì)造成更大的混亂。
我的想法是根據(jù)需要獲取數(shù)據(jù)結(jié)構(gòu),然后迭代兩個(gè)數(shù)組。使用模板引擎,它看起來(lái)會(huì)比我的代碼更漂亮。
<?php
$generations = [];
foreach ($all_model as $value) {
if (!isset($generations[$value['generation']])) {
$generations[$value['generation']] = [];
}
$generations[$value['generation']][] = $value;
}
foreach ($generations as $generation => $model): ?>
<div class="col-lg-3">
<div class="crand-car-container">
<?php echo "Generation:" . $generation; ?>
<?php foreach ($model as $value): ?>
<a href="/<?php echo $value['mark_url'] ?>/<?php echo $value['model_url'] ?>/<?php echo $value['generation_url'] ?>/<?php echo $value['body_car_url'] ?>"><?php echo $value['mark'] . " " . $value['model'] . " " . $value['year_production'] . " [" . $value['generation'] . "]" ?></a>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
- 1 回答
- 0 關(guān)注
- 117 瀏覽
添加回答
舉報(bào)