Jump to content

Php Pagination


Deanznet

Recommended Posts

Hey i was reading the tutorial.. and i tried doing it..

 

I got it to limit the display but the links for the page dont work.

 

The previous and the next links don't work. Cant click them.

 

Any idea what i did wrong?

 

 

<?
ob_start();//output buffering
include("include/common.php");

function washdata($input) { if(preg_match('/^[0-9a-zA-z]*$/', $input) == 1) { return true; } else { return false; } }

if(washdata($_POST['keyword_query']) == true) {
$keyword_query = mysql_real_escape_string($_POST['keyword_query']);//we need to sanitize ANY and ALL user input, to prevent sql injection and yada-yada

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

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

    $limitvalue = $page * $limit - ($limit);  




$query = @mysql_query("SELECT * FROM images WHERE keywords LIKE '%$keyword_query%'  LIMIT $limitvalue, $limit ") or die(mysql_error());

if(!mysql_num_rows($query)){//0 = false, 1 or more = true
die("I'm sorry, no results were found for $keyword_query.");
	}
		echo '<div class="tableborder">
		<table cellpadding="4" cellspacing="1" border="0" width="100%">
		<tr>';
$rowcounter = 0;
while($row = mysql_fetch_array($query)){


			$file      = $row['filename'];
			$class     = (($class == 'row1') ? 'row2' : 'row1');

			if($count == 4){
				echo '</tr><tr>';
				$count = 0;
			}
			$count++;	
			echo "<td class='{$class}' valign='top'><center>
			<a href='viewer.php?file={$file}'><b>{$file}</b></a><br><br>
			<a href='viewer.php?file={$file}'><img src='http://sharedimage.net/uploads/{$file}' width='30' height'30'  alt='{$file}' class='galthumb' /></a><br><br>
			{$size}<br>
			{$date}<br>
			{$rating['html']} ( {$rating['votes']} vote(s) ) <br>
			<a href='download.php?file={$file}'><b>Download Now</b></a>
			</center></td>";
		}
		echo '</tr></table></div>';

    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($query);//clear off memory of all data from this query, just in case it returns a lot of results and takes up memory on the sever, helps w/ server load
} else {
echo "Only letters and numbers please.";
}
?>

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

i'm having the same problem for my pagination that i'm trying to implement into my news system. the limiting works fine but the links are wrong and doesn't work. I've tried posting here, on phpbuilder, pixel2life, and biorust.com forum where i got the tutorial but still no luck of help  :(

Link to comment
https://forums.phpfreaks.com/topic/81817-php-pagination/#findComment-415658
Share on other sites

Yea I had the same problem when I tried the

PHP Pagination tutorial from here, but I fixed it,

heres my script,

 

(you may need to edit some info!)

 

<?php
$localhost = 'localhost';
$user = 'username';
$password = 'password';
$database = 'db';

    $mconn = mysql_connect($localhost, $user, $password);
    mysql_select_db($database, $mconn) or die(mysql_error());

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

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

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

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

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


    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo('<font color=red>'.$i."</font> | ");
        }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><br><br>"); 
    }else{
        echo("<br><br>"); 
    }

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

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

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

    echo '<a href="'.$row["file"].'">'.$row["name"].'</a><br><br>';
    }


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

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


    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo('<font color=red>'.$i."</font> | ");
        }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(""); 
    }
    
    mysql_free_result($result);
mysql_close($mconn);
?>

 

By the way, I have my page numbers on the top and bottom of the results

Link to comment
https://forums.phpfreaks.com/topic/81817-php-pagination/#findComment-415722
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.