[英]Not getting expected results through foreach loop
我有以下表格結構
Updates
+----------------------------------------------------------------------------+
| Id | name | category | link | artist | home |
+---------------------------------------------------------------------------+
| 1 | song_1 | single | #### | artist_1 | 1 |
+-------+------------+----------------+-------------+--------------+
| 2 | song_2 | bollywood | #### | artist_2 | 1 |
+-------+------------+----------------+-------------+--------------+
| 3 | song_3 | single | #### | artist_3 | 1 |
+-------+------------+----------------+-------------+--------------+
| 4 | song_4 | bollywood | #### | artist_4 | 1 |
+------------------------------------------------------------------+
我獲取結果的代碼。
<?php $update_list = $db->query('select * from `update` where home = 1 order by id desc LIMIT 0, 10');
foreach($update_list as $field => $value)
{
print_r($value['category']' - ');
if($value['link'] != '') { print_r('<a href="'.$value['link'].'" class="new5_text">'.$value['name'].'</a> - - ');
}
if($value['singers'] != '') {
print_r('['.$value['singers'].']');
} ?>
我得到了以下結果
single - song_1- [artist_1]
bollywood - song_2 - [artist_2]
single - song_3 - [artist_3]
bollywood - song_4 - [artist_4]
通過上面的單個查詢,我想要這樣的結果。
single - song_1 - [artist_1] > song_3 - [artist_3]
bollywood - song_2 - [artist_2] > song_4 - [artist_4]
無法弄清楚如何編碼。 請幫幫我,給我一個php函數在這里使用的方法。
如果有幫助,請嘗試以下代碼,
<?php
$update_list = $db->query('select * from `update` where home = 1 order by category desc, id desc LIMIT 0, 10');
$currentcategory = false;
foreach($update_list as $field => $value) {
if($currentcategory != $value['category']) {
if($currentcategory != false) {
echo '<br>';
}
echo $value['category'].' - ';
$currentcategory = $value['category'];
}
else {
echo ' > ';
}
if($value['link'] != '') {
echo '<a href="'.$value['link'].'" class="new5_text">'.$value['name'].'</a> - ';
}
if($value['singers'] != '') {
echo '['.$value['singers'].']';
}
} ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.