Jump to content

[SOLVED] Pagination - If $page > 10?


Northern Flame

Recommended Posts

I created a Pagination tutorial that I plan to submit to

phpfreaks soon, but someone asked me to add something

and I thought I'd try to add this before submitting it here.

 

Someone recommended that I tell the script only to display

a certain amount of pages so that it doesn't mess up the

template or the website. So that instead of being like

<<PREV 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 NEXT>>

 

I want it to be like

<<PREV 1 2 3 4 5 ... 14 16 17 18 19 20 NEXT>>

 

or something like that, just so that it shortens the links.

Can someone guide me on how I can do this?

 

Heres the script:

<?php

    include('mysql.php'); // We are including the file we made with your MySQL info
    
    $conn = mysql_connect($host, $user, $pass); // Connect to MySQL
    mysql_select_db($db, $conn)or die(mysql_error()); // Select Database
    
    // We are going to establish a few variables so that we can start
    $limit = 14; // This will give us 14 results per page, change this if you want
    
    if(empty($_GET['page'])){
        $page = 1;
        $s_limit = 0;
    } else{
        $page = $_GET['page'];
        $s_limit = ($page - 1) * $limit;
    }
    /*
    $page tells us which page we are on and
    $s_limit tells us which previous results to exclude
    */
    
    $query = mysql_query("SELECT * FROM mp3 ORDER BY artist LIMIT $s_limit, $limit");
    // We are going to order the results by the name of the artist so they are organized well on the page
    
    $query2 = mysql_query("SELECT * FROM mp3");
    // This query is for $num
    
    $num = mysql_num_rows($query2);
    // This tells us how many rows are in our results
    
    $blow = $num / $limit;
    $blown = explode('.', $blow);
    if(empty($blown[1])){
        $top = $blown[0];
    } else{
        $top = $blown[0] + 1;
    }
    // That tells us how many total pages we are going to have
    
    // Variables for Previous and Next Links
    $prev = $page - 1;
    $next = $page + 1;
    
    /*
    Now we are going to put in the links,
    <<PREV 1 2 3 (etc.) NEXT>>
    */
    if($page == 1){
        echo '«PREV ';
    } else{
        echo '<a href="data.php?page='. $prev .'">«PREV</a> ';
    }
    for($x = 1; $x < $page; $x += 1){
        echo '<a href="data.php?page='. $x .'">'. $x .'</a> ';
    }
    echo $page . ' ';
    for($y = $page + 1; $y <= $top; $y += 1){
        echo '<a href="data.php?page='. $y .'">'. $y .'</a> ';
    }
    if($page == $top){
        echo 'NEXT»';
    } else{
        echo '<a href="data.php?page='. $next .'">NEXT»</a>';
    }
    
    // Create Some Seperation from our results and the data
    echo "\n<br>\n<br>\n";
    
    // Get the data from our databse and echo it
    while($row = mysql_fetch_array($query)){
        echo '<a href="http://www.yourwebsite.com/directory/'. $row['file'] .'.mp3">'. $row['name'] .' by '. $row['artist'] .'</a><br>'."\n";
    }
    // Be Sure To Edit The Link!
    
    /*
    Re- Create the links
    <<PREV 1 2 3 (etc) NEXT>>
    */
    if($page == 1){
        echo '«PREV ';
    } else{
        echo '<a href="data.php?page='. $prev .'">«PREV</a> ';
    }
    for($x = 1; $x < $page; $x += 1){
        echo '<a href="data.php?page='. $x .'">'. $x .'</a> ';
    }
    echo $page . ' ';
    for($y = $page + 1; $y <= $top; $y += 1){
        echo '<a href="data.php?page='. $y .'">'. $y .'</a> ';
    }
    if($page == $top){
        echo 'NEXT»';
    } else{
        echo '<a href="data.php?page='. $next .'">NEXT»</a>';
    }
    
    // Close Connection
    mysql_close($conn);
    
?> 

 

the SQL and more info is located here:

http://forums.tizag.com/showthread.php?t=5670

 

thanks for any replies I get

Link to comment
Share on other sites

Then just change your loop.

 

<?php
$skip = -1;
if ($page > 10) $skip = 5;
if ($skip = -1){
for ($blah=0; $blah<$page; $blah++){
   // display links //
}}
else {
for ($blah=0; $blah<=$skip; $blah++){
  // display links //
}
echo " ... ";
for ($blah=$page; $blah>=$page-$skip; $blah--){
  // display links //
}}
?>

Link to comment
Share on other sites

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.