doumi9618 2013-04-13 07:42
浏览 24

页面加载的增量计数

I am trying to create a thumbnails pagination using Jquery. everything is working perfectly fine. My only problem is I do not know how to increment count on each page load. Below is my code. My main goal is, if the user click the next button, I want to increment 1 on this php/reverse.php?page="+page. If there are no more result from php, do not allow to increment anymore. Same thing on previous button.

    <html>
  <head>
    <title></title>
  </head>
  <body>
      <div id="par"></div>
      <input type="button" id="prev" value="Prev"/>
      <input type="button" id="next" value="Next"/>

      <script src="js/jquery.js"></script>
      <script>
        var default_val = 1;
        function get_page(page){
          $.ajax({
            url: "php/reverse.php?page="+page
          }).done(function(data) {
            $('#par').html(data.thumbs);
          });
        }

        $('#prev').click(function(){
            get_page(default_val)
        });


        $('#next').click(function(){
         get_page(default_val+1)
        });

        get_page(default_val);
      </script>
  </body>
</html>

Here is my PHP:

<?php
  require '../php/connect.inc.php';
  $per_page = 4;

  $pages_query = mysql_query("SELECT COUNT('id') FROM my_list");
  $pages = ceil(mysql_result($pages_query, 0) / $per_page);

  $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
  $start = ($page - 1) * $per_page;


  $query = mysql_query("SELECT id, image_small FROM my_list LIMIT $start, $per_page");

  $results = array();
  while($mysql_fetch_assoc = mysql_fetch_assoc($query)){    
    $results[] = "<img src='img/".$mysql_fetch_assoc['image_small']."'/>";
  }      


  header('Content-type: application/json');

  echo json_encode(array(
    'thumbs' => $results,
    'pages' => $pages,
    'current_page' => $page,
    'per_page' => $per_page
  ));
?>
  • 写回答

1条回答 默认 最新

  • dotwc62080 2013-04-13 08:04
    关注

    first you need to return status of request

    var page = 1;
    function get_page(page){
      $.ajax({
        dataType: 'json',
        url: "php/reverse.php?page="+page
      }).done(function(data) {
        if (data.thumbs.length>0) {
          $('#par').html(data.thumbs);
          return true; // here
        } else
          return false; // and here
      });
    }
    

    next, what you want is blocking function when json will return false and increase or decrease page count

    var block_next = false,
        block_prev = false;
    
    $('#prev').click(function(){
      block_next = false;
      if (!block_prev)
        if (!get_page(page))
          block_prev = true;
        else
          page = page - 1;
    });
    
    $('#next').click(function(){
      block_prev = false;
      if (!block_next)
        get_page(page+1)
          block_next = true;
        else
          page = page + 1;
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)