$nav_next_id = $page_id + 1;
$sql = "SELECT page FROM plm WHERE id = '$nav_next_id'"; {
$result = mysql_query($sql);
while ($next_page = mysql_fetch_array($result))
{
if($page_id + 1 > 24)
{
$goto_next_page = 'some_page.php';
}
else
{
$goto_next_page = $next_page['page_title'];
}
}
}
Basically a very straight forward next page button on simple site. Using the ID in the MYSQL table I can determine the next page from the current page.
All works well until I come to the last page. Which is page 24.
So i put an if
in the loop saying if the page is > 24 go to some_page.php.
I am echoing $goto_next_page
in my html.
However nothing is echoed from page 24, all other pages work fine. What wrong with this script?
---------UPDATE---------------- THIS IS HOW I GOT IT TO WORK ------------------
if($cur_page_id == 24)
{
$goto_next_page = 'some_page.php';
}
else
{
$sql = "SELECT page FROM plm WHERE id = '$nav_next_id'"; {
$result = mysql_query($sql);
while ($next_page = mysql_fetch_array($result))
{
$goto_next_page = $next_page['page_title'];
}
}
}
Perhaps you meant to use the pre-increment operator:
if(++$page_id > 24)
Effectively, this means:
$page_id = $page_id + 1;
if($page_id > 24) {
This operator as well as its brother operator (the post-increment operator) both add 1 to the variable. However, the pre-increment operator adds one and then returns the new value. The post-increment operator, on the other hand, returns the value of the variable and then increments it by 1.
The reason why your code doesn't work is that $page_id + 1 > 24
doesn't increment $page_id
so it always remains the same. This makes sense because if you did:
$a = $page_id + 1;
You wouldn't expect $page_id
to be incremented. You would expect $a
to be 1 more than $page_id
, but you'd expect $page_id
to be the same.
The answer is very simple: the value of $page_id is not changing inside your loop.
<?php
$nav_next_id = $page_id + 1;
$sql = "SELECT page FROM plm WHERE id = '$nav_next_id'";
$result = mysql_query($sql);
while ($next_page = mysql_fetch_array($result))
{
if($page_id + 1 > 24)
{
$goto_next_page = 'some_page.php';
}
else
{
$goto_next_page = $next_page['page_title'];
}
}
?>
Dude you are not using the ID of the page in the loop that is why you are always getting the same result making it look like the if else is not working:
<?php
$nav_next_id = $page_id + 1;
$sql = "SELECT page FROM plm WHERE id = '$nav_next_id'";
$result = mysql_query($sql);
while ($pages = mysql_fetch_array($result)){
if ($pages['id'] > 24) {
$goto_next_page = 'some_page.php';
}
else {
$goto_next_page = $pages['page_title'];
}
}
?>