1 回答

TA貢獻1848條經驗 獲得超2個贊
你絕對應該為你的項目使用一些模板引擎,因為隨著時間的推移它會變得更加復雜,并且混合 PHP 和 HTML 會造成更大的混亂。
我的想法是根據需要獲取數據結構,然后迭代兩個數組。使用模板引擎,它看起來會比我的代碼更漂亮。
<?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 關注
- 107 瀏覽
添加回答
舉報