Page 1 of 1

First Function

Posted: Sun Mar 23, 2008 1:33 pm
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";
        }
    }
 

Re: First Function

Posted: Sun Mar 23, 2008 4:01 pm
by www.WeAnswer.IT
Looks good to me...

Re: First Function

Posted: Sun Mar 23, 2008 7:29 pm
by devbro
what about the current page?

Re: First Function

Posted: Sun Mar 23, 2008 8:51 pm
by psurrena
current page?

Re: First Function

Posted: Sun Mar 23, 2008 8:55 pm
by Christopher
What is this code supposed to do? SELECT COUNT(42) FROM mytable? COUNT takes a column name or *.

Re: First Function

Posted: Sun Mar 23, 2008 8:58 pm
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.