gpt4 book ai didi

javascript - PHP中使用ajax滚动到窗口底部的分页

转载 作者:行者123 更新时间:2023-11-30 00:25:23 25 4
gpt4 key购买 nike

我从一个链接中获得了一些在线代码,其中数组上的分页是通过大量记录完成的。示例.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<title>PHP Array Pagination</title>
<style>
<!--
body {
font-family: Tahoma, Verdana, Arial, Sans-serif;
font-size: 11px;
}
hr {
border: 1px #ccc;
border-style: none none solid none;
margin: 20px 0;
}
a {
color: #333;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a.selected {
font-weight: bold;
text-decoration: underline;
}
.numbers {
line-height: 20px;
word-spacing: 4px;
}
//-->
</style>
</head>
<body>
<h1>PHP Array Pagination</h1>
<hr />
<?php

ini_set('display_errors','On');
error_reporting(E_ALL);

// Include the pagination class
include 'pagination.class.php';

// some example data
foreach (range(1, 200) as $value) {
$products[] = array(
'Product' => 'Product '.$value,
'Price' => rand(100, 1000),
);
}

// If we have an array with items
if (count($products)) {
// Create the pagination object
$pagination = new pagination($products, (isset($_GET['page']) ? $_GET['page'] : 1), 15);
// Decide if the first and last links should show
$pagination->setShowFirstAndLast(false);
// You can overwrite the default seperator
$pagination->setMainSeperator(' | ');
// Parse through the pagination class
$productPages = $pagination->getResults();
// If we have items
if (count($productPages) != 0) {
// Create the page numbers
echo $pageNumbers = '<div class="numbers">'.$pagination->getLinks($_GET).'</div>';
// Loop through all the items in the array
foreach ($productPages as $productArray) {
// Show the information about the item
echo '<p><b>'.$productArray['Product'].'</b> &nbsp; &pound;'.$productArray['Price'].'</p>';
}
// print out the page numbers beneath the results
echo $pageNumbers;
}
}
?>
<hr />
<p><a href="http://www.lotsofcode.com/php/projects/php-array-pagination" target="_blank">PHP Array Pagination</a> provided by <a href="http://www.lotsofcode.com/" target="_blank">Lots of Code</a></p>
</body>
</html>

分页.class.php

<?php

/************************************************************\
*
* PHP Array Pagination Copyright 2007 - Derek Harvey
* www.lotsofcode.com
*
* This file is part of PHP Array Pagination .
*
* PHP Array Pagination is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* PHP Array Pagination is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PHP Array Pagination ; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
\************************************************************/

class pagination
{

/**
* Properties array
* @var array
* @access private
*/
private $_properties = array();

/**
* Default configurations
* @var array
* @access public
*/
public $_defaults = array(
'page' => 1,
'perPage' => 10
);

/**
* Constructor
*
* @param array $array Array of results to be paginated
* @param int $curPage The current page integer that should used
* @param int $perPage The amount of items that should be show per page
* @return void
* @access public
*/
public function __construct($array, $curPage = null, $perPage = null)
{
$this->array = $array;
$this->curPage = ($curPage == null ? $this->defaults['page'] : $curPage);
$this->perPage = ($perPage == null ? $this->defaults['perPage'] : $perPage);
}

/**
* Global setter
*
* Utilises the properties array
*
* @param string $name The name of the property to set
* @param string $value The value that the property is assigned
* @return void
* @access public
*/
public function __set($name, $value)
{
$this->_properties[$name] = $value;
}

/**
* Global getter
*
* Takes a param from the properties array if it exists
*
* @param string $name The name of the property to get
* @return mixed Either the property from the internal
* properties array or false if isn't set
* @access public
*/
public function __get($name)
{
if (array_key_exists($name, $this->_properties)) {
return $this->_properties[$name];
}
return false;
}

/**
* Set the show first and last configuration
*
* This will enable the "<< first" and "last >>" style
* links
*
* @param boolean $showFirstAndLast True to show, false to hide.
* @return void
* @access public
*/
public function setShowFirstAndLast($showFirstAndLast)
{
$this->_showFirstAndLast = $showFirstAndLast;
}

/**
* Set the main seperator character
*
* By default this will implode an empty string
*
* @param string $mainSeperator The seperator between the page numbers
* @return void
* @access public
*/
public function setMainSeperator($mainSeperator)
{
$this->mainSeperator = $mainSeperator;
}

/**
* Get the result portion from the provided array
*
* @return array Reduced array with correct calculated offset
* @access public
*/
public function getResults()
{
// Assign the page variable
if (empty($this->curPage) !== false) {
$this->page = $this->curPage; // using the get method
} else {
$this->page = 1; // if we don't have a page number then assume we are on the first page
}

// Take the length of the array
$this->length = count($this->array);

// Get the number of pages
$this->pages = ceil($this->length / $this->perPage);

// Calculate the starting point
$this->start = ceil(($this->page - 1) * $this->perPage);

// return the portion of results
return array_slice($this->array, $this->start, $this->perPage);
}

/**
* Get the html links for the generated page offset
*
* @param array $params A list of parameters (probably get/post) to
* pass around with each request
* @return mixed Return description (if any) ...
* @access public
*/
public function getLinks($params = array())
{
// Initiate the links array
$plinks = array();
$links = array();
$slinks = array();

// Concatenate the get variables to add to the page numbering string
$queryUrl = '';
if (!empty($params) === true) {
unset($params['page']);
$queryUrl = '&amp;'.http_build_query($params);
}

// If we have more then one pages
if (($this->pages) > 1) {
// Assign the 'previous page' link into the array if we are not on the first page
if ($this->page != 1) {
if ($this->_showFirstAndLast) {
$plinks[] = ' <a href="?page=1'.$queryUrl.'">&laquo;&laquo; First </a> ';
}
$plinks[] = ' <a href="?page='.($this->page - 1).$queryUrl.'">&laquo; Prev</a> ';
}

// Assign all the page numbers & links to the array
for ($j = 1; $j < ($this->pages + 1); $j++) {
if ($this->page == $j) {
$links[] = ' <a class="selected">'.$j.'</a> '; // If we are on the same page as the current item
} else {
$links[] = ' <a href="?page='.$j.$queryUrl.'">'.$j.'</a> '; // add the link to the array
}
}

// Assign the 'next page' if we are not on the last page
if ($this->page < $this->pages) {
$slinks[] = ' <a href="?page='.($this->page + 1).$queryUrl.'"> Next &raquo; </a> ';
if ($this->_showFirstAndLast) {
$slinks[] = ' <a href="?page='.($this->pages).$queryUrl.'"> Last &raquo;&raquo; </a> ';
}
}

// Push the array into a string using any some glue
return implode(' ', $plinks).implode($this->mainSeperator, $links).implode(' ', $slinks);
}
return;
}
}

example.php 文件中的数组中有很多记录,即从 1-200。example.php 中包含一个文件,即 pagination.class.php,其中提供了分页逻辑。我需要在滚动到窗口底部时完成分页。就像当您向下滚动到底部时一样,下一页记录应该通过 ajax 调用显示。

就像在这段代码中一样。将给出页数链接。单击它们时,将显示下一页记录。当我们使用ajax滚动到底部时,我需要类似的东西。

请指导我如何执行此操作...

最佳答案

每次您处于底部位置时,您都可以进行 ajax 调用来加载更多数据,例如

if (($(window).innerHeight() + $(window).scrollTop()) >= $("body").height()) {
//make ajax calls to load you data
}

关于javascript - PHP中使用ajax滚动到窗口底部的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22931983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com