0

I have condition in my blade template which check if customer is paid from api call. The problem is that doesn't matter if some of if/elseif conditions is met it's always goes to else and show what is in there.

@if (is_array($totala['data']['incoming']) || is_object($totala['data']['incoming']))

    @foreach($totala['data']['incoming'] as $key => $date)
    @endforeach
           @if(strpos($date['address'], $address['address']) !== false )

                @if($order['price'] == $date['amount'] )
                    <span>Paid</span>

                @elseif($order['price'] < $date['amount'] )
                    <span>Over Paid</span>


                @elseif($order['price'] > $date['amount'] )
                    <span>Under Paid</span>
                @else                       
                @endif
           @else
                <span>Else block</span>                                
           @endif       
@else
    <span>Unpaid</span>
@endif

I've made it like this because if I put inner if/else block inside foreach I've got even unwanted results on the page because they are multiple elements inside $totala['data']['vouts'] and foreach loop iterate until there is no more elements..

So why the result always goes in else block?

Note: All data is there and is correct. I've triple checked the data in the json array.

Update: Here is example of the array

Array
(
  [status] => success
  [data] => Array
    (
        [id] => id
        [incoming] => Array
            (
                [0] => Array
                    (
                        [address] => address1
                        [amount] => sum1

                    )

                [1] => Array
                    (
                        [address] => address2
                        [amount] => sum2

                    )

                [2] => Array
                    (
                        [address] => address3
                        [amount] => sum3

                    )

                [3] => Array
                    (
                        [address] => address4
                        [amount] => sum4

                    )

                [4] => Array
                    (
                        [address] => address5
                        [amount] => sum5

                    )
            )
    )
 )

With foreach I'm looping in array[data][incoming] to search for the address strpos($date['address'], $address['address']) !== false then when the address is found I want to go trough conditions.

I just realized that I's always goes in else block because they are multiple arrays in [incoming] and there always conditions are not met. Is that correct? If is that how can I fix it?

S.I.
  • 3,250
  • 12
  • 48
  • 77
  • What data is in `$totala['data']['incoming']`? Your code is evaluating based on the final element in this array which might be what you want but using a `foreach` loop isn't the best way to get it. Also - which else block is always being triggered? There's 3 in your example. – Steve Sep 29 '16 at 11:17
  • `Else block` this block is always triggered. I've updated my question with example how the array looks. – S.I. Sep 29 '16 at 11:27
  • Ok so `strpos($date['address'], $address['address'])` is evaluating to false - what is the value of `$address['address']` in your script? – Steve Sep 29 '16 at 11:30
  • it's value which is came from database. It's address saved in database. This address match one of the array inside `[incoming]`. Will always match 1 address inside the `[incoming]`. I mean it should match and if not match should go in else block. – S.I. Sep 29 '16 at 11:32
  • When I var_dump `$address['address']` and `$date['address']` i see them matching i.e. equals. – S.I. Sep 29 '16 at 11:35
  • If you are checking for two identical strings why don't you use `if($date['address'] == $address['address'])`? also if there are spaces or any slight formatting differences, the strings won't match, even if they look the same when dumped. – Steve Sep 29 '16 at 11:40
  • Because I need to loop in `[incoming]` to find the address if is there. No? Also I just found that `@if(strpos($date['address'], $address['address']) !== false )` this compare always last address in `[incoming]` addresses array. That's why always show `else` block i.e. no matching. If the address is on last spot of the array it doesn't skip to else... – S.I. Sep 29 '16 at 11:44

1 Answers1

1

Messy, but I think this will achieve what you need.

@if (is_array($totala['data']['incoming']) || is_object($totala['data']['incoming']))

   <?php
      // find the first matching address in $totala['data']['incoming']
      $match = false;
      foreach ($totala['data']['incoming'] as $data) {
         if ($data['address'] == $address['address']) {
            $match = $data;
            break;
         }
      }
   ?>

   @if($match !== false )

      @if($order['price'] == $match['amount'] )
         <span>Paid</span>
      @elseif($order['price'] < $match['amount'] )
         <span>Over Paid</span>
      @else
         <span>Under Paid</span>                      
      @endif
   @else
      <span>No match found</span>                                
   @endif
@else
   <span>Unpaid</span>
@endif

If you are only interested in the matching data you should extract this information in your controller - not in the view.

Update:
If using PHP >= 5.5.0 you have access to the array column function, have a look at https://stackoverflow.com/a/34785508/2180572 for an explanation and example. In your code replace the php block with...

<?php
   $match = array_search($address['address'], array_column($totala['data']['incoming'], 'address'));
   if($match) {
      $match = $totala['data']['incoming'][$match];
   }
?>
Community
  • 1
  • 1
Steve
  • 1,853
  • 16
  • 30
  • Thanks for the answer. I've tried to extract it in my controller and didn't succeeded.. that's why I wen in the view. Can you give me example how to extract it in the controller and share the result? Will test this answer in a sec. – S.I. Sep 29 '16 at 12:06
  • Cool, glad I could help! – Steve Sep 29 '16 at 12:18