在此页面中没有jquery的分页工作

时间:2017-08-09 09:46:08

标签: php mysql ajax pdo

我有一个名为records.php的页面,它基本上适用于ajax分页,但是我希望它能够在没有ajax或任何js的情况下工作,有人可以帮助我,我是新手,它真的很棒

我不知道从哪里删除这些代码中的javascript代码,以便我可以在这个php脚本中进行分页,真的希望这里的一些专家可以帮助我

records.php

<?php
    //DB configuration Constants
    define('_HOST_NAME_', 'localhost');
    define('_USER_NAME_', 'root');
    define('_DB_PASSWORD', '');
    define('_DATABASE_NAME_', 'data');

    //PDO Database Connection
    try {
        $databaseConnection = new PDO('mysql:host='._HOST_NAME_.';dbname='._DATABASE_NAME_, _USER_NAME_, _DB_PASSWORD);
        $databaseConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }

    $sqlQuery   = $databaseConnection->query("SELECT * FROM tbl_pagination");
    $count      = $sqlQuery->rowCount();

    $adjacents = 2;
    $records_per_page = 5;

    $page = (int) (isset($_POST['page_id']) ? $_POST['page_id'] : 1);
    $page = ($page == 0 ? 1 : $page);
    $start = ($page-1) * $records_per_page;

    $next = $page + 1;    
    $prev = $page - 1;
    $last_page = ceil($count/$records_per_page);
    $second_last = $last_page - 1; 


    $pagination = "";
    if($last_page > 1){
        $pagination .= "<div class='pagination'>";
        if($page > 1)
            $pagination.= "<a href='javascript:void(0);' onClick='change_page(1);'>&laquo; First</a>";
        else
            $pagination.= "<span class='disabled'>&laquo; First</span>";

        if ($page > 1)
            $pagination.= "<a href='javascript:void(0);' onClick='change_page(".($prev).");'>&laquo; Previous&nbsp;&nbsp;</a>";
        else
            $pagination.= "<span class='disabled'>&laquo; Previous&nbsp;&nbsp;</span>";   

        if ($last_page < 7 + ($adjacents * 2))
        {   
            for ($counter = 1; $counter <= $last_page; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class='current'>$counter</span>";
                else
                    $pagination.= "<a href='javascript:void(0);' onClick='change_page(".($counter).");'>$counter</a>";     

            }
        }
        elseif($last_page > 5 + ($adjacents * 2))
        {
            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='javascript:void(0);' onClick='change_page(".($counter).");'>$counter</a>";     
                }
                $pagination.= "...";
                $pagination.= "<a href='javascript:void(0);' onClick='change_page(".($second_last).");'> $second_last</a>";
                $pagination.= "<a href='javascript:void(0);' onClick='change_page(".($last_page).");'>$last_page</a>";   

           }
           elseif($last_page - ($adjacents * 2) > $page && $page > ($adjacents * 2))
           {
               $pagination.= "<a href='javascript:void(0);' onClick='change_page(1);'>1</a>";
               $pagination.= "<a href='javascript:void(0);' onClick='change_page(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='javascript:void(0);' onClick='change_page(".($counter).");'>$counter</a>";     
               }
               $pagination.= "..";
               $pagination.= "<a href='javascript:void(0);' onClick='change_page(".($second_last).");'>$second_last</a>";
               $pagination.= "<a href='javascript:void(0);' onClick='change_page(".($last_page).");'>$last_page</a>";   
           }
           else
           {
               $pagination.= "<a href='javascript:void(0);' onClick='change_page(1);'>1</a>";
               $pagination.= "<a href='javascript:void(0);' onClick='change_page(2);'>2</a>";
               $pagination.= "..";
               for($counter = $last_page - (2 + ($adjacents * 2)); $counter <= $last_page; $counter++)
               {
                   if($counter == $page)
                        $pagination.= "<span class='current'>$counter</span>";
                   else
                        $pagination.= "<a href='javascript:void(0);' onClick='change_page(".($counter).");'>$counter</a>";     
               }
           }
        }
        if($page < $counter - 1)
            $pagination.= "<a href='javascript:void(0);' onClick='change_page(".($next).");'>Next &raquo;</a>";
        else
            $pagination.= "<span class='disabled'>Next &raquo;</span>";

        if($page < $last_page)
            $pagination.= "<a href='javascript:void(0);' onClick='change_page(".($last_page).");'>Last &raquo;</a>";
        else
            $pagination.= "<span class='disabled'>Last &raquo;</span>";

        $pagination.= "</div>";       
    }


$records    = $databaseConnection->query("SELECT * FROM tbl_pagination LIMIT $start, $records_per_page");
$count      = $records->rowCount();
$HTML='';
if($count > 0)
{
    foreach($records as $row) {
        $HTML.='<div>';
        $HTML.= $row['first_name'];
        $HTML.='</div><br/>';
    }
}
else
{
    $HTML='No Data Found';
}
echo $HTML;
echo $pagination;
?>

1 个答案:

答案 0 :(得分:0)

您需要将$ _POST [&#39; page_id&#39;]更改为$ _GET [&#39; page_id&#39;],以便可以通过查询字符串发送页码。然后,您需要从所有页面链接中删除onClick事件。对于HREF,您必须添加页面的URL,其中page_id作为查询var,并且必需页面作为值。像这样:

...
...
$page = (int) (isset($_GET['page_id']) ? $_GET['page_id'] : 1);
$page = ($page == 0 ? 1 : $page);
$start = ($page-1) * $records_per_page;

$next = $page + 1;    
$prev = $page - 1;
$last_page = ceil($count/$records_per_page);
$second_last = $last_page - 1; 


$pagination = "";
if($last_page > 1){
    $pagination .= "<div class='pagination'>";
    if($page > 1)
        $pagination.= "<a href='URL_OF_PAGE.php?page=1'  >&laquo; First</a>";
    else
        $pagination.= "<span class='disabled'>&laquo; First</span>";

    if ($page > 1)
        $pagination.= "<a href='URL_OF_PAGE.php?page=<?php echo $prev; ?>'  >&laquo; Previous&nbsp;&nbsp;</a>";
    else
        $pagination.= "<span class='disabled'>&laquo; Previous&nbsp;&nbsp;</span>";   

    if ($last_page < 7 + ($adjacents * 2))
    {   
        for ($counter = 1; $counter <= $last_page; $counter++)
        {
            if ($counter == $page)
                $pagination.= "<span class='current'>$counter</span>";
            else
                $pagination.= "<a href='URL_OF_PAGE.php?page=<?php echo $counter; ?>'>$counter</a>";     

        }
    }

...
...