gpt4 book ai didi

php - 用 PHP 比较两个不同的 mysqli_fetch_array 的结果

转载 作者:行者123 更新时间:2023-11-29 06:00:59 25 4
gpt4 key购买 nike

我创建了一个数据库,用于保存有关应用程序的信息。这些应用程序可以链接到垂直。我以这样的方式设计了数据库,即我有一个表用于每个应用程序的标准信息,其中包括一个自动递增的 ID 和名称等。我有另一个表列出了所有不同的行业/垂直行业。

除了那 ​​2 个表,我还有第三个表连接这两个表,它看起来像这样(我在 PHP 数组中显示它,因为我认为它更容易阅读):

  $verticals_joint = array(
array('vertical_joint_id' => '1','vertical_id' => '11','demo_id' => '173'),
array('vertical_joint_id' => '2','vertical_id' => '12','demo_id' => '173'),
array('vertical_joint_id' => '3','vertical_id' => '13','demo_id' => '173'),
array('vertical_joint_id' => '4','vertical_id' => '2','demo_id' => '174'),
array('vertical_joint_id' => '5','vertical_id' => '4','demo_id' => '174'),
array('vertical_joint_id' => '6','vertical_id' => '3','demo_id' => '175'),
array('vertical_joint_id' => '7','vertical_id' => '4','demo_id' => '175'),
array('vertical_joint_id' => '8','vertical_id' => '8','demo_id' => '176'),
array('vertical_joint_id' => '9','vertical_id' => '9','demo_id' => '176'),
array('vertical_joint_id' => '10','vertical_id' => '1','demo_id' => '92'),
array('vertical_joint_id' => '11','vertical_id' => '3','demo_id' => '92'),
array('vertical_joint_id' => '12','vertical_id' => '12','demo_id' => '92'),
array('vertical_joint_id' => '13','vertical_id' => '1','demo_id' => '179'),
array('vertical_joint_id' => '14','vertical_id' => '3','demo_id' => '179'),
array('vertical_joint_id' => '15','vertical_id' => '4','demo_id' => '179'),
array('vertical_joint_id' => '16','vertical_id' => '2','demo_id' => '180'),
array('vertical_joint_id' => '17','vertical_id' => '4','demo_id' => '180')
);

vertical_id 将行业/垂直领域与应用程序/演示联系起来。 demo_id 引用应用程序(或我称之为演示)。

现在我的问题来了。我在 PHP 中想出了一个 while 循环,并在其中嵌套了一个 if 语句,这样我就可以显示所有垂直领域,并另外标记给定应用程序是为哪些垂直领域创建的。例如。这就是我想要实现的目标:

<select id="Vertical[]" class="form-control verticalText" multiple="multiple">
<option value="1">Education</option>
<option value="2">Manufacturing</option>
<option value="3">State, Local or Provincial Government</option>
<option value="4">Federal Government</option>
<option value="5">Other</option>
<option value="6">Transportation and Logistics</option>
<option value="7">Healthcare</option>
<option value="8">Petrochemical</option>
<option value="9">Utilities</option>
<option value="10">Public Safety</option>
<option value="11" selected>Hospitality</option>
<option value="12" selected>Wholesale Distribution</option>
<option value="13" selected>Human Services</option>
<option value="14">Retail</option>

这就是我想要实现的,但是当我使用我的 while 循环时,它只正确地选择了Hospitality 而没有选择Wholesale DistHuman Services.

我的 while 循环 + 嵌套 if 语句如下所示:

//Gathers the names of all existing verticals.
$vertical_query = "SELECT * FROM demos.verticals";
$result_vertical = mysqli_query($con, $vertical_query);
//Gathers information about the particular application you are comparing with.
//It retrieves the verticals for a particular app/demo id.
//For this example I used id 173
$joint_query = "SELECT o.vertical_id, o.vertical_name FROM demos.alldemos alld
LEFT JOIN demos.verticals_joint j ON alld.id = j.demo_id
LEFT JOIN demos.verticals o ON o.vertical_id = j.vertical_id
WHERE alld.id = 173";
$result_joint = mysqli_query($con, $joint_query);
$compare = mysqli_fetch_array($result_joint);
$output = '';
// while there are Verticals to be listed.
while ($row_vertical = mysqli_fetch_array($result_vertical)) {
// If a vertical is already selected for this particular app id,
if ($compare['vertical_name'] == $row_vertical["vertical_name"]) {
//then add the word select to the option so it will be selected
$output .= '<option value="'.$row_vertical["vertical_id"].'" selected>'.$row_vertical['vertical_name'].'</option>';
} elseif ($compare['vertical_name'] != $row_vertical["vertical_name"]) {
//Otherwise just print out the normal option without 'selected'.
$output .= '<option value="'.$row_vertical["vertical_id"].'">'.$row_vertical["vertical_name"].'</option>';
}
}
echo $output;

您知道为什么它只将我的 SQL 连接中的第一项与我的其他 SELECT 查询中的垂直项进行比较吗?你会建议以不同的方式做事吗?

最佳答案

为了检查两个查询之间的所有匹配条件,您需要在 $compare$row_vertical 数组之间运行两个循环。现在您只检查所有 $row_vertical 行到 $compare 的第一行(即 Hospitality)。由于值可以在嵌套循环中被覆盖,因此在满足条件时包含一个 break 1:

while ($row_vertical = mysqli_fetch_array($result_vertical)) {        
while ($compare = mysqli_fetch_array($result_joint)) {

if ($compare['vertical_name'] == $row_vertical["vertical_name"]) {
$output = ...;
break 1;
...
}
}

但是,当您在同一表上运行 LEFT JOIN 时,不需要嵌套循环,其中不匹配的记录将返回 NULL。因此,不需要运行和获取冗余的第一个查询,而只需运行一个 $compare 查询,然后检查 NULL

while ($compare = mysqli_fetch_array($result_joint)) {
// If a vertical is already selected for this particular app id,
if (is_null($compare['vertical_name']) == false) {
//then add the word select to the option so it will be selected
$output .= '<option value="'.$compare["vertical_id"].'" selected>'.$compare['vertical_name'].'</option>';

} elseif (is_null($compare['vertical_name']) == true) {
//Otherwise just print out the normal option without 'selected'.
$output .= '<option value="'.$compare["vertical_id"].'">'.$compare["vertical_name"].'</option>';

}
}

如果您将 WHERE 条件保留在一个特定的 demo_id 上,则Vertical 值不应重复。

关于php - 用 PHP 比较两个不同的 mysqli_fetch_array 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44951994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com