Y
Y
yativ_sobb2017-04-16 15:50:32
Yii
yativ_sobb, 2017-04-16 15:50:32

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]);
    }

views
<? 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

1 answer(s)
M
Maxim Timofeev, 2017-04-16
@yativ_sobb

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 question

Ask a Question

731 491 924 answers to any question