简体   繁体   中英

Php mysql while loop inside foreach loop duplicates

i have a string with data structure like this $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

$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 ...

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.

    $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 :

$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. So simple, you need to place SQL query out of foreach and run it like below

$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:

  • get all ids
  • check each id is selected or not
$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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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