首页 > 解决方案 > 多列搜索然后回显找到的特定列

问题描述

我的 MySQL 数据库对参与“事件”的车辆中的乘客姓名进行分组。最多 46 个名字/姓氏对可以在单个事件中分散在最多 5 辆车中(分组为车辆 1 中最多 2 个,车辆 2 中最多 2 个,车辆 3 中最多 4 个,车辆 4 中 8 个,车辆 5 中最多 30 个) . 我可以搜索所有 46 个名称,并为每个成功的搜索返回行 ID。然后,我想回显这些成功搜索的超链接列表,以便用户可以单击并查看整个记录;但是,为了让他们选择感兴趣的行,我需要显示导致他们的搜索匹配的名称对。现在,由于姓氏有 46 列,名字有 46 列,所以它只列出记录中的第一个非空名字/姓氏对。因此,它列出了车辆 1 中的乘客 1(无论是否匹配),而不是来自车辆 2 和 3 的乘客 4 和 8(与搜索匹配并且在同一记录中)。这些都在一个记录中,因为他们对他们都参与的事件有一个共同的叙述。

我已经成功地创建了搜索找到的所有记录的编号列表,并为事件的日期创建了一个超链接。然后,我尝试在该超链接之后回显其他列以识别所涉及的名称,以便用户知道哪个链接最有可能是感兴趣的链接。正如我所提到的,它只返回该记录中的第一个名称对,而不是搜索找到的名称对。当然,如果您单击超链接,您将获得该记录中的所有内容,但这比我的问题所在的页面晚。我认识到循环只遍历每条记录一次以创建行 ID,但我不知道该怎么做那部分,如果那是它必须被编辑的地方。或者在查询中,这是一个多行迭代:

WHERE ((firstname1_1 IS NOT NULL and firstname1_1 LIKE '%$firstname%') AND (lastname1_1 IS NOT NULL and lastname1_1 LIKE '%$lastname%'))
OR ((firstname2_1 IS NOT NULL and firstname2_1 LIKE '%$firstname%') AND (lastname2_1 IS NOT NULL and lastname2_1 LIKE '%$lastname%'))

这里有几行代码。在 List Link 部分下,应该有 46 个名称对分组在 5 辆车中,但我在这里截断了它。

// Loop the recordset $result

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

// Lists results with the date being a hyperlink

$strName = $row['day_value'] . " " . $row['month_value'] . " " . $row['year_value'];

// Create a link to loss.php with the id-value in the URL

$strLink = "<a href = 'loss.php?id=" . $row['id'] . "'>" . $strName . "</a>";

// List link; this outputs the first lastname of a record in the results

// array, rather than the lastname that was searched for, so that's the 

// problem.

print "<ol style='text-align: left;'>";

if(!empty('lastname1_1'))
echo "<li style='font-weight:bold;font-size:14; width: 50%; margin-left: auto; margin-right: auto;'>" . $strLink . ":  " . "{$row['title1_1']}" . "  ". "{$row['firstname1_1']}" . "  " . "{$row['lastname1_1']}," . "  " . "{$row['injury1_1']}" . "  " . "{$row['vehicle1']}" . "</li>";

elseif(!empty('lastname2_1'))
echo "<li style='font-weight:bold;font-size:14; width: 50%; margin-left: auto; margin-right: auto;'>" . $strLink . ":  " . "{$row['title2_1']}" . "  ". "{$row['firstname2_1']}" . "  " . "{$row['lastname2_1']}," . "  " . "{$row['injury2_1']}" . "  " . "{$row['vehicle1']}" . "</li>";

elseif(!empty('lastname1_2'))
echo "<li style='font-weight:bold;font-size:14; width: 50%; margin-left: auto; margin-right: auto;'>" . $strLink . ":  " . "{$row['title1_2']}" . "  ". "{$row['firstname1_2']}" . "  " . "{$row['lastname1_2']}," . "  " . "{$row['injury1_2']}" . "  " . "{$row['vehicle2']}" . "</li>";

elseif(!empty('lastname2_2'))
echo "<li style='font-weight:bold;font-size:14; width: 50%; margin-left: auto; margin-right: auto;'>" . $strLink . ":  " . "{$row['title2_2']}" . "  ". "{$row['firstname2_2']}" . "  " . "{$row['lastname2_2']}," . "  " . "{$row['injury2_2']}" . "  " . "{$row['vehicle2']}" . "</li>";

elseif(!empty('lastname1_3'))
echo "<li style='font-weight:bold;font-size:14; width: 50%; margin-left: auto; margin-right: auto;'>" . $strLink . ":  " . "{$row['title1_3']}" . "  ". "{$row['firstname1_3']}" . "  " . "{$row['lastname1_3']}," . "  " . "{$row['injury1_3']}" . "  " . "{$row['vehicle3']}" . "</li>";

elseif(!empty('lastname2_3'))
echo "<li style='font-weight:bold;font-size:14; width: 50%; margin-left: auto; margin-right: auto;'>" . $strLink . ":  " . "{$row['title2_3']}" . "  ". "{$row['firstname2_3']}" . "  " . "{$row['lastname2_3']}," . "  " . "{$row['injury2_3']}" . "  " . "{$row['vehicle3']}" . "</li>";

elseif(!empty('lastname3_3'))
echo "<li style='font-weight:bold;font-size:14; width: 50%; margin-left: auto; margin-right: auto;'>" . $strLink . ":  " . "{$row['title3_3']}" . "  ". "{$row['firstname3_3']}" . "  " . "{$row['lastname3_3']}," . "  " . "{$row['injury3_3']}" . "  " . "{$row['vehicle3']}" . "</li>";

elseif(!empty('lastname4_3'))
echo "<li style='font-weight:bold;font-size:14; width: 50%; margin-left: auto; margin-right: auto;'>" . $strLink . ":  " . "{$row['title4_3']}" . "  ". "{$row['firstname4_3']}" . "  " . "{$row['lastname4_3']}," . "  " . "{$row['injury4_3']}" . "  " . "{$row['vehicle3']}" . "</li>";

elseif(!empty('lastname1_4'))
echo "<li style='font-weight:bold;font-size:14; width: 50%; margin-left: auto; margin-right: auto;'>" . $strLink . ":  " . "{$row['title1_4']}" . "  ". "{$row['firstname1_4']}" . "  " . "{$row['lastname1_4']}," . "  " . "{$row['injury1_4']}" . "  " . "{$row['vehicle4']}" . "</li>";

elseif(!empty('lastname2_4'))
echo "<li style='font-weight:bold;font-size:14; width: 50%; margin-left: auto; margin-right: auto;'>" . $strLink . ":  " . "{$row['title2_4']}" . "  ". "{$row['firstname2_4']}" . "  " . "{$row['lastname2_4']}," . "  " . "{$row['injury2_4']}" . "  " . "{$row['vehicle4']}" . "</li>";

print "</ol";

}   
} 
// Close the database connection
    mysqli_close($connect);
} 

我已经经历了几十次迭代,试图找到一种让它工作的方法。

我认为,理想情况下,如果 PHP 中有类似的 MySQL“LIKE”,我也许可以在列表中使用它;但是,如果在一条记录中有多个名称与搜索匹配,那可能仍然是个问题,因为我想列出所有这些名称。

任何帮助将不胜感激。

标签: phpmysqlmysqli

解决方案


推荐阅读