How can I add pagination in my php codes?

Viewed 28

Currently in our website, I add pagination to show the results 1-50 for each. But the problem is If I click to next or page2, the whole page got refresh and all the results got disappear. But in URL link it shows index.php?page=2 but the whole page got refresh and all the data disappear. Is there anyway to fix it? Edit: I think I need to make onClick and run the resultDiv. Not sure.

echo "<div id='resultDiv'><div id='inResult'></div></div>";
    <?php
include("../common/_config7.php");
include('variables.php');
$site = $_GET['site'];
    if($site == 7){ $site_name = 'AMK-6E'; } else if($site == 9){ $site_name = 'TPY'; } else { $site_name = 'AMK-J9'; }

$reportArray = array ('op4 wip', 'in cabinet (cabinet/box)', 'loan (emome)', 'ship-out (invoice no.)');

// echo 'search string: '.$srch.'<br>report type: '.$reportArray[$rtyp].'<br>sdte: '.$sdte.'<br>edte: '.$edte.'<br>site: '.$site.'<br>site name: '.$site_name.'<br>';

$ctr = 0;
$colCount = count($hdrArray);
$rowCount = $colCount-1;
$field = $varArray[$ct];
$csv = ''; 

$tableName="tbl_scrap_wafer";
$tPage="index.php?";


echo "<html>";
echo "<body bgcolor=\"white\" onClick=\"document.pac_request.name.focus();\>";
echo "<form name=\"pac_request\" method=\"post\">\n";
echo "<div class=\"pg_display\"><br/>";

  echo "<table align=\"center\" border=\"0\" width=\"95%\" bordercolor=\"#DADADA\" cellpadding=\"3\" cellspacing=\"1\">\n";

$sql1 = "SELECT * FROM lot_track_smd.tbl_scrap_wafer WHERE `site` LIKE '$site' AND `pl` != '82' AND `dt_terminate` = '0000-00-00 00:00:00' AND `status` != '8' ORDER BY `location` ASC";
//echo $sql1.'<br>';
$q_list1 = mysqli_query($conn,$sql1);
$totalCount = mysqli_num_rows($q_list1);

echo  "<tr align=\"left\"><td align=\"left\" colspan=\"6\">\n";
echo  "<font face=\"verdana\" color=\"black\" size=\"0\"><b>\n";
echo  "&nbsp ";
include("pagination.php");
echo  "</b></font>\n";
echo  "</td></tr>\n";

echo "<table id='scrp'>";
    echo "<tr>";
        echo "<td colSpan='$colCount' style='text-align: right; padding-right:10px;' title='export to excel'>";
            echo "<img id='expClicker' src='".$src."excel.png' onClick='exportMe()'>";
        echo "</td>";
    echo "</tr>";
    echo "<tr>";
    for($x=0; $x<=$rowCount; $x++){
        if($x == 0){ $th = 0; } else if($x == $rowCount){ $th = 2; } else { $th = 1; }
        echo "<th class='th_scrp$th'>";
            echo $hdrArray[$x];
            $csv .= $hdrArray[$x].",";
        echo "</th>";
    }
    echo "</tr>";
    $csv .= "\n";
    
    mysqli_select_db($conn, 'lot_track_smd') or die ('cannot connect to db: '.mysqli_error($conn));
    $sql = "SELECT * FROM lot_track_smd.tbl_scrap_wafer WHERE `site` LIKE '$site' AND `pl` != '82' AND `dt_terminate` = '0000-00-00 00:00:00' AND `status` != '8' ORDER BY `location` ASC  LIMIT $start, $limit";
    echo $sql.'<br>';
    //IN ('02','10','19','59','74')
    $result = mysqli_query($conn, $sql) or die ('query error: '.mysqli_error($conn).$sql);
    while ($query = mysqli_fetch_assoc($result)){
        if($ctr % 2 == 0){ $tr = 0; } else { $tr = 1; }
        echo "<tr class='tr_scrp$tr'>";
            for($x=0; $x<=$rowCount; $x++){
                if($x == 0){ $td = 0; } else if($x == $rowCount){ $td = 2; } else { $td = 1;}
                echo "<td class='td_scrpx$td'>";
                    if($varArray[$x] == 'ocr' && $query['location'] != ''){
                        $val = $query[$varArray[$x]];
                        echo "<a value='$val' onClick='checkTDate(this.value)'>".$val."</a>";
                        $csv .= $val.",";
                    } elseif($varArray[$x] == 'site'){
                        echo $sitArray[$query[$varArray[$x]]];
                        $csv .= $sitArray[$query[$varArray[$x]]].",";
                    } elseif($varArray[$x] == 'status'){
                        echo $selArray[$query['status']];
                        $csv .= $selArray[$query['status']].",";
                    } else {
                        echo $query[$varArray[$x]];
                        $csv .= $query[$varArray[$x]].",";
                    }
                echo "</td>";
            }
        echo "</tr>";
        $csv .= "\n";
        $ctr++;
    }

    echo "<tr>";
        echo "<td colSpan='$colCount' class='tf_scrp'>";
            echo $ctr.' record/s found';
            $csv .= $ctr.' record/s found';
        echo "</td>";
    echo "</tr>";
    $csv .= "\n";
echo "</table>";


////////////////////////////////////////////////// for excel export ///////////////////////////////////////////////////
$filename= "scrap_".date("ymdHis").".csv";
$csvFile = $globalTempDir.$filename;    
    
if(file_exists($csvFile) == true){
    unlink($csvFile);
}
         
$fp = @fopen($csvFile,"w");
fwrite($fp, $csv);
fclose($fp);
    
echo "<a id='export' href='".$globalTempUrl.$filename."'></a>";
mysqli_close($conn);
echo "<br><br>";
mysqli_close($conn);

echo  "<tr bgcolor=\"#F5F5F5\"></tr>\n";
echo  "</table>\n";

echo  "<br>\n";
echo "</div>";
echo  "</form>\n";
echo "</body>";
echo "</html>";
?>

Below codes are from pagination.php file.

    <?php
    /*
        Place code to connect to your DB here.
    */
    include('_dbconn.php'); // connect to DB.
  
    $tbl_name=$tableName;       //your table name
    // How many adjacent pages should be shown on each side?
    $adjacents = 5;
    
    /* 
       First get total number of rows in data table. 
       If you have a WHERE clause in your query, make sure you mirror it here.
    */
  
    //$query = $sql_query;
    //$total_pages = mysql_fetch_array(mysql_query($query));
    //$total_pages = $total_pages[num];
    
  $total_pages=$totalCount;
    /* Setup vars for query. */
    $targetpage = $tPage. 'page';   //your file name  (the name of this file)
    $limit = 50;                                //how many items to show per page
    $page = $_GET['page'];
    if($page) 
        $start = ($page - 1) * $limit;          //first item to display on this page
    else
        $start = 0;                             //if no page var is given, set start to 0
    
    /* Get data. 
    $sql1 = $sql . " LIMIT $start, $limit";
    $result = mysql_query($sql1);*/
    
    /* Setup page vars for display. */
    if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
    $prev = $page - 1;                          //previous page is page - 1
    $next = $page + 1;                          //next page is page + 1
    $lastpage = ceil($total_pages/$limit);      //lastpage is = total pages / items per page, rounded up.
    $lpm1 = $lastpage - 1;                  //last page minus 1
    
  $start_record=$start + 1 ;
  $record=$limit * $page; 
  
    /* 
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
    */
    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "<div class=\"pagination\">";
    if($record > $total_pages)
    {
       $pagination .= "<span class=\"current\">Records " .$start_record. " to " .$total_pages. " of " .$total_pages. "</span>";
    }
    else
    {
        $pagination .= "<span class=\"current\">Records " .$start_record. " to " .$record. " of " .$total_pages. "</span>";
     }
        //previous button
        if ($page > 1) 
            $pagination.= "<a href=\"$targetpage=$prev\"><< previous</a>";
        else
            $pagination.= "<span class=\"disabled\"><< previous</span>";    
        
        //pages 
        if ($lastpage < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage=$counter\" >$counter</a>";                 
            }
        }
        elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
        {
            //close to beginning; only hide later pages
            if($page < 1 + ($adjacents * 2))        
            {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage=$counter\" >$counter</a>";                 
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$targetpage=$lpm1\" >$lpm1</a>";
                $pagination.= "<a href=\"$targetpage=$lastpage\" >$lastpage</a>";       
            }
            //in middle; hide some front and some back
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {
                $pagination.= "<a href=\"$targetpage=1\" >1</a>";
                $pagination.= "<a href=\"$targetpage=2\" >2</a>";
                $pagination.= "...";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage=$counter\" >$counter</a>";                 
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$targetpage=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$targetpage=$lastpage\">$lastpage</a>";        
            }
            //close to end; only hide early pages
            else
            {
                $pagination.= "<a href=\"$targetpage=1\" >1</a>";
                $pagination.= "<a href=\"$targetpage=2\" >2</a>";
                $pagination.= "...";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage=$counter\" >$counter</a>";                 
                }
            }
        }
        
        //next button
        if ($page < $counter - 1) 
            $pagination.= "<a href=\"$targetpage=$next\">next >></a>";
        else
            $pagination.= "<span class=\"disabled\">next >></span>";
        $pagination.= "</div>\n";       
    }
?>
<?=$pagination?>
0 Answers
Related