Hi all i am trying to attempt to create my own pagination system i am trying to figuring out how to do a pagination system for my gallery

this is my code

PHP Code:
<form method="post" action="">
<input name="cid" type="hidden" value="<? echo $cid?>">
</form>
<?php
        $cid
=($_GET['cid']);
        
$imagethumbnail=$_POST['imagethumbnail'];
        if(
$_GET['cid'])
            {
            
$cid=($_GET['cid']);
            
$imagethumbnail=$_POST['imagethumbnail'];
            
$cid=$_GET['cid'];
            
//$pid=$_GET['pid'];
            //echo $pid;
            
include("dbconnect.php");
            
$gQuery="SELECT * FROM gallery WHERE cid='$cid'";
            
//excute the query
            //echo $gQuery;
            
$rs=$con->query($gQuery);
            if(!
$rs)
            {
                
$dbError="Error:Failed to executing query".mysqli_error($con);
            }
            else
            {
                
$count=$rs->num_rows;
                if(
$count>0)
                {
                    
print_r($data);
                    
$data=$rs->fetch_assoc();
                    
$imagethumbnail=$data['imagethumbnail'];
                }
                else
                {
                    
print_r($data);
                    echo 
"<br/>Project Cannot be Found";
                }
            }
        } 
                    include(
"dbconnect.php");
                    
//$uQuery="SELECT * FROM gallery WHERE cid='$cid'";
                    
$limit=3;
                    
$page=$_GET['page'];
                    
$totalrows=mysqli_num_rows(mysqli_query("select * from gallery");
                    if(empty(
$page))
                    {
                        
$page='1';
                    }
                    
$start=($page-1)*$limit;
                    
$start=round($start,0);
                    
$rs=mysqli_query("select * from gallery LIMIT $start,$limit");
                    while(
$r=mysqli_fetch_aray($result))
                    {    
                        if(!
$rs
                    { 
                        echo 
"Error:".mysqli_error($con); 
                    } 
                    else 
                    { 
                            
$display 5;
                            
$pg = (isset($_REQUEST['pg']) &&
                            
ctype_digit($_REQUEST['pg'])) ?
                            
$_REQUEST['pg'] : 1;
                            
$start $display $pg $display;
                            
$count=$rs->num_rows
                            if(
$count>0
                            { 
                                 while(
$data=$rs->fetch_assoc()) 
                                {
                                echo 
"<tr><td><a href=gallery/".$data['image']."><img src=gallery/".$data['imagethumbnail']." width=\"100px\" height=\"100px\"></a></td></tr>";
                                }
                            }
                            
$totalpage=$totalrows/$limit;
                            
$totalpages=ceil($totalpages);
                            if(
$page==1)
                            {
                                
$actualpage='[1]';
                            }
                            else
                            {
                                
$actualpage="[$page]";
                            }
                            if(
$page<$totalpages)
                            {
                                
$nv=$page+1;
                                
$pv=$page-1;
                                
$nextpage="<a href=?page=$nv>></a>";
                                
$prevpage="<a href=?page=$nv><</a>";
                                
$firstpage="<a href=?page=$nv></a>";
                                
$finalpage="<a href=?page=$totalpage\"></a>";    
                            } 
                            if(
$page=='1')
                            {
                                
$nv=$page+1;
                                
$nextpage="<a href=?page=$nv>></a>";
                                
$prevpage="<";
                                
$firstpage="";
                                
$finalpage="<a href=?page=$totalpage\"></a>";
                            }
                            elseif(
$page==$totalpages)
                            {
                                
$pv=$page-1;
                                
$nextpage=">";
                                
$prevpage "<a href=?page=$pv><</a>";
                                
$firstpage "<a href=\"?page=1\"></a>";
                                
$finalpage "";
                            }
                            if(
$totalpage=='1' || $totalpage=='0')
                            {
                                
$nextpage ">";
                                
$prevpage "<";
                                
$firstpage "";
                                
$finalpage "";
                            }
                        }

echo 
"$firstpage $prevpage $actualpage $nextpage $finalpage<br>Pages: $totalpages";//echoes the pages at the botton of the file                    }
?>
Doesnt seem to work how i want it to do i want it to have a pagination system of what is in the cid so cid is the category number so doesnt matter how many images there are in the gallery system i want it so its 3 pictures per page

this is my code before i made the changes above

PHP Code:
<form method="post" action="">
<input name="cid" type="hidden" value="<? echo $cid?>">
</form>
<?php
        $cid
=($_GET['cid']);
        
$imagethumbnail=$_POST['imagethumbnail'];
        if(
$_GET['cid'])
            {
            
$cid=($_GET['cid']);
            
$imagethumbnail=$_POST['imagethumbnail'];
            
$cid=$_GET['cid'];
            
//$pid=$_GET['pid'];
            //echo $pid;
            
include("dbconnect.php");
            
$gQuery="SELECT * FROM gallery WHERE cid='$cid'";
            
//excute the query
            //echo $gQuery;
            
$rs=$con->query($gQuery);
            if(!
$rs)
            {
                
$dbError="Error:Failed to executing query".mysqli_error($con);
            }
            else
            {
                
$count=$rs->num_rows;
                if(
$count>0)
                {
                    
print_r($data);
                    
$data=$rs->fetch_assoc();
                    
$imagethumbnail=$data['imagethumbnail'];
                }
                else
                {
                    
print_r($data);
                    echo 
"<br/>Project Cannot be Found";
                }
            }
        } 
                    include(
"dbconnect.php");
                    
//$uQuery="SELECT * FROM gallery WHERE cid='$cid'";
                    
$rQuery="SELECT * FROM `gallery` LIMIT 0 , 3";
                    
$rs=mysqli_query($con,$rQuery);
                    
//$rs=mysqli_query($con,$uQuery); 
                    
if(!$rs
                    { 
                        echo 
"Error:".mysqli_error($con); 
                    } 
                    else 
                    { 
                        
$display 5;
                        
$pg = (isset($_REQUEST['pg']) && ctype_digit($_REQUEST['pg'])) ?
                          
$_REQUEST['pg'] : 1;
                        
$start $display $pg $display;
                        
$count=$rs->num_rows
                        if(
$count>0
                        { 
                            while(
$data=$rs->fetch_assoc()) 
                            {
                                echo 
"<tr><td>\"<img src=gallery/".$data['imagethumbnail']." width=\"100px\" height=\"100px\"></td></tr>";
                            }
                        } 

                    }
?>
What am i doing wrong?