简体   繁体   English

PHP mysql while循环内的foreach循环重复

[英]Php mysql while loop inside foreach loop duplicates

i have a string with data structure like this $string = "2,3,4,5";我有一个像这样的数据结构的字符串 $string = "2,3,4,5"; i want to select from my database to get all ids then highlight the exploded string values if equal to the database ids我想从我的数据库中选择以获取所有 ID,然后如果等于数据库 ID,则突出显示分解的字符串值

$string = "2,3,4,5";
$exp = explode(",",$string);

foreach($exp as $e) {
    $query = $conn->query("select * from tbl");
    while($row = $query->fetch_assoc()) {

        $result .= '<option value="" ';
        if($e === $row['id']):
            $result .= ' selected';
        endif;                                      
        $result .= '>';
        $result .= $row['id'];
        $result .= '</option>';

    }
}

but i keep on get duplicates like 1234678,12345678,12345678,12345678 Please help ...但我不断得到重复,如 1234678,12345678,12345678,12345678 请帮忙...

Not sure what you are trying to do but try below code.不确定您要做什么,但请尝试以下代码。 In your code, you are running database query in for loop that's a performance hit.在您的代码中,您正在 for 循环中运行数据库查询,这对性能造成了影响。

    $string = "2,3,4,5";
    $exp = explode(",",$string);

    $query = $conn->query("select * from tbl");
    while($row = $query->fetch_assoc()) {
     $result .= '<option value="" ';
        foreach($exp as $e){
         if($e === $row['id']){
           $result .= ' selected';
           break;
         }
        }

        $result .= '>';
        $result .= $row['id'];
        $result .= '</option>';

    }

You should change the positions of foreach and while like :您应该更改 foreach 和 while 的位置,例如:

$query = $conn->query("select * from tbl");
while($row = $query->fetch_assoc()) {    
    $result .= '<option value="" ';
    foreach($exp as $e) {
      if($e === $row['id']) {
        $result .= ' selected';
      }
    }                                  
    $result .= '>';
    $result .= $row['id'];
    $result .= '</option>';
}

Your logic is absolutely correct but you are placing loops on wrong place.你的逻辑是绝对正确的,但你把循环放在错误的地方。

As foreach is running every time and also SQL query every time and return first row every time and foreach match that and print same result every time.因为 foreach 每次都在运行,而且每次都运行 SQL 查询,每次都返回第一行,foreach 匹配并每次都打印相同的结果。 So simple, you need to place SQL query out of foreach and run it like below如此简单,您需要将 SQL 查询放在 foreach 之外并像下面这样运行

$query = $conn->query("select * from tbl");
while($row = $query->fetch_assoc()) {    
  $result .= '<option value="" ';
  foreach($exp as $e) {
    if($e === $row['id']) {
      $result .= ' selected';
    }
  }                                  
 $result .= '>';
 $result .= $row['id'];
 $result .= '</option>';
}

You will get required output for sure.您肯定会获得所需的输出。 Thanks谢谢

Looks like you want to mark id user selected.I will do following two steps:看起来你想标记 id user selected。我将执行以下两个步骤:

  • get all ids获取所有 ID
  • check each id is selected or not检查每个 id 是否被选中
$query = $con->query("select id from config");
$datas = [];
$result = '';
while($row = $query->fetch_assoc()) {    
    $result .= '<option value="'.$row['id'].'" ';
    if(in_array($row['id'], $exp))
    {
        $result .= ' selected';
    }                                 
    $result .= '>';
    $result .= $row['id'];
    $result .= '</option>';
}
echo $result;

result look like:结果看起来像:

<option value="1"  selected>1</option><option value="2"  selected>2</option>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM