berto
berto

Reputation: 51

do something after whole foreach loop is done

In this code I have a for each loop that goes trough all the results from the array then looks of the user input is equal to that which for now is $testsubject after that it checks if the voucher code is not expired and then it needs to calculate the discount to the price $testamount

echo's

enter image description here

I have this foreach loop and I want that it to first loops trough all the items in the array and then if `$testsubject is equal to one of them go check the next thing and if none of them are equal then show the last echo once.

so basically if ($testsubject == $testVoucherArr['code']) is true stop the for each and go check the next if statement and if it is not true then show the last echo once "this needs to only show when there are no results"

index.php

function display()
{
$arrContextOptions=array(
  "ssl"=>array(
    "verify_peer"=>false,
    "verify_peer_name"=>false,
  ),
); 

$getVoucherList = "https://www.planyo.com/rest/?method=list_vouchers&api_key=yourkey&resource_id=110556";
$cleanVoucherList = preg_replace("/ /", "%20", $getVoucherList);
$voucherlist = file_get_contents("$cleanVoucherList", false, stream_context_create($arrContextOptions));
$voucherList = json_decode($voucherlist, true);
$testsubject = "TESTVOUCHER-B";
$testamount = "5,00";
$doesvoucherexists = false;

foreach($voucherList['data']['results'] as $testVoucher => $testVoucherArr){
  if ($testsubject == $testVoucherArr['code']) {
       $doesvoucherexists = true;
        if (date("Y-m-d") <= $testVoucherArr['rental_end_date']) {
          echo "this code can be used <br>";
          echo $testamount - $testVoucherArr['discount_value'] . "<br>";
      }else{
        echo "this code is expired";
      };

         break;
   }
}
if ($doesvoucherexists === false) {
      echo "this needs to only show when there are no results <br>";
}

if(isset($_POST['submit']))
{
   display();
}}

Upvotes: 0

Views: 1893

Answers (1)

Dimitris Filippou
Dimitris Filippou

Reputation: 809

If i understand correctly what you wanna do you have to move the

if ($doesvoucherexists === false) {

outside the foreach loop.

also you have a syntax error with an extra ; after the else } closing bracket

Tell me if code below is working for you

function display()
{
    $arrContextOptions = array(
        "ssl" => array(
            "verify_peer" => false,
            "verify_peer_name" => false,
        ) ,
    );
    $getVoucherList = "https://www.planyo.com/rest/?method=list_vouchers&api_key=yourkey&resource_id=110556";
    $cleanVoucherList = preg_replace("/ /", "%20", $getVoucherList);
    $voucherlist = file_get_contents("$cleanVoucherList", false, stream_context_create($arrContextOptions));
    $voucherList = json_decode($voucherlist, true);
    $testsubject = "TESTVOUCHER-B";
    $testamount = "5,00";
    $doesvoucherexists = false;
    foreach($voucherList['data']['results'] as $testVoucher => $testVoucherArr) {
        if ($testsubject == $testVoucherArr['code']) {
            $doesvoucherexists = true;
            if (date("Y-m-d") <= $testVoucherArr['rental_end_date']) {
                echo "this code can be used <br />";
                echo $testamount - $testVoucherArr['discount_value'] . "<br />";
            }
            else {
                echo "this code is expired";
            }
            break;
        }

    }
    if ($doesvoucherexists === false) {
        echo "this needs to only show when there are no results <br />";
    }
}

if (isset($_POST['submit'])) {
    display();
}

Upvotes: 1

Related Questions