Codeigniter 视图显示进度
Posted
技术标签:
【中文标题】Codeigniter 视图显示进度【英文标题】:Codeigniter view show progress 【发布时间】:2016-08-04 14:06:18 【问题描述】:我正在使用 Codeigniter 并希望显示 XML 导入的进度。 但我面临的问题是 当查看加载时,它卡在加载(空白页面)上,当我可以看到它的显示 100% 完成时。
我的代码如下
$i=0;
$count=sizeof($record->List);
foreach($record->List as $Item)
$i++;
echo "processing ".$i." of ".$count;
---processing code which takes times to process---
---processing code which takes times to process---
此代码在视图中,但是当我单击链接以加载此视图时,我必须等待所有进程完成,然后我才能看到所有进程完成后的视图。
我想要的是:
显示视图(空)。
然后继续循环打印每一行。
谢谢
【问题讨论】:
你不能只使用php来做这个,php只有在执行完成后才会打印结果 您好@Rijin,感谢您的回复,那我该怎么做呢? 您可以使用 WebSockets 更新客户端。 这里是如何立即回显 - ***.com/a/4978809/2083877 【参考方案1】:这里有一个使用 AJAX 的进度条示例:
index.php
<?php
// Start the session.
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Progress Bar</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<style>
#progress
width: 500px;
border: 1px solid #aaa;
height: 20px;
#progress .bar
background-color: #ccc;
height: 20px;
</style>
</head>
<body>
<div id="progress"></div>
<div id="message"></div>
<script>
var timer;
// The function to refresh the progress bar.
function refreshProgress()
// We use Ajax again to check the progress by calling the checker script.
// Also pass the session id to read the file because the file which storing the progress is placed in a file per session.
// If the call was success, display the progress bar.
$.ajax(
url: "checker.php?file=<?php echo session_id() ?>",
success:function(data)
$("#progress").html('<div class="bar" style="width:' + data.percent + '%"></div>');
$("#message").html(data.message);
// If the process is completed, we should stop the checking process.
if (data.percent == 100)
window.clearInterval(timer);
timer = window.setInterval(completed, 1000);
);
function completed()
$("#message").html("Completed");
window.clearInterval(timer);
// When the document is ready
$(document).ready(function()
// Trigger the process in web server.
$.ajax(url: "process.php");
// Refresh the progress bar every 1 second.
timer = window.setInterval(refreshProgress, 1000);
);
</script>
</body>
</html>
checker.php
<?php
// The file has JSON type.
header('Content-Type: application/json');
// Prepare the file name from the query string.
// Don't use session_start here. Otherwise this file will be only executed after the process.php execution is done.
$file = str_replace(".", "", $_GET['file']);
$file = "tmp/" . $file . ".txt";
// Make sure the file is exist.
if (file_exists($file))
// Get the content and echo it.
$text = file_get_contents($file);
echo $text;
// Convert to JSON to read the status.
$obj = json_decode($text);
// If the process is finished, delete the file.
if ($obj->percent == 100)
unlink($file);
else
echo json_encode(array("percent" => null, "message" => null));
process.php
<?php
// Start the session.
session_start();
// The example total processes.
$total = 20;
// The array for storing the progress.
$arr_content = array();
// Loop through process
for ($i = 1; $i <= $total; $i++)
// Calculate the percentation
$percent = intval($i / $total * 100);
// Put the progress percentage and message to array.
$arr_content['percent'] = $percent;
$arr_content['message'] = $i . " row(s) processed.";
// Write the progress into file and serialize the PHP array into JSON format.
// The file name is the session id.
file_put_contents("tmp/" . session_id() . ".txt", json_encode($arr_content));
// Sleep one second so we can see the delay
sleep(1);
只需将它写在 CI 上并创建一个 tmp 文件夹,然后在脚本中指向它的路径。祝你好运!
【讨论】:
以上是关于Codeigniter 视图显示进度的主要内容,如果未能解决你的问题,请参考以下文章