在Javascript的帮助下分页

时间:2013-10-03 12:25:21

标签: javascript php html

社区,我目前在我的页面上有一些分页,以帮助用户浏览已登录的票证。为了解决这个问题,我分别有两个表单元格,每个单元格分别有Next和Previous。现在,当用户点击其中任何一个时,它会向AJAX为字符串的JS计数器添加+1或-1。然后,我有我的分页,要么转到我的mysql数据库中的下一个或前15个项目。我写了我的JS,如果计数器小于1,它将返回到一个。这样他们就不必反复点击下一个按钮来查看门票。我试图想办法从另一端做到这一点,如果结果列表的结尾是空白,或者没有更多结果可用,它将保留在最后一个计数器。 PhP包括初始表设置和字符串变量

include 'Search_Var.php';  //This file is used to build $sequel.
                           //$sequel is a string built by user selected search options
echo '<div style="display: none" id="Search_Var">'.$sequel.'</div>
<div class="container" style="clear: both;">
<fieldset>
<legend class="legend1"><h2> &nbsp Results &nbsp </h2></legend>
<div style="padding-top: 5px;">
<span id="page">';
include 'Search_Tickets.php';  // This file builds a table to show the first 15 results
                               // It also displays the total number of tickets found
                               // in the database matching the search query
echo '</span>';
if($openTicketsCount!=0) {include '********/***********/PreviousNext.inc.php';};
//If no tickets are available the Previous and Next boxes won't appear.
</fieldset>
</div>
</div>
';

的Javascript

var count = 1;
$(window).on('load', function() {
    var id = document.getElementById( "Search_Var" ).innerText;
    $('.page').on('click', function() {
        var page = $(this).html();
        if(page == "Next")
        {
            count = count + 1;
        }
        if(page == "Previous")
        {
            count = count - 1;
        }
        if(count < 1)
        {
            count = 1;
        }
        $.get("/*******/********/Search_Pagination.inc.php?count_id=" + count + "&search_id=" + id, function(data) {
            $('#page').html(data);
        });
    });
});

最后,分页

$start = 0;
$per_page = 15;
$page = $_GET['selection_id'];
$_SESSION['role_id'] = $_GET['role_id'];
$_SESSION['bus_id'] = $_GET['bus_id'];
$_SESSION['dept_id'] = $_GET['dept_id'];
if($page <= 1)
{
    $start = 0;
}
else
{
    $start = $page * $per_page - $per_page;
}
$get_openTickets = $search;
$openTicketsCount = mysql_num_rows(mysql_query($get_openTickets));
$num_rows = mysql_num_rows(mysql_query($get_openTickets));
$num_pages = $num_rows / $per_page;
$get_openTickets .= " LIMIT $start, $per_page";
if($openTicketsCount !=true)
{
    echo '
    <td colspan = "9">No open tickets were found!</td>
    ';
}
else
{
    $result = mysql_query($get_openTickets);
    echo '
    <table class="openTickets" width="1150px" border="0px" padding="0px">
    <tr>
      <th scope="col">ID</th>
      <th scope="col" style="width:75px;">PRIORITY</th>
      <th scope="col">CALLER /<br />DEPARTMENT</th>
      <th scope="col">COMPANY NAME /<br />SALES REP</th>
      <th scope="col" style="width:75px;">CREATED<br />ON</th>
      <th scope="col" style="width:75px;">LAST<br />UPDATED</th>
      <th scope="col" style="width:75px;">SOURCE</th>
      <th scope="col">USER</th>
      <th scope="col">CATEGORY /<br />SUBCATEGORY</th>
    </tr>
';
    while(($openTicket = mysql_fetch_assoc($result)))
    {
        echo'
        <tr>
          <td><a href="?page=ViewTicket&ticket='.$openTicket['call_id'].'">'.$openTicket['call_id'].'</a></td>
          <td style="width:75px;">'.$openTicket['priority_name'].'</td
          <td>'.$openTicket['caller_name'].'<br />'.$openTicket['caller_dept'].'</td>
          <td>'.$openTicket['cust_name'].'<br />'.$openTicket['cust_rep'].'</td>
          <td style="width:75px;">'.$openTicket['created_on'].'</td>
          <td style="width:75px;">'.$openTicket['updated_on'].'</td>
          <td style="width:75px;">'.$openTicket['source_name'].'</td>
          <td>'.$openTicket['created_by'].'</td>
          <td>'.$openTicket['cat_name'].'<br />'.$openTicket['subcat_name'].'</td>
       </tr>';
   }
   echo '
   <tr>
     <th colspan="9" style="text-align:right;">Total Open Ticket(s): '.$openTicketsCount.'&nbsp;&nbsp;</th>
   </tr>
   </table>';
}

2 个答案:

答案 0 :(得分:1)

看看下面的

$start = 0;
$per_page = 15;
$page = $_GET['selection_id'];
$_SESSION['role_id'] = $_GET['role_id'];
$_SESSION['bus_id'] = $_GET['bus_id'];
$_SESSION['dept_id'] = $_GET['dept_id'];
if($page <= 1)
{
    $start = 0;
}
else
{
    $start = $page * $per_page - $per_page;
}
$get_openTickets = $search;
$openTicketsCount = mysql_num_rows(mysql_query($get_openTickets));
$num_rows = mysql_num_rows(mysql_query($get_openTickets));
$num_pages = $num_rows / $per_page;
$get_openTickets .= " LIMIT $start, $per_page";
if($openTicketsCount !=true)
{
    echo '
    <td colspan = "9">No open tickets were found!</td>
    ';
}
else
{
    $result = mysql_query($get_openTickets);
    echo '<input type="hidden" value="'.$num_pages.'" id="pageCount"/>
    <table class="openTickets" width="1150px" border="0px" padding="0px">
    <tr>
      <th scope="col">ID</th>
      <th scope="col" style="width:75px;">PRIORITY</th>
      <th scope="col">CALLER /<br />DEPARTMENT</th>
      <th scope="col">COMPANY NAME /<br />SALES REP</th>
      <th scope="col" style="width:75px;">CREATED<br />ON</th>
      <th scope="col" style="width:75px;">LAST<br />UPDATED</th>
      <th scope="col" style="width:75px;">SOURCE</th>
      <th scope="col">USER</th>
      <th scope="col">CATEGORY /<br />SUBCATEGORY</th>
    </tr>
';
    while(($openTicket = mysql_fetch_assoc($result)))
    {
        echo'
        <tr>
          <td><a href="?page=ViewTicket&ticket='.$openTicket['call_id'].'">'.$openTicket['call_id'].'</a></td>
          <td style="width:75px;">'.$openTicket['priority_name'].'</td
          <td>'.$openTicket['caller_name'].'<br />'.$openTicket['caller_dept'].'</td>
          <td>'.$openTicket['cust_name'].'<br />'.$openTicket['cust_rep'].'</td>
          <td style="width:75px;">'.$openTicket['created_on'].'</td>
          <td style="width:75px;">'.$openTicket['updated_on'].'</td>
          <td style="width:75px;">'.$openTicket['source_name'].'</td>
          <td>'.$openTicket['created_by'].'</td>
          <td>'.$openTicket['cat_name'].'<br />'.$openTicket['subcat_name'].'</td>
       </tr>';
   }
   echo '
   <tr>
     <th colspan="9" style="text-align:right;">Total Open Ticket(s): '.$openTicketsCount.'&nbsp;&nbsp;</th>
   </tr>
   </table>';
}

然后改变这个

var count = 1;
$(window).on('load', function() {
    var id = document.getElementById( "Search_Var" ).innerText;
    $('.page').on('click', function() {
        var page = $(this).html();
        if(page == "Next")
        {
            count = count + 1;
        }
        if(page == "Previous")
        {
            count = count - 1;
        }
        if(count < 1)
        {
            count = 1;
        }
        if(count > parseInt( $("#pageCount").val()))
        {
            count = parseInt($("#pageCount").val());
        }
        $.get("/*******/********/Search_Pagination.inc.php?count_id=" + count + "&search_id=" + id, function(data) {
            $('#page').html(data);
        });
    });
});

编辑:

可能是页面计数不是整数,所以只检查页面计数是否返回一个有效的整数,还添加了parseInt,这可能是它无法正常工作的原因

答案 1 :(得分:0)

我不知道为什么“添加评论”没有显示给我,

但我想说不要使用函数mysql _ *

尝试使用像PDO这样的东西