我正在为我的项目使用Yii2框架。 我的控制器中有一个循环,这是代码
public function actionResult() {
$connection = \Yii::$app->db;
$searchModel = new HeaderSearch();
$dataProvider = new ActiveDataProvider([
'query' => Header::find()->
where(['user_id' => Yii::$app->user->identity->id]),
'pagination' => [
'pageSize' => 24,
],
]);
$val = base64_decode($val);
$txt = base64_decode($txt);
$arrayVal = explode(",", $val);
unset($arrayVal[count($arrayVal) - 1]);
$arrayHeader = explode(",", $txt);
unset($arrayHeader[count($arrayHeader) - 1]);
$headerArray = null;
for ($i = 0; $i < count($arrayVal); $i++) {
$headerArray[$arrayVal[$i]] = $arrayHeader[$i];
}
$userId = Yii::$app->user->id;
$modelUser = User::find()->where(['id' => $userId])->one();
$parentId = $modelUser->parent_id;
$sql = $connection->createCommand("SELECT list_header FROM customize_header WHERE user_id=$userId");
$modelData = $sql->queryColumn();
$arrayData = json_decode($modelData[0]); //all data in array form
$countData = count($arrayData); // count rows of data except header row
$modelUser = User::find()->where(['id' => Yii::$app->user->id])->one();
$data = CustomizeHeader::find()->all();
$command = "DELETE FROM header WHERE user_id = '$userId'";
$query = Yii::$app->db->createCommand($command)->execute();
foreach ($arrayHeader as $value) {
$az = preg_replace('/^([0-9]{1,2})(_.*)/i', "\\2", $value);
$data = ltrim($az, '_');
$command = "INSERT INTO header(nama_header, user_id, parent_id) VALUES('$data', '$userId', '$modelUser->parent_id')";
$query = Yii::$app->db->createCommand($command)->execute();
}
$countHeader = count($arrayHeader); //count of selected header
$j = 0;
$point = floor($countData / 4);
$percent = 0;
for ($k = $j; $k < $countData; $k++) {
if($k % $point == 0 && $k > 0){ //Special Condition
$percent = $percent + 10; // Special Variable
}
$keyData = array_keys($arrayData[$k]);
$countKeyData = count($keyData);
for ($x = $k; $x <= $countData; $x++) {
foreach ($arrayHeader as $key => $value) {
$headerId[] = preg_replace('/^([0-9]{1,2})(_.*)/i', "\\1", $value);
}
$n[$k] = [];
for ($a = 0; $a < $countHeader; $a++) {
for ($b = 0; $b < $countKeyData; $b++) {
if ($headerId[$a] == $keyData[$b]) {
$n[$k][] = $arrayData[$k][$b];
}
}
}
}
}
//some code
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'userId' => $userId,
]);
在上面的代码中,我的条件if($k % $point == 0 && $k > 0)
我将评论标记为特殊条件,其中包含变量$percent
,我将评论标记为特殊变量。我希望每当条件为 true 时,将此变量(包含值)发送到 upload.js 。
因此,在操作进行过程中,变量将实时发送到 upload.js 。
function displayResult() {
$('#progress').show();
var x = document.getElementById('bootstrap-duallistbox-selected-list_CustomizeHeader[list_header][]');
if (x.length == 24) {
var txt = "";
var val = "";
for (var i = 0; i < x.length; i++) {
txt += x[i].text + ",";
val += x[i].value + ",";
document.getElementById("progress").innerHTML="<div style=\"width:'percent';background-color:#ddd;\"> </div>"; //Here
}
window.location = 'result?txt=' + btoa(txt) + '&val=' + btoa(val);
} else {
alert("At least 24 Headers!");
}
}
我使用此脚本在进度条的 view.php , width 中显示进度条(我设置的行代码)评论标记为Here
)取决于已从控制器传递到此脚本的$percent
。
知道如何解决这个问题吗? 谢谢你:)
答案 0 :(得分:1)
在这种情况下,您应该在返回数组中传递正确的值,然后在视图中管理正确的用途 例如:假设您需要的值在$ percent var
中在您的控制器中
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'userId' => $userId,
'percent' => $percent
]);
在您的视图中,您可以检查正确使用
.......
<?php
if ( $percent > 0) {
echo "<div>". $percent . "</div>";
}
?>
.....
答案 1 :(得分:0)
if ($percent) {
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'userId' => $userId,
'percent' => $percent
]);
}
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'userId' => $userId,
]);
我对你有用的洞。