Jump to content

Pagination


pcbytes

Recommended Posts

I can display the page no problem. There are six entries in the database, I limit it to show only 2 per page.

It shows Prev2 and Next 2 as well as current page on the bottom. But they do not show up as hyperlinks. Also too, this is returning that there is only 1 page, when there should be 3. My code is below.

 

<?
//GET FORM DATA
$section = $_POST['section'];
$category = $_POST['category'];

require("connectDB.php"); //DATABASE CONNECTION SEQUENCE

$limit = 2; //VARIABLE TO LIMIT RESULTS SHOWN PER PAGE
$result_count   = mysql_query("SELECT count(*) FROM upload");				// Pulls what we want from the DB
$totalrows  	= mysql_num_rows($result_count);							// Counts Number of Rows
if(empty($page)){    														// Checks if the $page variable is empty (not set)
        $page = 1;     													    // If it is empty, we're on page 1
} 
$limitvalue = $page * $limit - ($limit); 								    // Ex: (2 * 25) - 25 = 25 <- data starts at 25 

//GET DATABASE RESULTS BASED ON CATEGORY PICKED
$result = mysql_query("select * from upload where category = '$category' limit  $limitvalue, $limit");

//FETCH SQL DATA AND PRINT IT TO THE SCREEN
while($row = mysql_fetch_array($result)){
$id = $row["id"];
$codes = $row["codes"];
// $codes = ereg_replace("INSERTURLHERE", , $codes); NOT CURRENTLY USING THIS FEATURE
print '<table width="400" border="2" cellspacing="0" cellpadding="0">';

//DISPLAY THUMBNAIL IMAGE FROM DATABASE
print ("<tr><td><img src=\"download.php?id=$id\"></td></tr>"); 

//POPULATE TEXTFIELD WITH CSS CODE FOR TEMPLATE
print "<tr><td><textarea name='textfield' wrap='OFF' cols='50' rows='7'>".$codes."</textarea></td></tr>";
print '</table><br /><br />';
}
if($page != 1){ 
        $pageprev = $page--;
        // Fancy way of subtracting 1 from $page
        
        print "<a href='index.php&page=$pageprev'>PREV ".$limit."</a>";  
    }else
        print "PREV $limit  ";
        // If we're on page 1, PREV is not a link 

$numofpages = $totalrows / $limit; 
/* We divide our total amount of rows (for example 102) by the limit (25). This 
will yield 4.08, which we can round down to 4. In the next few lines, we'll 
create 4 pages, and then check to see if we have extra rows remaining for a 5th 
page. */ 
    
    for($i = 1; $i <= $numofpages; $i++){
    /* This for loop will add 1 to $i at the end of each pass until $i is greater 
    than $numofpages (4.08). */ 
   
        if($i == $page){
            print "$i";
        }else{
            print "<a href='index.php&page=$i'>$i</a> "; 
        }
        /* This if statement will not make the current page number available in 
        link form. It will, however, make all other pages available in link form. */
    }   // This ends the for loop


    if(($totalrows % $limit) != 0){
    /* The above statement is the key to knowing if there are remainders, and it's 
	all because of the %. In PHP, C++, and other languages, the % is known as a 
	Modulus. It returns the remainder after dividing two numbers. If there is no 
	remainder, it returns zero. In our example, it will return 0.8 */ 
        if($i == $page){
            print "$i";
        }else{
            print "<a href='index.php&page=$i'>$i</a>";
		/* This is the exact statement that turns pages into link form that is used 
		above */  
        }
    }   // Ends the if statement


if(($totalrows - ($limit * $page)) > 0){
			//This statement checks to see if there are more 
			//rows remaining, meaning there are pages in front
			//of the current one.
    
        $pagenext   = $page++;
       			 // Fancy way of adding 1 to page
         
        print "<a href='index.php?page=$pagenext'>NEXT ".$limit."</a>";
      	    	//Since there are pages remaining, this outputs NEXT in link form.
    }else{
        print "NEXT $limit"; 
        		//If we're on the last page possible, 
			//NEXT will NOT be displayed in link form.
    }

require("disconnectDB.php");
?>

Link to comment
https://forums.phpfreaks.com/topic/43492-pagination/
Share on other sites

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.