Reputation: 1422
I want to make a minimalistic pagination script that basically does three things:
I have most of the code worked out, but I'm just making some if/elseif statements that determine which page the user is on and I'm having a bit of trouble. (those at bottom) First, here's the query code:
$per_page = 10;
$pages_query = mysql_query("SELECT COUNT(idnum) FROM images");
$pages = ceil(mysql_result($pages_query, 0) / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page'] :1;
$start = ($page -1) * $per_page;
$query = mysql_query("SELECT * FROM images ORDER BY idnum DESC LIMIT $start, $per_page");
And here's the if statement part:
$nextend = $pages - 1;
$next = $page + 1;
$previous = $page - 1;
if ($pages >= 1 && $page = 1) {
echo '<a href="?page='.$next.'">next</a>';
} elseif ($pages >= 1 && $page = 2) {
echo '<a href="?page='.$previous.'">previous</a>';
}
It always results in the next button, no matter what page I'm on. How do I detect the page number so I can display the pagination buttons the way I want to? By the way, I know I don't have the else statement for the middle pages (next and previous) yet.
Upvotes: 0
Views: 214
Reputation: 1587
What you've got to do is way simple:
Please note that both those conditions can happen at the same time so using an elseif between them won't work as it will only allow one of them to execute.
Example:
if ( $page > 1 )
{
echo( "Previous" );
}
if ( $page < $pages )
{
echo( "Next" );
}
Upvotes: 0
Reputation: 1129
$nextend = $pages - 1;
$next = $page + 1;
$previous = $page - 1;
$maxpages = ? //You need to have a variable with the last page number
if ($pages > 1) {
echo '<a href="?page='.$previous.'">previous</a>';
}
if ($page < $maxpages) {
echo '<a href="?page='.$next.'">next</a>';
}
I don't understand what logic you are trying to do with your if/else statements, also when checking if a variable is equal to a number/other variable your "==" not "="
Upvotes: 0
Reputation: 70075
You are assigning in your if statements rather than comparing. You don't want this in your if statement:
$page = 1
That just assigns 1
to $page
.
You want this:
$page == 1
Or this:
$page === 1
Upvotes: 4