我试图更改MySQL结果的顺序,具体取决于PHP
是否符合duplicates
条件。
我的代码检查是否有任何行Frequency
并更改行类(如果是)。
我想要实现的是,如果存在重复,则Name
会对MySQL查询进行排序,如果找不到重复项,则按function getFrequencies() {
$conn = getConnected("websiteData");
$frequencyQuery = "
SELECT
f.Name,
f.Frequency,
f.Country,
f.Programmed,
(SELECT count(*) FROM frequencies WHERE Frequency = f.Frequency) as count
FROM frequencies f;
";
$frequencyResult = mysqli_query($conn, $frequencyQuery);
while ($frequencyRow = mysqli_fetch_assoc($frequencyResult)) {
if($frequencyRow['count'] > 1) {
$frequencyQuery = "
SELECT
f.Name,
f.Frequency,
f.Country,
f.Programmed,
(SELECT count(*) FROM frequencies WHERE Frequency = f.Frequency) as count
FROM frequencies f
ORDER BY Frequency;
";
} else {
$frequencyQuery = "
SELECT
Name,
Frequency,
Country,
Programmed
FROM frequencies
ORDER BY Name;
";
}
}
$frequencyResult = mysqli_query($conn, $frequencyQuery);
while ($frequencyRow = mysqli_fetch_assoc($frequencyResult)){
if ($frequencyRow['count'] > 1) {
echo '<tr class="warning">' .
"<td>" . $frequencyRow['Name'] . "</td>" .
"<td>" . $frequencyRow['Frequency'] . "</td>" .
"<td>" . $frequencyRow['Country'] . "</td>" .
"<td>" . $frequencyRow['Programmed'] . "</td>" .
"</tr>" . PHP_EOL;
} else {
echo "<tr>" . PHP_EOL .
"<td>" . $frequencyRow['Name'] . "</td>" .
"<td>" . $frequencyRow['Frequency'] . "</td>" .
"<td>" . $frequencyRow['Country'] . "</td>" .
"<td>" . $frequencyRow['Programmed'] . "</td>" .
"</tr>" . PHP_EOL;
}
}
}
排序。
这就是我所拥有的:
Name
现在我知道我的表包含重复项,但目前没有突出显示的行,订单是function getFrequencies() {
$conn = getConnected("websiteData");
// $frequencyQuery = "SELECT Name, Frequency, Country, Programmed, count(*) FROM frequencies GROUP BY Frequency ORDER BY Name";
$frequencyQuery = "
SELECT
f.Name,
f.Frequency,
f.Country,
f.Programmed,
( SELECT count(*) FROM frequencies WHERE Frequency = f.Frequency ) as count
FROM frequencies f
ORDER BY Frequency;
";
$frequencyResult = mysqli_query($conn, $frequencyQuery);
while ($frequencyRow = mysqli_fetch_assoc($frequencyResult)) {
if ($frequencyRow['count'] > 1) {
echo '<tr class="danger">' . 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;
} else {
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;
}
}
}
。
在我将代码更改为上面提到的代码之前,这个代码确实突出了我的重复行:
Frequency
我能做些什么来实现我想要做的事情?我想要更改顺序的原因是因为当没有重复时,优先按字母顺序滚动列表,但如果有重复项,则按find
排序将重复项放在另一行之后易于edit/delete
和<!DOCTYPE html>
<html>
<head>
<style>
/* Popup container - can be anything you want */
.popup {
position: relative;
display: inline-block;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* The actual popup */
.popup .popuptext {
visibility: hidden;
width: 160px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px 0;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -80px;
}
/* Popup arrow */
.popup .popuptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
/* Toggle this class - hide and show the popup */
.popup .show {
visibility: visible;
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s;
}
/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
from {opacity: 0;}
to {opacity: 1;}
}
@keyframes fadeIn {
from {opacity: 0;}
to {opacity:1 ;}
}
</style>
</head>
<body style="text-align:center">
<h2>Popup</h2>
<div class="popup" onclick="myFunction()">Click me to toggle the popup!
<span class="popuptext" id="myPopup">A Simple Popup!</span>
</div>
<script>
// When the user clicks on div, open the popup
function myFunction() {
var popup = document.getElementById("myPopup");
popup.classList.toggle("show");
}
</script>
</body>
</html>
。
答案 0 :(得分:0)
根据你要做的事情,我会构建一个结果的关联数组,然后使用你的逻辑来使用PHP而不是MySQL的order by
子句对它们进行排序。
您可以采用的方法(如果要保留所有重复项)是通过Name
key
对数组进行排序然后说if unique display
{{1 }} else
这将导致所有重复项连续显示,整个事件按字母顺序排序,您可以使用for each duplicate in array display
。
为了阻止这种情况触发副作用,您可以构建两个数组,一个来自MySQL asort
,另一个是使用select
的唯一数组,当您遍历数组以显示页面时你可以遍历唯一数组,只在渲染时从原始数组中获取重复元素。
请考虑以下事项:
array_unique
如果您不想保留重复项,则可以完全按照相同的公式进行操作,但在匹配副本而不是显示它们时,您可以从数据库中清除它们中的每一个,只保留一个,然后从唯一数组中呈现项目。