2012-12-09 59 views
0

以下是我将xls转换为csv的脚本。无论前面提到的for循环代码片段是什么,它都会先运行。请让我知道如何显示来自以下脚本的图像,然后运行循环脚本:循环执行前的显示图像

<body> 

asdjasldjasdlj // Even this text gets load after execution of for loop 
<!-- Progress information --> 
<div id="information" style="width"><img src="images/L.gif" /></div> 

<?php 
    require_once 'lib/excel_reader2.php'; 
    $excel = new Spreadsheet_Excel_Reader(); 
    $excel->setOutputEncoding('UTF-16'); 
    $excel->read('Student2.xls'); 
    $x=1; 
    $sep = ","; 

    $nbSheets = count($excel->sheets); 
    echo $x." - ".$nbSheets; 

    $total = $nbSheets; 

for($i = 0; $i < $nbSheets; $i++) { 
      ob_start(); 
    while($x<=$excel->sheets[$i]['numRows']) { 
     $y=1; 
     $row=""; 
     while($y<=$excel->sheets[$i]['numCols']) { 
      $cell = isset($excel->sheets[$i]['cells'][$x][$y]) ? $excel->sheets[$i]['cells'][$x][$y] : ''; 
      $row.=($row=="")?"\"".$cell."\"":"".$sep."\"".$cell."\""; 
      $y++; 
     } 
     echo $row."\n"; 
     $x++; 
    } 
    $x = 1; $y = 1; 
    $fp = fopen("data.csv",'a'); 
    fwrite($fp,ob_get_contents()); 
    fclose($fp); 
    ob_end_clean(); 

} 

echo "CSV file created successfully"; 
// Tell user that the process is completed 
echo '<script language="javascript">document.getElementById("information").innerHTML="Process completed"</script>'; 

?> 
</body> 
+1

http://en.wikipedia.org/wiki/Ajax_%28programming%29 –

+0

什么是上下文从客户端将使用这个脚本? –

+0

@JustinGingyMcDonald它将在loacalhost上运行。脚本工作正常,我只是想显示'加载数据'的图像,直到循环完成工作。 –

回答

3

您可以使用flush()函数

它将当前输出一直推送到浏览器。

http://php.net/manual/en/function.flush.php

+0

它会将“ready”输出在页面仍在执行时浏览器。 –

+0

嗯,我试过已经没有工作太循环循环执行第一,即使我在身上onload加上任何函数 –

+0

尝试'flush()'和'ob_flush()'并检查你的输出缓冲设置在php.ini中。 –