简体   繁体   中英

How to use if statements inside foreach loops?

I'm trying to create an if statement inside a foreach loop that obtains data from a column named ['online'], the value is either 0 or 1. I'm trying to convert the value to a string like so:

  $result = mysqli_stmt_get_result($stmt);
  $datas = array();

  while($row = mysqli_fetch_assoc($result)){
    $datas[] = $row;
  }

  foreach($datas as $data){
    $status = $data['online'];
    if($status = 1){
      $status = "Online";
    }else{
      $status = "Offline";
    }
    echo '<td>'.$status.'</td>';
  }

If the value is 1 it should be converted to "Online". If it's not 1 then it should be "Offline", currently I get all the results showing as "Online". Not sure why, help would be appreciated! :)

Try:

if($status == 1){/*...*/}

Istead of

if($status = 1){/*...*/}

Equal sign is for variable assignment

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