0

I have cURL function which make calls to api based on ID which I provide and return data in array. So far everything works perfectly. What I want and can't figure out is this:

I have foreach which display in table all orders lie

@foreach($orders as $order)

   $order->address
   $order->time_created
   $order->price
    ... etc

@endforeach

Is it possible to put this cURL inside the foreach and add on each row based on $order->order_id to show some more data from the array?

So something like this

@foreach($orders as $order)

   $order->address
   $order->time_created
   $order->price
    ... etc

   $url = get_curl_content_tx("http://example.com/".$order->order_id);
   $data = json_decode($url,true);

   @foreach ( $data as $moreData )
       @if ( $moreData['data'] == $order->time_created )

         // show something
       @else
         // show other thing
       @endif
   @endforeach
@endforeach

My problem is that I can't figure how to loop the url so I can get all orders_id i.e.

$url = get_curl_content_tx("http://example.com/1");
$url = get_curl_content_tx("http://example.com/2");
$url = get_curl_content_tx("http://example.com/3");
$url = get_curl_content_tx("http://example.com/4");
$url = get_curl_content_tx("http://example.com/5");
$url = get_curl_content_tx("http://example.com/6");

So then I can perform my if/else block. Framework which I use is Laravel 4.2.

Hope it is clear enough what I want to accomplish. If is not I will try to clear things..

EDIT: Example what it should be:

<table>
    <thead>
        <tr>
            <th class="text-center">#</th>
            <th class="text-center">Date Created</th>
            <th class="text-center">Name</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th>1</th>
            <th>$url = get_curl_content_tx("http://example.com/1")</th> 
            <th>Product 1</th>
        </tr>
        <tr>
            <th>2</th>
            <th>$url = get_curl_content_tx("http://example.com/2")</th> 
            <th>Product 2</th>
        </tr>       
    </tbody>
</table>
S.I.
  • 3,250
  • 12
  • 48
  • 77
  • Yes it is possible. Still I'm not sure what is your problem though? – Suraj Oct 12 '16 at 12:32
  • My problem is that I loop trough orders and on the page are 10 orders. Then I got 10 times curl url with same order_id .. i.e. first one. – S.I. Oct 12 '16 at 12:34
  • From what I understand you want to perform a CURL request inside a foreach, and the CURL depends on a value of the foreach. But then you say the CURL is performed with the same order_id? If the code you've shown is correct (ie: `@foreach($orders as $order)` and each `$order->order_id` is different, then that should be working correctly..Can you provide additional details? – Luís Cruz Oct 12 '16 at 21:02
  • Yes, it is working but for example if foreach take 5 orders from database and display them in the table on the page I should have 5 times this CURL with each product ID at the end `$url = get_curl_content_tx("http://example.com/1...5");`. Instead I've got 5 times curl url with same ID i.e. looping order with ID=1 5 times. Seems like something in my loops isn't correct. – S.I. Oct 13 '16 at 04:26
  • I've updated my question with how is expected to be. However instead of getting correct ID I've got same ID on each product. – S.I. Oct 13 '16 at 04:32
  • As I've said, your code is correct and you have a different value for `$order->order_id`, it should work correctly. Add a `dd($orders)` and verify the `order_id` is different for each item on the Collection. If the `order_id` is the same for each item, then you have an issue on the query / model. (as a note, when replying to comments, be sure to add @username, for example, @milz). Thanks. – Luís Cruz Oct 13 '16 at 09:14
  • `order_id` are different yes. I've got managed to take different Id's on the curl url and got response but I have 5 different arrays response from url's now and I don't know how to access data in them and perform those `if/else` conditions on each order – S.I. Oct 13 '16 at 09:25

2 Answers2

1

You can use laravel's unique method for collections to remove duplicate order id's before iterating them.

$collection = collect([
    ['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'],
    ['name' => 'iPhone 5', 'brand' => 'Apple', 'type' => 'phone'],
    ['name' => 'Apple Watch', 'brand' => 'Apple', 'type' => 'watch'],
    ['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'],
    ['name' => 'Galaxy Gear', 'brand' => 'Samsung', 'type' => 'watch'],
]);

$unique = $collection->unique('brand');

$unique->values()->all();

Or you can use PHP method as well. How to remove duplicate values from a multi-dimensional array in PHP

Community
  • 1
  • 1
Suraj
  • 2,181
  • 2
  • 17
  • 25
0

I have manage to do it and will post it as answer. So since I already had ID's of the product I've made it like this directly in the blade. Maybe there is a room for improvements but for now at least it is working.

This is short version without echoes in conditions

<td class="text-center">
<?php

$url=get_curl_content_tx("http://example.com/". $order->order_id);          
$arr = json_decode($url, true);     
if (is_array($arr['data']) || is_object($arr['data'])) {
        foreach($arr['data'] as $data ) {                   
            $match = false;
            if ($data['amount'] == $order->amount) {                    
                    $match = $data;
                    break;
            }
        }
        if($match !== false ) {

            if( .. ) {
                 // do something
            }
            else {      
                // do something else
            }                 
        }
        else { }    
}    
?>
</td>
S.I.
  • 3,250
  • 12
  • 48
  • 77