Jump to content

Pagination next and back link confused logic


mkultron

Recommended Posts

Hi guys, I'm getting a bit tongue-tied trying to implement some next and previous link logic for my pagination (see lines under /* next and prev links */ comment). I've tried several alternatives but I can't seem to simply go forwards by one page through the pagination or back one. If you could give me some pointers I'd be really grateful. Thanks. MK

 

		$eventdate = mktime(0,0,0,date("m"),date("d")+1,date("Y"));

	// to get total pages / last page
        $total_results = mysql_num_rows($result);
        $total_pages = ceil($total_results / $per_page);

        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
        if (isset($_GET['page']) && is_numeric($_GET['page']))
        {
                $show_page = $_GET['page'];
                
                // make sure the $show_page value is valid
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                        $start = ($show_page -1) * $per_page;
                        $end = $start + $per_page; 
                }
                else
                {
                        // error - show first set of results
                        $start = 0;
                        $end = $per_page; 
                }               
        }
        else
        {
                // if page isn't set, show first set of results
                $start = 0;
                $end = $per_page; 
        }

	$mysqldate = date( 'd-m-Y H:i:s', $eventdate );
	$eventdate = strtotime( $mysqldate );

        // loop through results of database query, displaying them in the table 
        for ($i = $start; $i < $end; $i++)
        { 

                // make sure that PHP doesn't try to show results that don't exist
                if ($i == $total_results) { break; }
        
                // echo out the contents of each row into a table
                echo "<br />";
                /*echo mysql_result($result, $i, 'eventid');*/
                echo mysql_result($result, $i, 'eventvenue');
			/*echo " ";
                echo mysql_result($result, $i, 'eventdate');*/
			echo ", ";
                echo mysql_result($result, $i, 'eventdate');
			//echo date("Y/m/d");				

			echo "<br />";
        }

        echo "</div><p class='pagination'>";
	echo "<a href='events3.php?page=$i=1'>« first</a> ";
        for ($i = 1; $i <= $total_pages; $i++)
        {
                echo "<a href='events3.php?page=$i'>$i</a> ";

        }
	echo "<a href='events3.php?page=$total_pages'>last »</a></p>";


/* next and prev links */

			$k = $page-1;


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

			if ($i < $total_pages)
			{
				echo "NEXT";
			} 

Link to comment
Share on other sites

Why are you using $i in that logic? Before the code for creating the ext/Prev links $i was used to create individual page links and it *should* be equal to $total_pages. So, you should use $total_pages. There is a reason we give variables meaningful names.

 

OK, typically the Prev/Next links come before and after the page links so you'll want to implement is accordingly. Plus, the first and last links are usually disabled if you are on the first or last page already. Also, the current page is not displayed as a link. Lastly, you will typically want to implement some restrictions on the number of pages that would be displayed in case there are many.

 

By the way, your code is referencing $page, but you don't have such a variable defined. I think you meant to use $show_page

 

Give this a whirl. Not tested, so there may be some typos

    //Create First and Previous links
    if($show_page>1)
    {
        $prev_page = $show_page-1;
        $prev = "<a href='events3.php?page={$prev_page}'>« Previous</a> ";
        $first = "<a href='events3.php?page=1'>«« first</a> ";
    }
    else
    {
        //On first page, create as non-links
        $prev  = "« Previous";
        $first = "« first";
    }

    //Create last and Next links
    if($show_page<$total_pages)
    {
        $next_page = $show_page+1;
        $next = "<a href='events3.php?page={$prev_page}'>Next »</a> ";
        $last = "<a href='events3.php?page={$total_pages}'>last »»</a> ";
    }
    else
    {
        //On last page, create as non-links
        $next  = "Next »";
        $last = "last »»";
    }
    
    //Create individual page links
    $page_range = 5; //Max # of individual pages to display before & after current page
    $pages_start = max($show_page-$page_range, 1);
    $pages_end   = min($show_page+$page_range, $total_pages);
    $start_ellipse = ($pages_start>1) ? '...' : '';
    $end_ellipse   = ($pages_end>$total_pages) ? '...' : '';
    
    $pages = '';
    for ($pg = $pages_start; $pg <= $pages_end; $pg++)
    {
        if($pg != $show_page)
        {
            $pages .= "<a href='events3.php?page={$pg}'>{$pg}</a> ";
        }
        else
        {
            $pages .= "{$pg} ";
        }
    }
    
    
    echo "</div><p class='pagination'>";
    echo $first . $prev . $start_ellipse . $pages . $end_ellipse . $next . $last;
    echo "</p>";

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.