Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
  1. #1
    Senior Member tekp's Avatar
    Join Date
    Jan 2004
    Location
    A small village near a small city near a small cit
    Posts
    918
    Member #
    4667
    Liked
    1 times
    Journal Entry

    On the journal entry above there's two links at the bottom, supposedly going to the next and previous entries. However, the next link goes to the current entry, and the previous link goes 2 entries back - as if we're actually on the entry prior to the current one, if you follow. If you keep clicking the 'previous entry' link, you'll see that all the entries are finely linked to at the bottom in the right order, even when it gets to the very last entry (in June if you want to cheat).

    Why is this?

    Here's the code:

    PHP:
    PHP Code:
       $result2 mysql_query("SELECT id, date FROM journal WHERE visibility = 'visible' ORDER BY date DESC") or die("MySQL Error");
       
    $rows mysql_affected_rows();
       
    $num 0;
       
    $entry_dates = array();
       while(
    $rows $num) {
        
    $row2 mysql_fetch_row($result2);
        
    $entries[] = array($row2[0],$row2[1]);
        
    // print $row2[0] . "<br />";
        
    $num++;
       }
       
    // print "<b>" . $row['id'] . "</b><br /><br />";
       
    $current each($entries);
       
    $current $current[1];
       
    $num 0;
       while(
    $current[1] > $row['date']) {
        
    $num++;
        
    // print "(" . ($num) . ")";
        // print $current[1];
        
    $current next($entries);
       }
       
    $next prev($entries);
       
    $prev next($entries);
       
    $prev next($entries);
       
    $result2 mysql_query("SELECT title, directory, date FROM journal WHERE id = '{$prev[0]}'") or die("MySQL Error");
       
    $row2 mysql_fetch_row($result2);
       
    $prevurl "/journal/" date("Y/m/d",strtotime($row2[2])) . "/" $row2[1];
       
    $prevtitle $row2[0];
       
    $result2 mysql_query("SELECT title, directory, date FROM journal WHERE id = '{$next[0]}'") or die("MySQL Error");
       
    $row2 mysql_fetch_row($result2);
       
    $nexturl "/journal/" date("Y/m/d",strtotime($row2[2])) . "/" $row2[1];
       
    $nexttitle $row2[0];
       print 
    " <br clear=\"all\" />";
       print 
    " <hr />";
       if(
    $prevurl != "" && $prevtitle != "") {
        print 
    " <div style=\"width:40%; float:left;\">";
        print 
    "  <p>";
        print 
    "   <a href=\"$prevurl\">&laquo; Previous Entry</a>";
        print 
    "   <br />";
        print 
    "   '$prevtitle'";
        print 
    "  </p>";
        print 
    " </div>";
       }
       if(
    $nexturl != "" && $nexttitle != "") {
        print 
    " <div style=\"width:40%; float:right; text-align:right;\">";
        print 
    "  <p>";
        print 
    "   <a href=\"$nexturl\">Next Entry &raquo;</a>";
        print 
    "   <br />";
        print 
    "   '$nexttitle'";
        print 
    "  </p>";
        print 
    " </div>";
       } 
    Thanks a lot
    tekp :cheeky: tekponline.com

  2.  

  3. #2
    Senior Member raspberryh's Avatar
    Join Date
    Aug 2004
    Location
    Erie, PA
    Posts
    882
    Member #
    7208
    Liked
    1 times
    what are the prev and next functions? do you have those defined somewhere?
    choosy developers choose gif!
    website | paintings | blog

  4. #3
    Junior Member
    Join Date
    Sep 2005
    Posts
    2
    Member #
    11453
    Use this page_navigation function for page browsing.
    $base_url - page base url;
    $num_items - total count of items to display
    $per_page - page size
    $on_page - current page number
    $add_prevnext_text - add previous/next text

    PHP Code:
    function page_navigation($base_url$num_items$per_page$on_page$add_prevnext_text TRUE)
    {
        
    $lang = array();
        
    $lang['Previous'] = 'Previous';
        
    $lang['Next'] = 'Next';
        
    $lang['Goto_page'] = 'Page';

        
    $total_pages ceil($num_items/$per_page);

        if ((-
    == $per_page) || ($total_pages == 1) )
        {
            return 
    '';
        }

        
    //$on_page = floor($start_item / $per_page) + 1;

        
    $page_string '';
        if ( 
    $total_pages 10 )
        {
            
    $init_page_max = ( $total_pages ) ? $total_pages;

            for(
    $i 1$i $init_page_max 1$i++)
            {
                
    $page_string .= ( $i == $on_page ) ? '<b>' $i '</b>' '<a class=pagenav href="' this_site_append_sid($base_url "&amp;pagesize=" $per_page"&amp;page=" $i ) . '">' $i '</a>';
                if ( 
    $i <  $init_page_max )
                {
                    
    $page_string .= ", ";
                }
            }

            if ( 
    $total_pages )
            {
                if ( 
    $on_page 1  && $on_page $total_pages )
                {
                    
    $page_string .= ( $on_page ) ? ' ... ' ', ';

                    
    $init_page_min = ( $on_page ) ? $on_page 5;
                    
    $init_page_max = ( $on_page $total_pages ) ? $on_page $total_pages 4;

                    for(
    $i $init_page_min 1$i $init_page_max 2$i++)
                    {
                        
    $page_string .= ($i == $on_page) ? '<b>' $i '</b>' '<a class=pagenav href="' this_site_append_sid($base_url "&amp;pagesize=" $per_page"&amp;page=" $i ) . '">' $i '</a>';
                        if ( 
    $i <  $init_page_max )
                        {
                            
    $page_string .= ', ';
                        }
                    }

                    
    $page_string .= ( $on_page $total_pages ) ? ' ... ' ', ';
                }
                else
                {
                    
    $page_string .= ' ... ';
                }

                for(
    $i $total_pages 2$i $total_pages 1$i++)
                {
                    
    $page_string .= ( $i == $on_page ) ? '<b>' $i '</b>'  '<a class=pagenav href="' this_site_append_sid($base_url "&amp;pagesize=" $per_page"&amp;page=" $i ) . '">' $i '</a>';
                    if( 
    $i <  $total_pages )
                    {
                        
    $page_string .= ", ";
                    }
                }
            }
        }
        else
        {
            for(
    $i 1$i $total_pages 1$i++)
            {
                
    $page_string .= ( $i == $on_page ) ? '<b>' $i '</b>' '<a class=pagenav href="' this_site_append_sid($base_url "&amp;pagesize=" $per_page"&amp;page=" $i ) . '">' $i '</a>';
                if ( 
    $i <  $total_pages )
                {
                    
    $page_string .= ', ';
                }
            }
        }

        if ( 
    $add_prevnext_text )
        {
            if ( 
    $on_page )
            {
                
    $page $on_page-1;
                
    $page_string ' <a class=pagenav href="' this_site_append_sid($base_url "&amp;pagesize=" $per_page"&amp;page=" $page) . '">' $lang['Previous'] . '</a>&nbsp;&nbsp;' $page_string;
            }

            if ( 
    $on_page $total_pages )
            {
                
    $page $on_page+1;
                
    $page_string .= '&nbsp;&nbsp;<a class=pagenav href="' this_site_append_sid($base_url "&amp;pagesize=" $per_page"&amp;page=" $page) . '">' $lang['Next'] . '</a>';
            }

        }

        
    $page_string '<br>' $lang['Goto_page'] . ' ' $page_string;

        return 
    $page_string;
    }

    function 
    this_site_append_sid($url)
    {
        return 
    $url;


  5. #4
    Senior Member raspberryh's Avatar
    Join Date
    Aug 2004
    Location
    Erie, PA
    Posts
    882
    Member #
    7208
    Liked
    1 times
    Nice! I might keep it for myself
    choosy developers choose gif!
    website | paintings | blog

  6. #5
    WDF Staff Wired's Avatar
    Join Date
    Apr 2003
    Posts
    7,657
    Member #
    1234
    Liked
    137 times
    FYI, it's called pagination (making dynamic back and next links).
    The Rules
    Was another WDF member's post helpful? Click the like button below the post.

    Admin at houseofhelp.com

  7. #6
    Senior Member visualAd's Avatar
    Join Date
    Jan 2003
    Location
    Slough, UK
    Posts
    201
    Member #
    434
    A member (The Hobo) at VbForums was kind enough to post a function which he uses for pagination on his site. I have set up an example of how it works here:

    http://adam.codedv.com/examples/pagination.php

    Below is the code for the page. Use it as you please:
    PHP Code:
    <?php 
        
    include 'site_db.php'

        
    $db = new SiteDb
         
        
    /* get the total items to display per page */   
        
    if (!isset($_GET['pp'])) { 
            
    $pp 10
        } else { 
            
    $pp intval($_GET['pp']); 
        } 

        
    /* get the page number to display */ 
        
    if (!isset($_GET['p'])) { 
            
    $p 1
        } else { 
            
    $p intval($_GET['p']); 
        } 

        
    /* find the start record */ 
        
    $start = ($p $pp) - $pp

        
    /* build the query */ 
        
    $query "SELECT SQL_CALC_FOUND_ROWS txt FROM items LIMIT $start,$pp"

        if (! 
    $result $db->query($query)) { 
            echo(
    $db->error()); 
            die(
    'Query error.'); 
        } 

        
    /* find the total number of records */ 
        
    $query 'SELECT FOUND_ROWS()'

        if (! 
    $found $db->query($query)) { 
            echo(
    $db->error()); 
            die(
    'Query error.'); 
        } 
         
        
    $row $db->fetch_row($found); 
        
    $total_rows $row[0]; 
    ?> 
    <html> 
        <head> 
            <title>Pagination Example</title> 
        </head> 
        <body> 
            <table> 
                <?php while ($row $db->fetch_row($result)): // print all the rows ?> 
                <tr> 
                    <td><?php echo($row[0]); ?></td> 
                </tr> 
                <?php endwhile; ?> 
            </table> 
        <p><?php echo(createlinks($total_rows$pp$p$_SERVER['PHP_SELF'] . "?pp=$pp&p=")) // print out the links ?></p> 
        </body> 
    </html> 
    <?php 
        
    function createlinks($totalItems$perPage$currentPage$link) { 
            
    // First, figure out the number of pages there will be total: 
            
    $totalPages floor($totalItems $perPage); 
            
    $totalPages += ($totalItems $perPage != 0) ? 0

            
    // Start building page links: 
            
    if ($totalPages 1) { 
                
    $output 'Pages [' $totalPages '] '
         
            
    $loopStart 1$loopEnd $totalPages
             
            if (
    $totalPages 5) { 
                if (
    $currentPage <= 3) { 
                    
    $loopStart 1
                    
    $loopEnd 5
                } else if (
    $currentPage >= $totalPages 2) { 
                    
    $loopStart $totalPages 4
                    
    $loopEnd $totalPages
                } else { 
                    
    $loopStart $currentPage 2
                    
    $loopEnd $currentPage 2
                } 
            } 

                
    // Add the "first page" link: 
                
    if ($loopStart != 1) { 
                    
    $output .= '<a href="' $link '1"></a> '
                } 

                
    // Use the variables we setup above to loop the links: 
                
    for ($i $loopStart$i <= $loopEnd$i++) { 
                    
    // If this is our current page: 
                    
    if ($i == $currentPage) { 
                        
    // Output bold without a link: 
                        
    $output .= '<b>' $i '</b> '
                    
    // This is not our current page: 
                    
    } else { 
                        
    // Output the link: 
                        
    $output .= '<a href="' $link $i '">' $i '</a> '
                    } 
                } 

                
    // Add the "last page" link: 
                
    if ($loopEnd != $totalPages) { 
                    
    $output .= '<a href="' $link $totalPages .'"></a> '
                } 

                return 
    $output
            } else { 
                return 
    ''
            } 
        }         
    ?>
    You will need to replace the DB functions with the functions which relate to the DBMS you are using.

  8. #7
    Senior Member tekp's Avatar
    Join Date
    Jan 2004
    Location
    A small village near a small city near a small cit
    Posts
    918
    Member #
    4667
    Liked
    1 times
    Thanks, but I was actually looking for a way to make the links at the bottom of each entry (next and previous entry, not next and previous page of entries) work.

    Any ideas?
    tekp :cheeky: tekponline.com


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Search tags for this page

next previous php script

Click on a term to search for related topics.
All times are GMT -6. The time now is 08:46 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com