Jump to content

Pagination question


Gruzin

Recommended Posts

Hey guys,

 

I have pagaination script wich works fine, but I have one problem - it displays all pages, and there could be 5,000 pages, lol. so how can I display pages like this:

 

Previous 1,2,3,4,5....789 Next

 

here is a part of a script:

 

if($page > 1){ 
    $prev = ($page - 1); 
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">Previous</a> "; 
} 

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){ 
        echo "$i";
        } else { 
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; 
    } 
} 

// Build Next Link 
if($page < $total_pages){ 
    $next = ($page + 1); 
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next</a>"; 
} 

 

Thanks for any advice,

George

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

for($i = 1; $i <= 5; $i++){
    if(($page) == $i){ 
        echo "$i";
        } else { 
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; 
    } 
} 

echo '...';

for($i = $total_pages-5; $i <= $total_pages; $i++){
    if(($page) == $i){ 
        echo "$i";
        } else { 
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; 
    } 
} 

 

you may want to do it like php forum does:

 

1,2,3,4,5...66,67,68,69,70...99,100,101

 

if you were on page 68 and there were 101 pages in total.

 

Hope this gives you some ideas.

 

monk.e.boy

Link to comment
https://forums.phpfreaks.com/topic/42854-pagination-question/#findComment-208067
Share on other sites

<?php

$tmp = Array();

for($i = 1; $i <= 5; $i++)
  $tmp[] = '<a href="'. $_SERVER['PHP_SELF'] .'?page=$i">$i</a>'; 

echo implode( ',', $tmp );
echo '...';

if( $page > 5 && $page < $total_pages-5 )
{
  $tmp = Array();

  for($i = $page-2; $i <= $page+2; $i++)
  {
    if( $i == $page )
      $tmp[] = '<b><a href="'. $_SERVER['PHP_SELF'] .'?page=$i">$i</a></b>'; 
    else
      $tmp[] = '<a href="'. $_SERVER['PHP_SELF'] .'?page=$i">$i</a>'; 
  }
  echo implode( ',', $tmp );
  echo '...';
}

$tmp = Array();

for($i = $total_pages-5; $i <= $total_pages; $i++)
  $tmp[] = '<a href="'. $_SERVER['PHP_SELF'] .'?page=$i">$i</a>'; 

echo implode( ',', $tmp );


?>

 

You will have to debug this as I can't be bothered to run it, but it should give you something like you are after.

 

Note: the arrays and the implode. These give you : "1,2,3,4" with very little work.

Note: the code will go wonky if $page is close to 5 or close to $total_pages-5, you may get : 1,2,3,4,5...5,6,7,8,9...100,101,102,103

 

monk.e.boy

Link to comment
https://forums.phpfreaks.com/topic/42854-pagination-question/#findComment-208101
Share on other sites

Hey guys, here is my code:

 

<?php
$con = mysql_connect("localhost","xxx","xxx"); //connect to db
if(!$con){
  die('Error:'.mysql_error());
  }
  
mysql_select_db("xxx", $con); //select db

if(!isset($_GET['page'])){ 
    $page = 1; 
} else { 
    $page = $_GET['page']; 
} 

$max_results = 10; 
$from = (($page * $max_results) - $max_results);  

$query = "SELECT * FROM link_manager ORDER BY name LIMIT $from, $max_results"; 
$query2 = "SELECT * FROM link_manager"; 

$result = mysql_query($query) or die("Error: " . mysql_error());
$result2 = mysql_query($query2) or die("Error: " . mysql_error()); 
$num = mysql_num_rows($result);
$num2 = mysql_num_rows($result2);

$total_results = $num2; 
$count = (($max_results*$page)-$max_results)+1;

$bgcolor = "#E9E9E9";

echo "<table border='0' cellpadding='1' cellspacing='1' width='100%'> 
<tr>
<th bgcolor='#4B8DA0' align='center'><span class='style1'><strong>$num2 Links in our Database</strong></span></th>
</tr>";
echo("</table>");

echo "<table border='0' cellpadding='0' cellspacing='1' width='100%'> 
<tr>
<td bgcolor='#4B8DA0' align='left' width='30'><span class='style1'>N</span></td>
<td bgcolor='#4B8DA0' align='center'><span class='style1'>Name</span></td>
<td bgcolor='#4B8DA0' align='center'><span class='style1'>URL</span></td>
<td bgcolor='#4B8DA0' align='center'><span class='style1'>Page Rank</span></td>
<td bgcolor='#4B8DA0' align='center'><span class='style1'>Comment</span></td>
<td bgcolor='#4B8DA0' align='center'><span class='style1'>Edit</span></td>
<td bgcolor='#4B8DA0' align='center'><span class='style1'>Delete</span></td>
</tr>";

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

$link = $row['id']; //get link id form db

$edit = "edit.php?link_id=$link"; // edit link
$delete = "delete.php?link_id=$link"; // delete link
  
  echo "<tr>";
  echo "<td bgcolor='$bgcolor' align='left'>$count</td>";	
  echo "<td bgcolor='$bgcolor' align='left'>".$row['name']."</td>";	
  echo "<td bgcolor='$bgcolor' align='left'>".$row['url']."</td>";
  echo "<td bgcolor='$bgcolor' align='center'>".$row['rank']."</td>";	
  echo "<td bgcolor='$bgcolor' align='left'>".$row['comment']."</td>";
  echo "<td bgcolor='$bgcolor' align='center'><a href='$edit'>Edit</a></td>";	
  echo "<td bgcolor='$bgcolor' align='center'><a href=\"" . $delete . "\" onClick=\"return confirm('Are you sure you want to delete this link?');\">Delete</a></td>";	
  echo "</tr>";
  $count++;
} 

echo("</table>");

$total_pages = ceil($total_results / $max_results); 

if($num2 > $max_results){

echo "<center><br>Select a Page <br>"; 


if($page > 1){ 
    $prev = ($page - 1); 
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">Previous</a>"; 
} 

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){ 
        echo "$i";
        } else { 
            echo " <a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; 
    } 
}

if($page < $total_pages){ 
    $next = ($page + 1); 
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next</a>"; 
} 
echo "</center>"; 
}
?>

 

Any help will be greatly appreciated,

 

George

Link to comment
https://forums.phpfreaks.com/topic/42854-pagination-question/#findComment-210423
Share on other sites

I wrote the code you asked for. What else can we do to help  ??? ??? ???

 

monk.e.boy

 

I think he's having a problem implement your script into his script to make it workable.

 

That's right, lol. I did not ask anyone to write a script for me, just asking for right direction (I thought this forum was for that).

 

 

Thanks anyway....

Link to comment
https://forums.phpfreaks.com/topic/42854-pagination-question/#findComment-210581
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.