使用PHP

时间:2017-07-06 14:37:15

标签: php mysql mysqli while-loop nested-if

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

除了那两个表,我有第三个连接两个表,看起来像这样(我在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 Dist Human Services < / em>的

我的while-loop +嵌套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查询中的垂直项进行比较吗?你会建议以不同的方式做事吗?

1 个答案:

答案 0 :(得分:1)

为了检查两个查询之间的所有匹配条件,您需要在 $ 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 上,则

垂直值不应重复。