比较两个不同 mysqli_fetch_array 和 PHP 的结果

Compare results from two different mysqli_fetch_array with PHP

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

除了那 2 个 table 之外,我还有第三个连接两者,看起来像这样(我将它显示在 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 连接 industry/vertical 和 application/demo。 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 循环时它只能正确 selects Hospitality 而不是 select 批发分销人工服务

我的 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$[ 之间进行两次循环=37=] 数组。现在你只检查所有 $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;
                 ...
       }
}

但是,不需要嵌套循环,因为您 运行 在同一个 table 上 LEFT JOIN,其中不匹配的记录将 return 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>';

    }
}

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