【发布时间】:2016-03-30 01:12:57
【问题描述】:
现在我正在控制器中从 cURL 加载数据并将其显示在我的视图文件中,但从 cURL 加载需要很长时间,所以我的网站加载时间也很长。
我想创建加载效果,只需加载我所有的页面,“加载中,请稍候”。消息直到 cURL 数据可用,之后隐藏加载消息并显示我的数据。
我应该怎么做?
我的代码:
控制器:
public function open_inventory($steam_id, $appid = 730, $contextid = 2)
{
$data = array("steamid" => $steam_id, "appid" => $appid);
$cache_file = $this->config->item('inv_dir')."/".$data['steamid']."_".$data['appid'].".json";
if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 60 * 60 ))) // 1 hour cache
{
$output = file_get_contents($cache_file);
}
else
{
// out-of-date
$data_string = json_encode($data);
$ch = curl_init('http://localhost:3000');
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))
);
$output = curl_exec($ch);
file_put_contents($cache_file, $output, LOCK_EX);
}
$json = json_decode($output,true);
if($json['data'])
{
return $json;
}
return false;
}
视图中的$items 是控制器中的$json['data']。
查看:
<?php foreach ($items as $item): ?>
<div class="item-slot animated zoomIn" data-id="<?= $item['id'] ?>
<p><?= $item['name'] ?></p>
</div>
<?php endforeach; ?>
问候。
【问题讨论】:
-
您是否使用 Ajax 向控制器发送请求?
-
@Matt.k 我从来没有使用过 Ajax,这就是我在这里问的原因。我不知道该怎么做。