First Function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

First Function

Post by psurrena »

I just made this function to generate "previous" and "next" links. It works but am I missing anything?

Code: Select all

 
    function pn_nav($table,$pn_var){
        //$table: database table
        //$pn_var: usualy the row id, it needs to be numeric.
        
        if(!is_numeric($_GET["$pn_var"])){
            echo "<p class=\"error\">The id needs to be a number.</p>\n";
        }else{
            $pn_id=$_GET["$pn_var"];
        }
        
        $navQuery="SELECT COUNT($pn_var) FROM $table";
        $navResult=mysql_query($navQuery) or die (mysql_error());
        $row=mysql_fetch_array($navResult);
    
        //Previous
        if($pn_id > 1){
            $prev=$pn_id - 1;
            echo "<a href=\"".$_SERVER['PHP_SELF']."?{$pn_var}={$prev}\">Previous</a> ";
        }
    
        // Next 
        if($pn_id < $row[0]){
            $next=$pn_id + 1;
            echo "<a href=\"".$_SERVER['PHP_SELF']."?{$pn_var}={$next}\">Next</a>\n";
        }
    }
 
www.WeAnswer.IT
Forum Newbie
Posts: 24
Joined: Wed Mar 19, 2008 6:33 pm

Re: First Function

Post by www.WeAnswer.IT »

Looks good to me...
devbro
Forum Newbie
Posts: 7
Joined: Tue Mar 18, 2008 11:46 am

Re: First Function

Post by devbro »

what about the current page?
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: First Function

Post by psurrena »

current page?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: First Function

Post by Christopher »

What is this code supposed to do? SELECT COUNT(42) FROM mytable? COUNT takes a column name or *.
(#10850)
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Re: First Function

Post by psurrena »

Say the $pn_var is the id field. It counts the total so it knows the beginning and end. That way, there is no previous button on the first image and no next button on the last image.
Post Reply