PHP突出显示来自MySQL的重复值的表行

时间:2017-03-12 04:09:59

标签: php mysql

我已经搜索过高低,以获得答案甚至是如何做到这一点的教程。

我有一个包含一些数据的表。 数据显示在HTML表格中。

我想要做的是突出显示具有重复值的行。

下面是我表格中的实际数据。其中一些包含重复。

Alpha Tag   Frequency   Country Usage   Programmed
WILD 3  151.50500   USA No
WILD 2  151.51500   USA No
PKALT1  151.57000   USA No
QAP 151.59500   USA No
LKJ 2   151.68500   USA No
WILD 4  151.74000   USA No
SUMMIT  151.77500   USA No
ALYE 6  151.80500   USA No
PKALT2  151.82000   USA No
MURS 151.82 151.82000   USA No
ALYE 5  151.83500   USA No
PNALT1  151.88000   USA No
MURS 151.88 151.88000   USA No
H ROAD  151.92500   USA No
PNALT2  151.94000   USA No
MURS 151.94 151.94000   USA No
MBCON2  151.95500   USA No
ALYE 4  152.30000   USA No
WBI K   152.91500   USA No
MBCON1  152.93000   USA No
WILD 1  152.99000   USA No
ALCHWY  153.03500   USA No
ALYE 7  153.05000   USA No
WBI A2  153.15500   USA No
AMB 2   154.05000   USA No
LADD 1  154.10000   CA  No
AK RL2  154.31500   USA No
LADD 3  154.32500   CA  No
AMB 1   154.38500   USA No
WLD EQ  154.54000   USA No
MURS 154.57 - Blue Dot  154.57000   USA No
MURS 154.60 - Green Dot 154.60000   USA No
B SHIT  154.60000   USA No
KPD 154.72500   USA No
FPD 155.01000   USA No
AROW 1  155.19000   USA No
WPD 155.37000   USA No
MOA 155.82000   USA No
DOT 2   156.12000   USA No
ALYE 1  157.43000   USA No
LKJ 1   157.66500   USA No
ALYE 2  158.28000   USA No
ALYE 3  158.33500   USA No
LADD 2  158.94000   CA  No
DOT 1   159.13000   USA No
LTIHNS  159.63000   USA No
LTIFBX  159.84000   USA No
AK WST  159.90000   USA No
LTIPBY  159.96000   USA No
C CAKE  160.08000   USA No
SLEP 1  160.09500   USA No
WBI A1  160.20000   USA No
AROW 2  162.27000   USA No
AK RL1  164.62500   USA No
SLEP 2  170.50000   USA No
LADD 4  173.37000   CA  No

我阅读MySQL的PHP​​函数是:

function getFrequencies() {
$conn = getConnected("websiteData");
$frequencyQuery = "SELECT * FROM frequencies ORDER BY Frequency";

$frequencyResult = mysqli_query($conn, $frequencyQuery);

while ($frequencyRow = mysqli_fetch_assoc($frequencyResult))
{ 
   echo "<tr>" . PHP_EOL . 
    "<td>" . $frequencyRow['Name'] . "</td>" . PHP_EOL . 
    "<td>" . $frequencyRow['Frequency'] . "</td>" . PHP_EOL . 
    "<td>" . $frequencyRow['Country'] . "</td>" . PHP_EOL . 
    "<td>" . $frequencyRow['Programmed'] . "</td>" . PHP_EOL . 
    "</tr>" . PHP_EOL;
}
}

我知道这不是一个不用自己尝试来构建代码的网站,但事实是我不知道从哪里开始。我不是要求有人为我这样做100%,但我正在寻求指导。

我已经看到使用MySQL JOIN的大量答案,但我不希望这样做。我只是想突出显示那些特定的行,以便我可以进入并更改记录或删除它们。

我确实找到了这个Q&amp; A但它让我头疼,因为我不明白接受的答案如何发挥作用:How to highlight rows if they contain duplicate data?

此外,我只想检查两列重复项。 Alpha Tag列和Frequency列。

既然答案已经指明了我正确的方向,这是我迄今为止没有运气的尝试,但这是一个开始:

function getFrequencies() {
$conn = getConnected("websiteData");
 //$frequencyQuery = "SELECT Name, Frequency, Country, Programmed, count(*) FROM frequencies GROUP BY Frequency ORDER BY Name";
 $frequencyQuery = "SELECT Name, Frequency, Country, Programmed, count(*) FROM frequencies GROUP BY Frequency ORDER BY Name";
$frequencyResult = mysqli_query($conn, $frequencyQuery);

while ($frequencyRow = mysqli_fetch_assoc($frequencyResult))
{ 
   if ($frequencyRow['count'] > 1) { echo '<tr class="danger">' . PHP_EOL; } else { echo '<tr>' . PHP_EOL; }
    echo "<td>" . $frequencyRow['Name'] . "</td>" . PHP_EOL . 
    "<td>" . $frequencyRow['Frequency'] . "</td>" . PHP_EOL . 
    "<td>" . $frequencyRow['Country'] . "</td>" . PHP_EOL . 
    "<td>" . $frequencyRow['Programmed'] . "</td>" . PHP_EOL . 
    "</tr>" . PHP_EOL;
}
}

1 个答案:

答案 0 :(得分:1)

您可以像这样重写您的查询:

SELECT Name, Frequency, Country, Programmed, count(*) as count
FROM frequencies
GROUP BY Frequency;

然后在您的PHP while中,您可以突出显示重复项:

if ($frequencyRow['count'] > 1) {
    // there are duplicates
    // apply special css
} else {
    // one row only
}

<强>更新

编写查询以显示和保留重复项(无分组)的另一种方法:

SELECT f.Name, f.Frequency, f.Country, f.Programmed, 
   (
    SELECT count(*)
    FROM frequencies
    WHERE Frequency = f.Frequency
   ) as count
FROM frequencies f
;