0

I have a form with three fields that I want to read separately from the database

 $_POST['wkNumer1'];
 $_POST['wkNumer2'];
 $_POST['wkNumer3'];

How can I read this data without repeating the same code 3 times? In this code, only the variable $wkNumer value will be change.

<?php
  if (isset($_POST['show_diagram'])) {
    
    $goodname = $_POST['htDriver'];
    $wkNumer = $_POST['wkNumer1'];
   
      // Table with data
      $sql = "SELECT WorkingDay, OrderNo, NameFinish, Type  FROM `status` where WEEK(WorkingDay) = :wknumer AND NameFinish = :nameFinish"; // SQL with parameters
      $stmt = $conn->prepare($sql); 
      $stmt->bindParam("wknumer", $wkNumer);
      $stmt->bindParam("nameFinish", $goodname);
      $stmt->execute();
      $OCSdatas = $stmt->fetchAll(PDO::FETCH_ASSOC);
      $count = $stmt->rowCount();

      $countWithoutE = 0;
      $countE = 0;
      foreach ($OCSdatas as $data) {
       
        if ($data['Type'] != 'E') {
          $countWithoutE = $countWithoutE + 1 ;
        }

        if ($data['Type'] == 'E') {
          $countE = $countE + 1 ;
        }
      }
      echo $goodname . '<br />';
      echo $wkNumer . '<br />';
      echo $countWithoutE . '<br>';
      echo $countE . '<br>';
      $countE = $countE/2;
      $countTotal = $countWithoutE + $countE;
      echo $countTotal/5  . '<br>';
      echo $count/5;
  }
  

?>

2 Answers2

0

How does this work for you?

$workNumbers = array($_POST['wkNumer1'],$_POST['wkNumer2'],$_POST['wkNumer3']);

foreach($workNumbers as $wkNumer){
  //Your Code block here
}
outlaw
  • 197
  • 8
0

You can put the numbers inside an array and iterate through that array to execute the same code.

$goodname = $_POST['htDriver'];

// Add the numbers to an array which we can iterate
$numbers = [
    $_POST['wkNumber1'],
    $_POST['wkNumber2'],
    $_POST['wkNumber3'],
];

// Prepare the statement only once before the loop and reuse it
$sql = "SELECT WorkingDay, OrderNo, NameFinish, Type  FROM `status` where WEEK(WorkingDay) = :wknumer AND NameFinish = :nameFinish";
$stmt = $conn->prepare($sql); 

// Loop through the numbers
foreach ($numbers as $number) {
    // Add the current number
    $stmt->bindParam("wknumer", $number);
    $stmt->bindParam("nameFinish", $goodname);
    $stmt->execute();
    
    // Now have everything as you had before
    $OCSdatas = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $count = $stmt->rowCount();

    $countWithoutE = 0;
    $countE = 0;
    foreach ($OCSdatas as $data) {
        if ($data['Type'] != 'E') {
            $countWithoutE = $countWithoutE + 1 ;
        }

        if ($data['Type'] == 'E') {
            $countE = $countE + 1 ;
        }
    }
 
    echo $goodname . '<br />';
    echo $wkNumer . '<br />';
    echo $countWithoutE . '<br>';
    echo $countE . '<br>';
    $countE = $countE/2;
    $countTotal = $countWithoutE + $countE;
    echo $countTotal/5  . '<br>';
    echo $count/5;
}
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • What is the best way to display the database data from each loop side by side. For example: echo $countTotal/5 (from first loop) . $countTotal/5; (from second loop ect) – omafiets1988 Jan 08 '22 at 01:39
  • @omafiets1988 - See if [this post](https://stackoverflow.com/questions/32298656/mysql-display-rows-as-columns) can give you some ideas. Check if you can make some attempt using that. If you can't make it, post another question about it. It's a bit hard to explain it well in a comment. – M. Eriksson Jan 08 '22 at 01:55