0

I'm using codeigniter and I have some cURL script in my controller, which will get some big json data and loading of it take about 15-20 seconds.

My view is loading so long like the curl time (15-20s).

How to first load my view with some "loading icon", and after script will download all json data replace "loading icon" with my data?

My code:

View:

        <?php foreach ($items as $item): ?>
            <div class="item-slot">
                <img src="http://cdn.steamcommunity.com/economy/image/<?= $item['icon_url'] ?>">
                <p><?= $item['name'] ?></p>
            </div>
        <?php endforeach; ?>

Controller (open_inventory function):

public function open_inventory($steam_id, $appid = 730, $contextid = 2)
{
    $data = array("steamid" => $steam_id, "appid" => $appid);                                                                    
    $data_string = json_encode($data);                                                                                   

    $ch = curl_init('http://localhost:3000/inventory');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',
        'Content-Length: ' . strlen($data_string))
    );
    $json = json_decode(curl_exec($ch),true);

    if($json['data'])
    {
        return $json['data'];
    }
    return false;
}

$items in View is $json['data'] from Controller.

Regards.

6
  • 1
    You need to look into using ajax calls in your application to retrieve async data from the server. Commented Mar 26, 2016 at 20:19
  • @Yani can you tell me what about I need to search into ajax functions to do this? Regards. Commented Mar 26, 2016 at 20:20
  • How about api.jquery.com/jquery.ajax Commented Mar 26, 2016 at 20:26
  • Or stackoverflow.com/questions/6325695/… Commented Mar 26, 2016 at 20:27
  • if you have multiple curl calls and in order to speed up things you might want to look into php manual for curl_multi_init() and also here stackoverflow.com/a/9311112/2275490 Commented Mar 26, 2016 at 21:28

1 Answer 1

0

jQuery has the functions ajaxStart and ajaxStop which you could use like this:

$(document).ajaxStart(function() {
  $("#loading-image").show();
}).ajaxStop(function() {
  $("#loading-image").hide();
}); 

more info here and here

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

3 Comments

can you explain me more how to do this with loading curl json data with php?
call your php function open_inventory() from view, via ajax
can you tell me how to call function via ajax?

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.