Jump to content

Recommended Posts

Hello, I need some help here... PLEASE!

 

I have a php code to split the results of a sql query over pages taht works great...

 

But I need to add some feature, I need to add that if there are more than * pages it shows me "..." instead of the hole numbers of pages...

 

Here is the code to split the pages:

$recordCount = "select count(*) " . $sql;
$totalRowsResult = mysql_query($recordCount);
$totalRows = mysql_fetch_row($totalRowsResult);

if($totalRows[0] > $limit){
     
echo("<div class=\"beginpag\"></div>");

if($page != 1){ 
        $pageprev = $page -1;

	echo("<a href=\"".$_SERVER['REQUEST_URI']."&page=$pageprev\">« Previous </a> - ");  
    }else { 
        echo("« Previous - ");  
 } 	 

    $numofpages = $totalRows[0] / $limit; 

    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo(" " . $i . " - ");
        }else{
            echo(" <a href=\"" . $_SERVER['REQUEST_URI'] . "&page=$i\">$i</a> - "); 
        }
    }   

  if(($totalRows[0] % $limit) != 0) {
  
        if($i == $page) {

            echo(" " . $i . " ");

        } else {

            echo("<a href=\"" . $_SERVER['REQUEST_URI'] . "&page=$i\">$i</a> - ");

        }

    }   
     
if(($totalRows[0] - ($limit * $page)) > 0){
        $pagenext = $page +1;
         
        echo("<a href=\"" . $_SERVER['REQUEST_URI'] . "&page=$pagenext\">Next »</a>");
    }else{
        echo("Next »"); 
    }
    
    
}

 

This shows the pagination like this: Previous - 1 - 2 - 3 -4 - 5 - 6 - Next

But there are some queries that return like 50 pages, so I want it to show (if page 1 is selected): Previous -1 -2-3-4-5-6-7-8-9...49-50-Next

(or if page selected is 30) Previous-1-2...28-29-30-31-32-33...49-50-Next.

 

Any idea please????

 

Thank you all!

Link to comment
https://forums.phpfreaks.com/topic/50869-help-with-mysql-results-pagination/
Share on other sites

Well, I can't help you with exactly what you want, cause I am not real sure how to approach it. I do know that you would make it happen in the for() loop. If your interested, here is a pagination script that will show up as Prev 5 6 7 8 9 Next. It shows 2 pages back and 2 pages forward from page your on now along with prev and next.

 

If you want to adjust how many pages it shows altogether, simply find the for loop and change the -2 and +2 to whatever number you want. It also includes alternating row colors for the data. dunno if this is helpful, but it's there. It is pretty heavily commented as I have posted it here before. I will add a few more to show the alternating row color parts

<?php
// connect and get all records, $total_entries = num of results,
$result = mysql_query("SELECT * FROM TABLE ") or die(mysql_error()); 
// $total entries is the number of rows that was returned in the query
$total_entries = mysql_num_rows($result);
// set max per page to display
$setlimit = 20;

//is $_GET['page'] set, if yes, set $page to it
if(isset($_GET['page']))
{ 
$page = $_GET['page']; 
}
else
{ 
$page = 1; 
} 
$pages = ceil($total_entries / $setlimit);
$offset = ($page - 1) * $setlimit; 

// go get our data
$result = mysql_query("SELECT * FROM TABLE LIMIT $offset, $setlimit") or die(mysql_error()); 
while($row = mysql_fetch_object($result)){ 

// loop through results and  display data
}; 
?>
<table width="90%" align="center" cellpadding="5" cellspacing="0">
<?php
$x=0;

//set a counter to 0   and set $row_count to 0 for our alternating colors
$row_count = "0";  
// set our 2 colors to alternate with
$color1="#333333";
$color2="#666666";
//alternating row color part1 ends here

while($x < count($disks)){ // change this to your results variable


$row_color = ($row_count % 2) ? $color1 : $color2 ; // this is what sets the color for the row
?>
<tr bgcolor="<?=$row_color ?>" align="left" valign="middle">
<td height="20"> display results here</td>
</tr>
<?php
$row_count++; // increment our row count for alternating color
}//endwhile
?>
</table>
<br>
<br>
<center>
<?php
// now we start the prev 123 next part
if($pages >1){ // if we have more than 1 page to display lets make our prev 123 next pat
if($page!=1){  // if we are not on page 1  display a Prev link
?>
<a href="<?=$PHP_SELF ?>?page=<?=($page-1) ?>">Prev</a> 
<?php
}else{};// else don't show a prev link

// loop through using for and the $pages var we calculated earlier for all our pages
for($i = ($page - 2); $i <= ($page + 2); $i++){ // change this here to show more links
if($i == $page){  // If $i == $page , then this is the current page. Don't make it a link. 
echo '<b>'.$i.'</b> '; 
}else{ 
// This is not the current page. Make it a link. 
?>
<a href="<?=$PHP_SELF ?>?page=<?=$i ?>"><?=$i ?></a> 
<?php
}//end else
} //end for
if($page!=$pages){ // if $page not equal to $pages then we are not yet on the last page, so show  next link
?>
<a href="<?=$PHP_SELF ?>?page=<?=($page+1) ?>" >Next</a> 
</center>
<?php
}else{}; // else $page is equal to $pages so its the last page , no next link

};//end if $pages > 1
?> 

 

Sorry the code is kinda hard to read. I started using it a while ago before I got in the habit of indenting my code and making it nice and readable.

 

Hope this helps some.

 

nate

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.