关于php运行结果输出的讨论 [ 新手入门 ]
在一个较长的类方法运行时,需要整个方法结束才输出结果 那么如果在中途植入某些变量 通过其他方法去获取这个值是否可行呢?
比如这个动作对应一个载入excel表格的方法
public function actionImport()
{
if (isset($_POST['filename']))
{
$filename = $_POST['filename'];
$sid = $_POST['sid'];
$outputname = iconv("UTF-8", "GBK", $filename);
$uploaddir = dirname(dirname(__FILE__)) . '/../uploads/';
$file = $uploaddir . $outputname;
$objPHPExcel = Importexcel::getdata($file);
// $cookie=new CHttpCookie('sid_'.$sid,0);
// Yii::app()->request->cookies[$name]=$cookie;
$allsheet = $objPHPExcel->getSheetCount() - 1;
$success = 0;
$issueline = array();
for ($i = 0; $i < $allsheet; $i++)
{
$objPHPExcel->setActiveSheetIndex($i);
$objWorksheet = $objPHPExcel->getActiveSheet(); //获取excel中sheet(0)的数据
$EXCEL_VALUE = array();
foreach ($objWorksheet->getRowIterator() as $row) //遍历数据
{
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(FALSE);
foreach ($cellIterator as $cell)
{
$EXCEL_VALUE[$row->getRowIndex()] [] = $cell->getValue();
}
}
$data_arr = $EXCEL_VALUE;
//根据$data_arr首行记录对应数据库各个字段内容编号 并记录编号 以后在循环导入时候使用
$stno = ''; //学号;
$stname = ''; //姓名;
$province = ''; //省;
$area = ''; //地区;
$dataline = 0;
$subject1 = '';
$score1 = '';
$subject2 = '';
$score2 = '';
$Info = array();
//筛选表格信息
foreach ($data_arr as $row => $cols)
{
foreach ($cols as $col => $value)
{
if ($value == '省:')
{
$province = $cols[$col + 1];
if ($province == null)
{
break 2;
}
}
if ($value == '市县:')
$area = $cols[$col + 1];
if ($value == "学员编号")
{
$dataline = $row;
$stno = $col;
$stname = $col + 1;
$subject1 = $col + 2;
$score1 = $col + 3;
$subject2 = $col + 4;
$score2 = $col + 5;
break 2;
}
}
}
/* */
foreach ($data_arr as $line => $contexts)
{
if ($province == null)
break;
if (isset($contexts[$stname]) == FALSE || $contexts[$stname] == '' || $line <= $dataline)
continue;
if ($line > $dataline && $contexts[$stname] != '')
{
if ($cols[$subject1] != '')
{
$model = new Info;
$model->sheet = $objWorksheet->getTitle();
$model->filename = $filename;
$model->stno = $contexts[$stno]; //
$model->stname = $contexts[$stname];
$model->province = $province;
$model->area = $area;
$model->subject = $contexts[$subject1];
$model->score = $contexts[$score1];
if ($model->validate())
{
$model->save(FALSE);
$success++;
}
else
{
$issueline[$objWorksheet->getTitle()][]= '第' . $line . '行数据存在错误' ;
}
}
if ($cols[$subject2] != '')
{
$model1 = new Info;
$model->sheet = $objWorksheet->getTitle();
$model->filename = $filename;
$model1->stno = $contexts[$stno]; //
$model1->stname = $contexts[$stname];
$model1->province = $province;
$model1->area = $area;
$model1->subject = $contexts[$subject2];
$model1->score = $contexts[$score2];
if ($model1->validate())
{
$model1->save(FALSE);
$success++;
}
else
{
$issueline[$objWorksheet->getTitle()][]= '第' . $line . '行数据存在错误' ;
}
}
}//endif $line > $dataline
}//endforeach 载入数据
Yii::app()->setGlobalState('sid_'.$sid, ($i + 1) / $allsheet * 100);
// $cache['progress']['sid'] = ($i + 1) / $allsheet * 100;
}[/code]通过 另一个方法 获取上面方法执行中间的状态信息[code] public function actionGetprogress($sid)
{
// $cache[];
$app = Yii::app();
$percent = $app->getGlobalState('sid_'.$sid);
$arr = array(
'percent' => $percent,
'sid' => $sid
);
$json_string = json_encode($arr);
echo $json_string;
}
结果最后还是不能获得中间状态的值。。。难道输出一定是在程序的结束才能得到么?
共 3 条回复
圆般
注册时间:2011-03-09
最后登录:1970-01-01
在线时长:0小时0分
最后登录:1970-01-01
在线时长:0小时0分
- 粉丝3
- 金钱590
- 威望0
- 积分590