doudouwd2017 2019-02-15 08:02
浏览 41
已采纳

从数据库中选择PHP多个数组

Hello please can anyone give idea for this?
I want data will selected when it match with their id but when i try this some data will not selected just display only how can I make it selected by their ID?
In this array it work:

[0] => 1 [1] => 2 [2] => 3

In this array it won't work:

[0] => 20 [1] => 21 [2] => 22 [3] => 23 [4] => 24 
$stat = $dbh->prepare("SELECT * FROM department");
$stat2 = $dbh->prepare("SELECT * FROM docprivacy WHERE doc_id = $id ORDER BY dep_id");   

$stat2->execute();

$array = array();

while ($row3 = $stat2->fetch()) {
    extract($row3);
 array_push($array, $row3['dep_id'] )    ; 
}

$stat->execute();
$i = 0 ;

while($row = $stat->fetch()){
    extract($row);
    while ( $i < count($array)) {
        # code...

     if ($row['d_id'] == $array[$i]){
       echo "<option value = '{$d_id}' selected>".$row['department']."</option>";
      }else{
        echo "<option value = '{$d_id}'>".$row['department']."</option>";
    }

    $i++;
  }
}
  • 写回答

2条回答 默认 最新

  • dtrotfd1012 2019-02-15 09:16
    关注
    if (in_array($row['d_id'], $array ))
      {
         echo "<option value = '{$row['d_id']}' selected>".$row['department']."</option>";
      }
    else
      {
        echo "<option value = '{$row['d_id']}'>".$row['department']."</option>";
      }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?