0

社区,我目前在我的页面上有一些分页,以帮助用户浏览记录的票证。为了解决这个问题,我有两个表格单元格,每个单元格分别包含 Next 和 Previous。现在,当用户单击其中一个时,它会将 +1 或 -1 添加到 AJAX 是字符串的 JS 计数器。然后,我的分页可以转到我的 mysql 数据库中的下一个或上一个 15 项。我编写了我的 JS,以便如果计数器小于 1,它将返回到 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>';
}
4

2 回答 2

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,这可能是它不起作用的原因

于 2013-10-03T12:42:16.663 回答
0

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

但我想说不要使用函数 mysql_*

尝试使用类似 PDO 的东西

于 2013-10-04T17:15:19.440 回答