I
I
Ivan Lykov2018-05-29 16:54:33
Yii
Ivan Lykov, 2018-05-29 16:54:33

How to transition between blog posts?

Good afternoon,
The task is to make transitions between posts on a single page. Two buttons next and previous
At the moment I have a transition from fluid to the very first tobish.
previous(1) - current(2) - next(3)
previous(1) - current(3) - next(4)
previous(1) - current(4) - next(5)
previous(1) - current(5) ) - next(6)
and so on.
But how can I fix the code that is in the model, so that it would display the type
previous (1) - current (2) - next (3)
previous (2) - current (3) - next (4)
previous (3) - current (4) - next(5)
previous(4) - current(5) - next(6)
How can I properly sort this whole thing?
code in the model

public static function getNextOrPrevEventPost($id_event,$operator)
  {
    $data = [];
    switch($operator) {
      case '<' :
        $data = Events::find()
          ->select('id,title,image')
          ->where(['<','id',$id_event]);
        break;
      case '>' :
        $data = Events::find()
          ->select('id,title,image')
          ->where(['>','id',$id_event]);
        break;
    }
    return $data->one();
  }

Controller code just in case
public function actionView()
    {
        $getIdEvents = Yii::$app->request->get( 'id' );

        $postPaginationEventPrev = Events::getNextOrPrevEventPost($getIdEvents,'<');
        $postPaginationEventNext = Events::getNextOrPrevEventPost($getIdEvents,'>');

        $getSingleEvent = Events::find()
            ->where( ['id' => $getIdEvents] )
            ->all();

        return $this->render( 'view', [
            'getSingleEvent' => $getSingleEvent,
      'postPaginationEventPrev' => $postPaginationEventPrev,
      'postPaginationEventNext' => $postPaginationEventNext,
        ] );
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-05-29
@Jhon_Light

$before = Blog::find()->select( 'id, title, image' )->where( [ '<', ' id', $this->id] )->orderBy('id DESC')->one();
$after = Blog::find()->select( 'id, title, image' )->where( [ '>', ' id', $this->id] )->orderBy('id ASC')->one();

Perhaps you need to swap DESC and ASC, I constantly confuse which way. The idea is to sort in descending order those that are less than the current one, then the first of them is what we need, so we select it accordingly. And vice versa, larger than the current one in reverse sorting.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question