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.

mateuszji
  • 67
  • 1
  • 10
  • 1
    You need to look into using `ajax` calls in your application to retrieve async data from the server. – dev7 Mar 26 '16 at 20:19
  • @Yani can you tell me what about I need to search into ajax functions to do this? Regards. – mateuszji Mar 26 '16 at 20:20
  • How about http://api.jquery.com/jquery.ajax/ – dev7 Mar 26 '16 at 20:26
  • Or http://stackoverflow.com/questions/6325695/creating-json-data-using-php-and-parsing-it-using-jquery – dev7 Mar 26 '16 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 http://stackoverflow.com/a/9311112/2275490 – Vickel Mar 26 '16 at 21:28
  • @Vickel I don't have multiple calls, I have only 1 but it return so big json data and it tooks a long time that's why I want to create something "loading" bar or image. – mateuszji Mar 26 '16 at 21:29

1 Answers1

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

Vickel
  • 7,879
  • 6
  • 35
  • 56