Home > Blockchain >  getting value of while loop in as a function argument in php
getting value of while loop in as a function argument in php

Time:10-17

I have to get the value of conditional statement(if/else) in function as argument , the conditional statement is in a while loop. When, I'm printing the value then the output is accurate ,but when I'm trying to get the conditional value as functional argument then output is wrong.

For example , when I'm not calling the function then those systems which are in auto are coming as auto mode and which are manual mode are coming in manual mode .But when I'm returning value in function ,then after the system which is in manual mode ,every device is showing that they are in manual mode.

 <?php

include('dbinc.php');

$mysqli = new mysqli("$mysql_hostname", "$mysql_user", "$mysql_password", "$mysql_database");
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}




$query = "SELECT *
FROM siteinfo
LEFT JOIN siteinalarm ON siteinfo.site_id = siteinalarm.site
LIMIT 0 , 1000
";


$result =  $mysqli->query($query);


function mysms($site_name, $site_id,  $manual_mode)
{
  //   Alarm Generated on Site Tikari (TK12345678) ) BTS Voltage 24.6V, FIRE & SMOKE, DG ON, MAINS FAIL, DOOR OPEN, DG FAIL TO START, DG FAIL TO STOP, SYSTEM ON MANUAL. - System Infra
  return (" Alarm Generated on Site {$site_name} ({$site_id}),{$manual_mode}. - System Infra");
}

while ($row = $result->fetch_row()) {

  $Id = $row[0];
  $site_id = $row[1];
  $Customer_Id = $row[2];
  $Cluster_Id = $row[3];
  $Circle = $row[6];
  $Site_Name = $row[7];
  $District = $row[8];
  $TechName = $row[18];
  $Tech_Number = $row[19];
  $data_status = $row[39];
  $last_message_time = $row[43];
  $autostatus = false;


  if (!$data_status == 0) {


    // echo "Alarm generated on Site $Site_Name($site_id).$last_message_time ";


    $stats = unpack("C*", $data_status);

    //test alternator fault

    if (($stats[4] & 0x04) == 0) {
      $auto = "No fault";
    } else {
      $alternatorfault = "DG Fail to Start (alternator fault),";
      // echo $alternatorfault;
    }


    if (($stats[5] & 0x04) == 0) {
      $automode = "Auto Mode,";
      $autostatus = false;
      // echo $automode;
    } else {
      $manual_mode =    "System on Manual,";
      // echo $manual_mode;
      $autostatus = true;
    }


    // //for Site battery low 48v
    // if (($stats[5] & 0x40) == 0) {
    //   echo "battery ok,";
    // } else {
    //   echo "Site battery low,";
    // }

    $messg = mysms("$site_id", "$Site_Name", "$manual_mode");
    echo $messg;
    echo "<br/>";

    // echo "<br>";
  }
}

CodePudding user response:

There is wrong to call your function, calling it as follows:

$messg = mysms($site_id, $Site_Name, $manual_mode);

And update your code here.

if(!empty($manual_mode){
 $messg = mysms($site_id, $Site_Name, $manual_mode);
}

Because if the condition is true, the function should not be called where your mode is auto_mode. If you pass it as arguments then you can update your variable as $mode.

CodePudding user response:

I wanted to toggle the message if it changed,earlier due to loop the message was coming same ,but when the message was declared by same variable then it started to toggle.

if (($stats[5] & 0x04) == 0) {


  $mode = "Auto Mode,";

  // echo $automode;
} else {

  $mode =    "System on Manual";
  // echo $manual_mode;

}
  • Related