Jump to content

Pagaination Tutorial Doesn't Work


cavey5

Recommended Posts

I used this tutorial:

http://www.phpfreaks.com/tutorials/43/0.php

 

I copies his code and simply changed my database login info

and the names of the fields to display and here is what I get:

 

http://www.atticuspubs.com/organic/pagination.php

 

There are about 300 records that match my search query, what gives?

It will display as many results as I put as the limit, if I put 50,

it lists out 50... but it won't paginate... WTF?

 

<?php

    // Makes initial conection to database
define ('DB_USER', '*********');
define ('DB_PASSWORD', '*********');
define ('DB_HOST', 'localhost');
define ('DB_NAME', '*********');

$connect = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
  	or die('215 Our database is currently down for updates, please check back later.'); 
	// or die(mysql_error());

$db = @mysql_select_db(DB_NAME, $connect)
or die('216 Our database is currently down for updates, please check back later.');

    $limit          = 12;               
    $query_count    = "SELECT count(*) FROM subscriber_data";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count); 

    if(empty($page)){
        $page = 1;
    }
        

    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM subscriber_data LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error()); 

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

    $bgcolor = "#E0E0E0"; // light gray

    echo("<table>");
    
    while($row = mysql_fetch_array($result)){
        if ($bgcolor == "#E0E0E0"){
            $bgcolor = "#FFFFFF";
        }else{
            $bgcolor = "#E0E0E0";
        }

    echo("<tr bgcolor=".$bgcolor."><td>");
    echo($row["id"]);
    echo("</td><td>");
    echo($row["firstname"]);
    echo("</td></tr>");
    }

    echo("</table>");

    if($page != 1){ 
        $pageprev = $page--;
        
        echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> "); 
    }else{
        echo("PREV".$limit." ");
    }

    $numofpages = $totalrows / $limit; 
    
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
        }
    }


    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
        }
    }

    if(($totalrows - ($limit * $page)) > 0){
        $pagenext = $page++;
         
        echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>"); 
    }else{
        echo("NEXT".$limit); 
    }
    
    mysql_free_result($result);

?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/66813-pagaination-tutorial-doesnt-work/
Share on other sites

ok dont want to see or read your code but paging is done by the following

1. get the total page and divide the page in you desired limit

2. get the ofset value of your query something like limit 10 , 20

3 get the query string (url page no) to determine what is the page to navigates

 

or echo this one

 

  $query  = "SELECT * FROM subscriber_data LIMIT $limitvalue, $limit";   

Okay, I found another tutorial but it doesn't work either, what the hell is up with these tutorials?

 

Here is a link to my test page:

http://www.atticuspubs.com/test/search.php

 

The code is below. If you were to type in "98" you should get a list of customers in the 98*** zip codes,

which you do for the first 5 results, but then if you click page 2 or any other page, it goes haywire and

lists all the customers, and no longer do they have 98*** zip codes. Where is my error?

 

Also, you'll see this code commented out:

 

//If($SearchString == "") {

//Echo"Nothing to Search For";

//exit();

 

If I uncomment it, the if statement fails and it displays "Nothing to Search For"

every time, even if I know it is a good search term... why is that?

 

 

 

<?php

    // Makes initial connection to database
    define ('DB_USER', '********');
    define ('DB_PASSWORD', '********');
    define ('DB_HOST', 'localhost');
    define ('DB_NAME', '********');

    $connect = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
  	    or die(mysql_error()); 

      $db = @mysql_select_db(DB_NAME, $connect)
      or die(mysql_error()); 
      
  $SearchString = $_POST['searchterm'];

      $Limit = 5; //Number of results per page
  
  If($SearchString == "") 
  
  $SearchString=$_GET["SearchString"]; // Get the search term from the navigation (GET)
  //If($SearchString == "") {
  //Echo"Nothing to Search For";
  //exit();


  $page=$_GET["page"]; //Get the page number to show
  If($page == "") $page=1; //If no page number is set, the default page is 1

  //Get the number of results
  $SearchResult=mysql_query("SELECT * FROM subscriber_data WHERE zipcode LIKE '%$SearchString%' ORDER BY zipcode") or die(mysql_error());
  $NumberOfResults=mysql_num_rows($SearchResult);

  //Get the number of pages
  $NumberOfPages=ceil($NumberOfResults/$Limit);

  //Get only the relevant info for the current page using LIMIT
  $SearchResult=mysql_query("SELECT * FROM subscriber_data WHERE zipcode LIKE '%$SearchString%' ORDER BY zipcode LIMIT " . ($page-1)*$Limit . ",$Limit") or die(mysql_error());
?>

 


<?php
	//Print the Titles
	While($row = mysql_fetch_object($SearchResult)) {
	Echo $row->firstname . " " . $row->lastname . " " . $row->zipcode . "<br />";

	} 
	?>
	<br /><br />
	<?php
	//Create and print the Navigation bar
	$Nav="";
	For($i = 1 ; $i <= $NumberOfPages ; $i++) {
	If($i == $page) {
	$Nav .= "<B>$i</B>";
	}Else{
	$Nav .= "<A HREF=\"searchresults5.php?page=" . $i . "&SearchString=" .urlencode($SearchString) . "\">$i</A>";
	}
	}

	?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.