PHP值重复两次

时间:2017-03-27 18:39:31

标签: javascript php sql

到目前为止,我的项目正在从我的SQL表中获取数据。我有四个不同的部分,它们都从同一个表中获取相同的信息。

第一部分效果很好。数据就在那里。

接下来的三个部分有效,但数据重复,我不明白为什么或如何解决它。下面是我的SQL表和我的PHP代码。

+--------+
| Color  |
+--------+
| Red    |
| Blue   |
| Orange |
| Black  |
+--------+


+-----------+--------------+-----------+
| GroupName | MemberName   | ValueName |
+-----------+--------------+-----------+
| Red       | Joe Bob      | Joe       |
| Red       | Catherine    | Kat       |
| Blue      | Tommy        | Tom       |
| Orange    | John Razks   | John      |
| Black     | Trevor Smith | Trevor    |
+-----------+--------------+-----------+

代码:

<form id=#blah>
     <select id="committee" name="committee" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);">
     <option value="">Select Color</option>
     <?php
     $conn = mysqli_connect("#connection");

    if(!$conn){
        die("Connection Failed".myslqi_connect_error());
    }
    $result = mysqli_query($conn, "SELECT * from Color order by Color ASC");
    while ($row = mysqli_fetch_assoc($result)){
        unset($committee, $Committee);
        // $committee = $row['Committee'];
        $Committee = $row['Color'];
        echo '<option value=" .$Committee. ">'.$Committee.'</option>';
    }
    ?>  
    </select>
    </div>

    <div class="sub_category_div" id="sub_category_div">
        Individual: 
        <select name="senator" id="senator">
        <option value="">Select Individual</option>
        <?php
        $conn = mysqli_connect("#connection");

    if(!$conn){
        die("Connection Failed".myslqi_connect_error());
    }
    $result = mysqli_query($conn, "SELECT distinct MemberName,GroupName,ValueName from Members order by MemberName ASC");
    while ($row = mysqli_fetch_assoc($result)){
        $array[$row['GroupName']][] = $row;
    }
    foreach($array as $class => $value){
        if(is_array($value)){
            echo "<optgroup class=".$class.">";
            foreach($value as $k=>$v){
                echo "<option value=".$v['ValueName'].">".$v['MemberName']."</option>";
            }
            echo "</optgroup>";
        }
    }
    ?>
    </select>
    </form>
    <form id=#id>
        <select id="committee" name="committee" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);">
        <option value="">Select Color</option>
        <?php
        $conn = mysqli_connect("#connection");

    if(!$conn){
        die("Connection Failed".myslqi_connect_error());
    }
    $result = mysqli_query($conn, "SELECT * from Color order by Color ASC");
    while ($row = mysqli_fetch_assoc($result)){
        unset($committee, $Committee);
        // $committee = $row['Committee'];
        $Committee = $row['Color'];
        echo '<option value=" .$Committee. ">'.$Committee.'</option>';
    }
    ?>  
    </select>
    </div>

    <div class="sub_category_div" id="sub_category_div">
        Individual: 
        <select name="senator" id="senator">
        <option value="">Select Individual</option>
        <?php
        $conn = mysqli_connect("#connection");

    if(!$conn){
        die("Connection Failed".myslqi_connect_error());
    }
    $result = mysqli_query($conn, "SELECT distinct MemberName,GroupName,ValueName from Members order by MemberName ASC");
    while ($row = mysqli_fetch_assoc($result)){
        $array[$row['GroupName']][] = $row;
    }
    foreach($array as $class => $value){
        if(is_array($value)){
            echo "<optgroup class=".$class.">";
            foreach($value as $k=>$v){
                echo "<option value=".$v['ValueName'].">".$v['MemberName']."</option>";
            }
            echo "</optgroup>";
        }
    }
    ?>
    </select>
</form>

1 个答案:

答案 0 :(得分:0)

我不是100%肯定,但我认为重复来自于在第二次调用相同查询时在循环中使用相同的变量名称,或者它也可能是循环中发生的事情。 我认为你可以通过每次只运行一次查询然后每次需要使用它时调用数据来避免这种情况并使代码更整洁:

<?php
  $conn = mysqli_connect("localhost","user","pass","db_name");
  if(!$conn){ die("Connection Failed".myslqi_connect_error()); }
  else{
    $color_result = mysqli_query($conn, "SELECT * from Color order by Color ASC");
    $colors = array();
    while ($row = mysqli_fetch_assoc($color_result)){ $colors[] = $row['Color']; }
    $member_result = mysqli_query($conn, "SELECT distinct MemberName,GroupName,ValueName from Members order by MemberName ASC");
    $members = array();
    while ($row = mysqli_fetch_assoc($member_result)){
      if(!isset($members[$row['GroupName']])){ $members[$row['GroupName']] = array(); }
        $members[$row['GroupName']][] = $row; }
  }
?>
<form id=#blah>
<select id="committee" name="committee" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);">
    <option value="">Select Color</option>
        <?php
            foreach($colors as $color){
              echo "<option value=\"".$color."\">".$color."</option>";
            }
         ?> 
            </select>
        </div>

    <div class="sub_category_div" id="sub_category_div">
    Individual: 
    <select name="senator" id="senator">
        <option value="">Select Individual</option>
        <?php
        foreach($members as $key => $member_group){
                echo "<optgroup class=\"".$key."\">";
                foreach($member_group as $val){
                    echo "<option value=\"".$val['ValueName']."\">".$val['MemberName']."</option>";
                }
                echo "</optgroup>";
        }
        ?>
    </select>
  </form>
  <form id=#id>
  <select id="committee" name="committee" onchange="javascript: dynamicdropdown(this.options[this.selectedIndex].value);">
<option value="">Select Color</option>
        <?php
            foreach($colors as $color){
              echo "<option value=\"".$color."\">".$color."</option>";
            }
         ?> 
            </select>
        </div>

    <div class="sub_category_div" id="sub_category_div">
    Individual: 
    <select name="senator" id="senator">
        <option value="">Select Individual</option>
        <?php
        foreach($members as $key => $member_group){
                echo "<optgroup class=".$key.">";
                foreach($member_group as $val){
                    echo "<option value=\"".$val['ValueName']."\">".$val['MemberName']."</option>";
                }
                echo "</optgroup>";
        }
        ?>
    </select>
  </form>

这样可以让您获得所需的输出,同时只需运行一次查询,并消除重复。我希望这有帮助