Hi, This is a reaction to a post(http://www.daniweb.com/forums/thread1720.html) thats a bit old, and I wanted to ask something, but it suggested I'd better start a new post. I'm sorry if theres another post going about this, but I couldnt really find the search function (just joined).

First of all I want to say it's a very nice and clean script but im having some problems getting it to work. I dont get any errors but it just refuses to go to a next record, when I click one of the buttons it just stays on the same page and nothing changes. I'm testing this with a database with 6 records, and it does display the links to go to the next page but it just doesnt work.

Here's the changed part of my code:

$db = mysql_connect($server, $user, $pass)or die(mysql_errno() . ": " . mysql_error() . "<br>");
	mysql_select_db($databasename, $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");
    $sql = "SELECT * FROM contacts ";
    $query = mysql_query($sql,$db);
    $total_results = mysql_num_rows($query);
    $limit = "1"; //limit of archived results per page.
    $total_pages = ceil($total_results / $limit); //total number of pages

    if (empty($page)){
		$page = "1"; //default page if none is selected
    }
    $offset = ($page - 1) * $limit; //starting number for displaying results out of DB
    $query = "SELECT * FROM contacts LIMIT $offset, $limit ";
    $result = mysql_query($query);

    //This is the start of the normal results...

    while ($row = mysql_fetch_array($result)){
	    echo "<h1><center>" . $row['Naam'] . "(" .  $row['Leeftijd'] . ")</center></h1><br />\n";
    }
    mysql_close();

I've been trying to fix a previous/next script all day but I keep running into the same problem. Have no idea why it doesnt work, because nobody seems to have the same trouble.

Hoping someone can help,

Thanks in advance

I think you have not intialized $page anywhere in the code. so line number 9 always sets $page to 1.

Member Avatar for rajarajan2017
if (empty($page)){		$page = "1"; //default page if none is selected    }

First time if $page is empty then you intialized to $page=1; for next records I think its become empty or even the 1 already you had set. So everytime the page value going to be 1. or somewhere the you changed the $page when you click the button?

Do you post the full code with your buttons?

Hi like i said, I used a script from this forum and I didnt want to spam duplicate shit here, but here is my complete php script.

$server="localhost"; // Host name
	$user="anti"; // Mysql username
	$pass="d3sn13w04r"; // Mysql password
	$databasename="anti_contact_data"; // Database name
    $db = mysql_connect($server, $user, $pass)or die(mysql_errno() . ": " . mysql_error() . "<br>");
	mysql_select_db($databasename, $db) or die(mysql_errno() . ": " . mysql_error() . "<br>");
    $sql = "SELECT * FROM contacts ";
    $query = mysql_query($sql,$db);
    $total_results = mysql_num_rows($query);
    $limit = "1"; //limit of archived results per page.
    $total_pages = ceil($total_results / $limit); //total number of pages

    if (empty($page)){
		$page = "1"; //default page if none is selected
    }
    $offset = ($page - 1) * $limit; //starting number for displaying results out of DB
    $query = " SELECT * FROM contacts LIMIT $offset, $limit ";
    $result = mysql_query($query);

    //This is the start of the normal results...

    while ($row = mysql_fetch_array($result)){
	    echo "<h1><center>" . $row['Naam'] . "(" .  $row['Leeftijd'] . ")</center></h1><br />\n";
    }
    mysql_close();
 
    // This is the Previous/Next Navigation
    echo "<font face=Verdana size=1>";
    echo "Pages:($total_pages)&nbsp;&nbsp;"; // total pages
    if ($page != 1){
	    echo "<a href=$PHP_SELF ?page=1><< First</a>&nbsp;&nbsp;&nbsp;"; // First Page Link
	    $prevpage = $page - 1;
	    echo "&nbsp;<a href=$PHP_SELF ?page=$prevpage><<</a>&nbsp;"; // Previous Page Link
    }
    if ($page == $total_pages){
		$to = $total_pages;
    }
    elseif ($page == $total_pages-1){$to = $page+1;}
    elseif ($page == $total_pages-2){$to = $page+2;}
    else{$to = $page+3;}
    if ($page == 1 || $page == 2 || $page == 3){$from = 1;}
    else{$from = $page-3;}
    for ($i = $from; $i <= $to; $i++){
	    if ($i == $total_results) $to=$total_results;
    	if ($i != $page){
	    	echo "<a href=$PHP_SELF ?showold=yes&page=$i>$i</a>";
        }

        else{echo "<b><font face=Verdana size=2>[$i]</font></b>";}
        if ($i != $total_pages)echo "&nbsp;";
	}
    if ($page != $total_pages){
	    $nextpage = $page + 1;
        echo "&nbsp;<a href=$PHP_SELF ?page=$nextpage>>></a>&nbsp;"; // Next Page Link
        echo "&nbsp;&nbsp;&nbsp;<a href=$PHP_SELF ?page=$total_pages>Last >></a>"; // Last Page Link
    }
    echo "</font>";

at line no 12 write following code

$page=$_REQUEST['page'];

You are my hero !! :) thanks a lot man!! I'll mark the thread as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.