使用foreach按重复值显示星标

时间:2014-02-02 03:19:09

标签: php mysql sql

我想从列出各种国家符号(旗帜,鲜花等)的表格中显示国家鸟类列表,其中字段符号包括特定符号的名称(例如褐鹈鹕)。我还想在最受欢迎的国家鸟类旁边放上星号或星图。例如,安第斯秃鹰可能会获得一颗金星,因为它代表三个或四个国家,在表格中有几行......

IDArea | Symbol
Ecuador | Andean condor
Bolivia | Andean condor
Peru | Andean condor

我有一个应该这样做的脚本,但它无法正常工作,可能是因为我做得不对。

当我将其粘贴到phpMyAdmin>中时,我的查询工作正常SQL ...

SELECT GS.N, GS.IDArea SymArea, GS.URL, GS.PostURL, GS.Unique, GS.Title,
 GS.MyKind2, GS.Symbol, GS.Latin, GS.Desig, GS.DesigGen, GS.DesigGroup, GS.Date, G4.Common,   G4.Latin,
 GG.N, GG.IDArea, GG.Name GName, GG.Type, GG.IDParent, GG.IDParent2, GG.Parent, GG.Parent2,  GG.IDReg
 FROM gs AS GS
 LEFT JOIN gs2_latin AS G4 ON G4.Common = GS.Symbol
 LEFT JOIN gw_geog AS GG ON GG.IDArea = GS.IDArea
 WHERE GS.DesigGen = 'bird' AND GG.Type = 'nat' AND GS.Symbol != ''
 OR GS.DesigGen = 'bird' AND GG.Type = 'dep' AND GS.Symbol != ''
 GROUP BY GS.IDArea ORDER BY GS.N

但是当我添加“Popular Birds”脚本时,它无法正确显示。根据我修改代码的方式,它可能会显示每行两次(或三次或四次),或者显示代表两个国家的鸟的两行,但是其中一行中出现两次青铜星,不管是一次在每一行。

我认为问题出在这里:

$Symbol = str_replace('_', ' ', $row['Symbol']); 
$Symbol = $Symbol.$RefStar;
$Symbol = str_replace($Symbol, '<tr><td>'.$Symbol.'</td></tr>', $Symbol); 

我不确定我是否应该添加数组括号,将$ Symbol更改为$ Symbol []或者我应该放置括号。我没有尝试过任何工作。

以下是整个脚本:

echo '<table>
<thead><tr><th>Country</th><th>Bird</th></tr></thead>
<tbody>';
$data = array();
$symbols = array(); 
while ($row = $stm->fetch())
{
$data[] = $row; 
$symbols[] = $row['Symbol']; 

//Get counts for each symbol with symbol name as key 
$counts = array_count_values($symbols); 

foreach($data as $k => $row){ 

$cnt = $counts[$row['Symbol']]; 

if($cnt > 3){ 
    $RefStar = 'Gold'; 
}elseif($cnt == 3){ 
    $RefStar = 'Silver'; 
}elseif($cnt == 2){ 
    $RefStar = 'Bronze'; 
}elseif($cnt < 2){ 
    $RefStar = ""; 
} 

 $Symbol = str_replace('_', ' ', $row['Symbol']); 
$Symbol = $Symbol.$RefStar;
 $Symbol = str_replace($Symbol, '<tr><td>'.$Symbol.'</td></tr>', $Symbol); 
 echo $Symbol;
} // END FOREACH
} // END WHILE LOOP

echo '</tbody>
</table>';

修改

根据这些数据库值,四个表行应以“安第斯秃鹰”为特色,后面跟一个金星,而两行应显示“谷仓燕子”,然后是青铜星。

IDArea | Symbol
Colombia | Andean condor
Ecuador | Andean condor
Bolivia | Andean condor
Peru | Andean condor
Austria | barn swallow
Estonia | barn swallow

注意:字段IDArea实际上包含国家CODES(例如玻利维亚的bol)。我的查询连接到名为gw_geog的表,该表提供实际的国家/地区名称。表格gs2_latin只是一个包含所有可能的通用名称的科学名称列表......

Common | Latin
bison | Bison_bison
American bison | Bison_bison
American buffalo | Bison_bison

这是SHOW CREATE TABLE gs ...

的结果
CREATE TABLE `gs` (
 `N` mediumint(4) NOT NULL AUTO_INCREMENT,
 `IDArea` varchar(7) COLLATE latin1_general_ci NOT NULL DEFAULT '',
 `URL` varchar(75) COLLATE latin1_general_ci NOT NULL DEFAULT '',
 `PostURL` varchar(44) COLLATE latin1_general_ci DEFAULT NULL,
 `Unique` char(20) COLLATE latin1_general_ci DEFAULT NULL,
 `Title` varchar(75) COLLATE latin1_general_ci DEFAULT NULL,
 `Subtitle` varchar(100) COLLATE latin1_general_ci DEFAULT NULL,
 `MyKind2` tinyint(1) NOT NULL DEFAULT '0',
 `Symbol` varchar(100) COLLATE latin1_general_ci DEFAULT NULL,
 `Latin` varchar(50) COLLATE latin1_general_ci DEFAULT NULL,
 `Desig` varchar(255) COLLATE latin1_general_ci DEFAULT NULL,
 `DesigGen` char(25) COLLATE latin1_general_ci DEFAULT NULL,
 `DesigGroup` varchar(7) COLLATE latin1_general_ci NOT NULL,
 `Date` char(4) COLLATE latin1_general_ci DEFAULT NULL,
 `Dupe` tinyint(1) DEFAULT NULL,
 `G1` tinyint(1) NOT NULL DEFAULT '1',
 PRIMARY KEY (`N`),
 KEY `IDArea` (`IDArea`),
 KEY `URL` (`URL`),
 KEY `Title` (`Title`)
) ENGINE=MyISAM AUTO_INCREMENT=4896 DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci

0 个答案:

没有答案