Answer the question
In order to leave comments, you need to log in
How to loop through an array in yii2 pjax?
When I press the button, the variable is subtracted and added.
Everything is fine, but the variable is assigned zero and ++/--;
It is necessary that the number is saved and not reset!
public function actionIndex()
{
$model = new Images();
$model = $model->find()->all();
$i=0;
if(Yii::$app->request->get('arrow')){
$arrow = $index = Yii::$app->request->get('arrow');
if($arrow == 1){
$i++;
}else if($arrow == 0){
$i--;
}
}else{
//Yii::$app->session->get('i', 0);
}
return $this->render('index',[
'model'=>$model,'i'=>$i]);
}
<? Pjax::begin();?>
<img src="web/img/photos/<?=$model[$i]->id.'/'.$model[$i]->src?>" alt="Marsa Alam underawter close up" />
<? Pjax::end();?>
Answer the question
In order to leave comments, you need to log in
In order for something to be preserved, it must be stored somewhere. In the database, in the cache, in the session, or somewhere else. You can stupidly pass the result back, write it to the data attribute or localStorage and then form ajax with a parameter. Accordingly, the calculations are carried out from the incoming parameter.
Variables have a scope. In this case, it is limited to the actionIndex function, and when the function is executed, the variables in it no longer exist.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question