如何删除此代码段中的重复项。查询中的Distinct函数将不起作用,因为您将在每次循环中读取每个单词并将其添加到表中。如何在我的表中显示所有不同的数据
<?php
$connect = mysqli_connect("localhost", "root", "", "dbProject");
$output = '';
if(isset($_POST["query"]))
{
$search = mysqli_real_escape_string($connect, $_POST["query"]);
$SearchEach= explode(" ",$search);
$output .= '<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>Book ID</th>
<th>Name</th>
<th>Author</th>
<th>Tags</th>
<th>Status</th>
</tr>';
foreach($SearchEach as $value){
$query = "
SELECT * FROM tblProject
WHERE tyear >=YEAR(NOW())-30 AND (tags LIKE '%".$value."%'OR
tcname LIKE '%".$value."%' OR
author LIKE '%".$value."%' OR
status LIKE '%".$value."%')";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{
$output .= '<tr>
<td>'.$row["tcno"].'</td>
<td>'.$row["tcname"].'</td>
<td>'.$row["Author"].'</td>
<td>'.$row["tags"].'</td>
<td>'.$row["status"].'</td>
</tr>';
}
}
echo $output;
}
?>发布于 2018-08-30 21:57:12
假设您的tcno字段值是唯一的,您可以使用以下逻辑来不显示已显示的记录
$processedRecrods = [];
foreach ($SearchEach as $value) {
$query = "
SELECT * FROM tblProject
WHERE tyear >=YEAR(NOW())-30 AND (tags LIKE '%" . $value . "%'OR
tcname LIKE '%" . $value . "%' OR
author LIKE '%" . $value . "%' OR
status LIKE '%" . $value . "%')";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_array($result)) {
//See if record with this tcno is already processed / displayed in table
if (!in_array($row['tcno'], $processedRecrods)) {
$output .= '<tr>
<td>' . $row["tcno"] . '</td>
<td>' . $row["tcname"] . '</td>
<td>' . $row["Author"] . '</td>
<td>' . $row["tags"] . '</td>
<td>' . $row["status"] . '</td>
</tr>';
//Add tcno in the array of processed records
$processedRecrods[] = $row["tcno"];
}
}
}
echo $output;发布于 2018-08-30 21:52:10
首先只存储在各种搜索过程中找到的行的ID。然后,删除重复的ID。然后,根据唯一ID检索并显示相关行。
发布于 2018-08-30 21:57:48
我会将结果推送到一个新的堆栈中,并使用array_unique。
<?php
$newStack = [];
while ($row = mysqli_fetch_array($result)) {
array_push($newStack, $row);
}
$newStack = array_unique($newStack);
foreach($newStack as $row) {
$output .= "my html";
}https://stackoverflow.com/questions/52098555
复制相似问题