Q:

How to go to previous images in Instagram php api?

Im using PHP-Instagram-API to pick out images. Instagram only allows you to make 20 requests at a time and because of this i want to create a pagination. The only thing is, i can only figure out how to do this forward and not backwards. This is how I create my "next" link who successfully prints out the next 20 images:
$media = $current_user->getMedia( isset( $_GET['max_id'] ) ? array( 'max_id' => $_GET['max_id'] ) : null );
if( $media->getNextMaxId() ): 
?>
<a href="instagram.php?max_id=<?php echo $media->getNextMaxId() ?>>Next</a>
<?php
endif;

How can i get the previous images? Then i have to select the ID of the last image in the previous page and put it as max_id. But I haven't been able to figure out a way to do this.

A:

I solved it by storing max_id in sessions like this:
if (!isset($_GET['max_id'])) {
    $page = NULL;
} else {
    $page = $_GET['max_id'];
}

if (!isset($_GET['pg'])) {
    $pg = 1;
} else {
    $pg = $_GET['pg'];
}

if (isset($_SESSION['max_id'][$pg])) {
    $page = $_SESSION['max_id'][$pg];
} else {
    $_SESSION['max_id'][$pg] = $page;
}
$previous = $pg - 1;

And then the html like this:
<a href="instagram.php?max_id=<?php echo $_SESSION['max_id'][$previous] ?>&pg=<?php echo ($pg - 1); ?>">Previous</a>
<a href="instagram.php?max_id=<?php echo $media->getNextMaxId() ?>&pg=<?php echo ($pg + 1); ?>">Next</a>

