[SOLVED] My previous and next links aren't working?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

[SOLVED] My previous and next links aren't working?

Post by cturner »

When I test my code it shows a different page to what it is suppose to. For instance when I click on the next link on the first page of the articles it shows ?page=1 in the link. Can someone please tell me why and how I can fix it? Thanks in advance.

Here is my code that I am working with:

Code: Select all

require "config2.php";

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

// Define the number of results per page
$max_results = 1;

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);

// Perform MySQL query on only the current page number's results
$sql = mysql_query("SELECT * FROM items LIMIT $from, $max_results") or die ("Could not select the database because: " . mysql_error());

while($row = mysql_fetch_array($sql)){
    // Build your formatted results here.
    echo $row['date_entered'].'<br>'.$row['article_item']."<br />";
} 

// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM items"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

// Build Page Number Hyperlinks
echo "<center>";

if ($page == 1) {
	echo "Previous ";
} else {
	echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$page\">Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
    }
}

if ($page == $total_pages) {
	echo " Next";
} else {
	echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$page\">Next</a>";
}

echo "</center>";

// and close the database connection
mysql_close();
User avatar
Buddha443556
Forum Regular
Posts: 873
Joined: Fri Mar 19, 2004 1:51 pm

Re: My previous and next links aren't working???

Post by Buddha443556 »

Code: Select all

if ($page == 1) {
	echo "Previous ";
} else {
	echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$page\">Previous</a> ";
}

.....

if ($page == $total_pages) {
	echo " Next";
} else {
	echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$page\">Next</a>";
}
You need to do a little math.

Code: Select all

if ($page == 1) {
	echo "Previous ";
} else {
	echo "<a href=\"".$_SERVER['PHP_SELF']."?page=".($page-1)."\">Previous</a> ";
}

.....

if ($page == $total_pages) {
	echo " Next";
} else {
	echo " <a href=\"".$_SERVER['PHP_SELF']."?page=".($page+1)."\">Next</a>";
}
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: My previous and next links aren't working???

Post by Luke »

Actually, wouldn't it be like...

Code: Select all

if ($page != 1) {
	echo "Previous ";
} else {
	echo "<a href=\"".$_SERVER['PHP_SELF']."?page=".($page-1)."\">Previous</a> ";
}

.....

if ($page != $total_pages) {
	echo " Next";
} else {
	echo " <a href=\"".$_SERVER['PHP_SELF']."?page=".($page+1)."\">Next</a>";
}
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Post by cturner »

Problem is now solved.
Post Reply