多列搜索,然后回显找到的特定列

时间:2019-05-30 18:22:08

标签: php mysql mysqli

我的MySQL数据库将参与“事件”的车辆中的乘客姓名分组。一次最多可将46个名字/姓氏对分散在多达5辆车辆中(在车辆1,车辆2,车辆2,车辆4,车辆3,车辆4、8,车辆5中的30个中最多分为2个) 。我可以搜索所有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%'))

以下是几行代码。在“列表链接”部分下,应该有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”,我也许可以在列表中使用它。但是,如果在一个记录中有多个与搜索匹配的名称,那可能仍然是个问题,因为我想列出所有这些名称。

任何帮助将不胜感激。

0 个答案:

没有答案