Table of Contents
Related recommendations:
Home Backend Development PHP Tutorial PHP+jQuery method to implement the function of dynamically loading data without refreshing on the scroll screen

PHP+jQuery method to implement the function of dynamically loading data without refreshing on the scroll screen

May 23, 2018 am 09:39 AM
php dynamic

This article mainly introduces the function of PHP jQuery to dynamically load data in scrolling without refreshing, and involves the specific operation skills of dynamically reading the database and loading data in PHP to achieve scrolling without refreshing. Friends in need can refer to it

The details are as follows:

index.php

<?php
require_once(&#39;connect.php&#39;); //连接数据库
$user = array(&#39;demo1&#39;,&#39;demo2&#39;,&#39;demo3&#39;,&#39;demo3&#39;,&#39;<de></de>mo4&#39;); //模拟了几个用户
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
  <title>滚屏加载--无刷新动态加载数据技术的应用</title>
  <style type="text/css">
    #container{margin:10px auto;width: 660px; border: 1px solid #999;}
    .single_item{padding: 20px; border-bottom: 1px dotted #d3d3d3;}
    .author{position: absolute; left: 0px; font-weight:bold; color:#39f}
    .date{position: absolute; right: 0px; color:#999}
    .content{line-height:20px; word-break: break-all;}
    .element_head{width: 100%; position: relative; height: 20px;}
    .nodata{display:none; height:32px; line-height:32px; text-align:center; color:#999; font-size:14px}
  </style>
  <script type="text/javascript" src="jquery-1.8.3.min.js"></script>  //需要引入jquery
</head>
<body>
  <p class="one" style="margin:20px">提示:使用滚动或拉动滚动条向下看。</p>
  <p id="container">
    <?php
    $query=mysqli_query($link, "select * from say order by id desc limit 0,15");
    while ($row=mysqli_fetch_array($query, MYSQLI_ASSOC)) {
    ?>
    <p class="single_item">
      <p class="element_head">
         <p class="date"><?php echo date(&#39;m-d H:i&#39;,$row[&#39;addtime&#39;]);?></p>
         <p class="author"><?php echo $user[$row[&#39;userid&#39;]];?></p>
       </p>
       <p class="content"><?php echo $row[&#39;content&#39;];?></p>
      </p>
    <?php } ?>
    </p>
  <p class="nodata"></p>
</body>
<script type="text/javascript">
$(function(){
  var winH = $(window).height(); //页面可视区域高度
  var i = 1;//设置当前页数
  $(window).scroll(function () {
    var pageH = $(document.body).height();
    var scrollT = $(window).scrollTop(); //滚动条top
    var aa = (pageH-winH-scrollT)/winH;
    if(aa<0.02){
      $.getJSON("result.php",{page:i},function(json){
        if(json){
          var str = "";
          $.each(json,function(index,array){
            var str = "<p class=\"single_item\"><p class=\"element_head\">";
            var str = str + "<p class=\"date\">"+array[&#39;date&#39;]+"</p>";
            var str = str + "<p class=\"author\">"+array[&#39;author&#39;]+"</p>";
            var str = str + "</p><p class=\"content\">"+array[&#39;content&#39;]+"</p></p>";
            $("#container").append(str);
          });
          i++;
        }else{
          $(".nodata").show().html("别滚动了,已经到底了。。。");
          return false;
        }
      });
    }
  });
});
</script>
</html>
Copy after login

ajax_demo.sql

-- phpMyAdmin SQL Dump
-- version 3.5.2.2
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2015 年 01 月 18 日 15:56
-- 服务器版本: 5.1.46-community
-- PHP 版本: 5.2.13
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- 数据库: `demo`
--
-- --------------------------------------------------------
--
-- 表的结构 `say`
--
CREATE TABLE IF NOT EXISTS `say` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `userid` int(11) NOT NULL DEFAULT &#39;0&#39;,
 `content` varchar(200) NOT NULL,
 `addtime` int(10) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=63 ;
--
-- 转存表中的数据 `say`
--
INSERT INTO `say` (`id`, `userid`, `content`, `addtime`) VALUES
(1, 0, &#39;爱爱爱&#39;, 1421332482),
(2, 1, &#39;爱爱爱&#39;, 1421332482),
(3, 0, &#39;爱爱爱&#39;, 1421332482),
(4, 1, &#39;爱爱爱&#39;, 1421332482),
(5, 0, &#39;爱爱爱&#39;, 1421332482),
(6, 0, &#39;爱爱爱&#39;, 1421332482),
(7, 0, &#39;爱爱爱&#39;, 1421332482),
(8, 2, &#39;爱爱爱&#39;, 1421332482),
(9, 0, &#39;爱爱爱&#39;, 1421332482),
(10, 0, &#39;爱爱爱&#39;, 1421332482),
(11, 0, &#39;爱爱爱&#39;, 1421332482),
(12, 0, &#39;爱爱爱&#39;, 1421332482),
(13, 0, &#39;爱爱爱&#39;, 1421332482),
(14, 0, &#39;爱爱爱&#39;, 1421332482),
(15, 0, &#39;爱爱爱&#39;, 1421332482),
(16, 0, &#39;爱爱爱&#39;, 1421332482),
(17, 0, &#39;爱爱爱&#39;, 1421332482),
(18, 0, &#39;爱爱爱&#39;, 1421332482),
(19, 0, &#39;爱爱爱&#39;, 1421332482),
(20, 0, &#39;爱爱爱&#39;, 1421332482),
(21, 0, &#39;爱爱爱&#39;, 1421332482),
(22, 0, &#39;爱爱爱&#39;, 1421332482),
(23, 0, &#39;爱爱爱&#39;, 1421332482),
(24, 0, &#39;爱爱爱&#39;, 1421332482),
(25, 0, &#39;爱爱爱&#39;, 1421332482),
(26, 0, &#39;2222&#39;, 1421333156),
(27, 0, &#39;2222&#39;, 1421333159),
(28, 0, &#39;2222&#39;, 1421333161),
(29, 0, &#39;2222&#39;, 1421333162),
(30, 0, &#39;2222&#39;, 1421333164),
(31, 0, &#39;2222&#39;, 1421333165),
(32, 0, &#39;2222&#39;, 1421333167),
(33, 0, &#39;2222&#39;, 1421333168),
(34, 0, &#39;2222&#39;, 1421333169),
(35, 0, &#39;2222&#39;, 1421333170),
(36, 0, &#39;2222&#39;, 1421333172),
(37, 0, &#39;2222&#39;, 1421333173),
(38, 0, &#39;2222&#39;, 1421333175),
(39, 0, &#39;2222&#39;, 1421333176),
(40, 0, &#39;2222&#39;, 1421333177),
(41, 0, &#39;2222&#39;, 1421333178),
(42, 0, &#39;2222&#39;, 1421333179),
(43, 0, &#39;2222&#39;, 1421333181),
(44, 0, &#39;2222&#39;, 1421333182),
(45, 0, &#39;2222&#39;, 1421333183),
(46, 0, &#39;2222&#39;, 1421333184),
(47, 0, &#39;2222&#39;, 1421333293),
(48, 0, &#39;2222&#39;, 1421333295),
(49, 0, &#39;2222&#39;, 1421333296),
(50, 0, &#39;2222&#39;, 1421333297),
(51, 0, &#39;2222&#39;, 1421333298),
(52, 0, &#39;2222&#39;, 1421333299),
(53, 0, &#39;2222&#39;, 1421333300),
(54, 0, &#39;2222&#39;, 1421333302),
(55, 0, &#39;2222&#39;, 1421333303),
(56, 0, &#39;2222&#39;, 1421333304),
(57, 0, &#39;2222&#39;, 1421333305),
(58, 0, &#39;2222&#39;, 1421333306),
(59, 0, &#39;2222&#39;, 1421333308),
(60, 0, &#39;2222&#39;, 1421333309),
(61, 0, &#39;2222&#39;, 1421333310),
(62, 0, &#39;2222&#39;, 1421333311);
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Copy after login

result.php Receive request page

<?php
require_once(&#39;connect.php&#39;); //连接数据库
$user = array(&#39;demo1&#39;,&#39;demo2&#39;,&#39;demo3&#39;,&#39;demo3&#39;,&#39;demo4&#39;);
$page = intval($_GET[&#39;page&#39;]); //获取请求的页数
$start = $page*15;
$query=mysqli_query($link, "select * from say order by id desc limit $start,15");
/* while ($row=mysqli_fetch_array($query)) {
  $arr[] = array(
    &#39;content&#39;=>$row[&#39;content&#39;],
    &#39;author&#39;=>$user[$row[&#39;userid&#39;]],
    &#39;date&#39;=>date(&#39;m-d H:i&#39;,$row[&#39;addtime&#39;])
  );
} */
if($query){
  while ($row=mysqli_fetch_array($query)) {
    $arr[] = array(
      &#39;content&#39;=>$row[&#39;content&#39;],
      &#39;author&#39;=>$user[$row[&#39;userid&#39;]],
      &#39;date&#39;=>date(&#39;m-d H:i&#39;,$row[&#39;addtime&#39;])
    );
  }
}
if(!empty($arr)){
  echo json_encode($arr); //转换为json数据输出
}
//echo json_encode($arr); //转换为json数据输出
?>
Copy after login

connect.php Database configuration file

<?php
$host="localhost";
$db_user="root";
$db_pass="admin";
$db_name="ajax_demo";
$timezone="Asia/Shanghai";
$link=mysqli_connect($host,$db_user,$db_pass);
mysqli_select_db($link, $db_name);
mysqli_query($link, "SET names UTF8");
header("Content-Type: text/html; charset=utf-8");
?>
Copy after login

PHPArray function knowledge summary_php skills

PHPObject-oriented const constant modifier

##PHP Random number C extended random number

The above is the detailed content of PHP+jQuery method to implement the function of dynamically loading data without refreshing on the scroll screen. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles