lundi 12 février 2018

Passing through array, getting the sum to add up

I need a lot of help here. I am totally stuck in my code, and cannot get it to perform. I am trying to get it to check my database, for example the first key in my array, there it should see if $count is greater or equal to $val. If it is great, if not, then the value in the array should be set to "0" for the current key, and then until the sum of the array is equal to starting sum of $prize.

    $prize = [
      "key1" => "2",
      "key2" => "2",
      "key3" => "2",
      "key4" => "2",
      "key5" => "2",
      "key6" => "2",
      "key7" => "2",
      "key8" => "2",
      "key9" => "2",
      "key10" => "1",
      "key11" => "1",
      "key12" => "1",
  ];

  $sum = array_sum($prize);
  echo $sum 

    foreach($prize as $key => $val){

$sql = "SELECT * FROM `indvejning` WHERE `art`= :art ORDER by `art`, `vaegt` DESC, `laengde` ASC";    

    $stmt = $conn->prepare("$sql");
    $stmt->bindParam(':art', $key);
    $stmt->execute();
    $count = $stmt->rowCount();

    while ($arr = $stmt->fetch(PDO::FETCH_ASSOC)) {

    Performs listing of the data from the SQL!!! Not important here...

}  

 if ($count < $val) {
//  print "Found $count in the query. Array needs $val records. <br />";
  $prize[$key] = $val-1;
} elseif ($count >= $val) {
//  print "Found $count in the query. Array only needs $val records. <br />";
$prize[$key] = $val+1 ;
} 

    }

    print_r($prize);
    echo "sum(prize) = " . array_sum($prize) . "\n";

I really hope you can help me see where it goes wrong?

Aucun commentaire:

Enregistrer un commentaire