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>
7
  • Yes it is possible. Still I'm not sure what is your problem though? Commented Oct 12, 2016 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. Commented Oct 12, 2016 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? Commented Oct 12, 2016 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. Commented Oct 13, 2016 at 4: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. Commented Oct 13, 2016 at 4:32

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.