Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 求救:关于这段代码中取数据的问题!!

求救:关于这段代码中取数据的问题!!

Jun 23, 2016 pm 02:13 PM

本帖最后由 XiaoShengbboy 于 2013-06-14 00:42:16 编辑

向各位求救
$sql="select * from xym_pic";
$res=mysql_query($sql,$conn);
while ($row=mysql_fetch_assoc($res)){
$data[]=$row;
}// 随机抽取9条记录以模拟实际情况
$keys = array_rand($data, 10);
$json = array();
foreach($keys as $key){
$json[] = $data[$key];
}
echo json_encode( $json );
这段代码中瀑布流data.php部分的代码,每次获取一次,就从数据库里面取出数据 放到数组$data[]里面,原来的是随机去取9条记录,我想让这段编程,每次获取去除按时间排序的9条记录,就是第一次取出来的是0-9,第二次是10-9,以此类推!那位能帮忙告诉怎么取出数据呢?万分感谢啊!小弟奉上100分,解决了另加!!!如果好心人原意加QQ指导,那是相当的感激啊:QQ 2 7 7 6 2 8 1 8 1

回复讨论(解决方案)

session_start();if(! isset($_SESSION['last'])) $_SESSION['last'] = 0;$last = $_SESSION['last'];$sql="select * from xym_pic limit $last,9";$res=mysql_query($sql,$conn);while ($row=mysql_fetch_assoc($res)){  $data[]=$row;}$_SESSION['last'] += count($data);echo json_encode( $data )
Copy after login
Copy after login
Copy after login
;

感谢版主的回复 已经解决了 谢谢谢谢!!!!!!!!!

session_start();if(! isset($_SESSION['last'])) $_SESSION['last'] = 0;$last = $_SESSION['last'];$sql="select * from xym_pic limit $last,9";$res=mysql_query($sql,$conn);while ($row=mysql_fetch_assoc($res)){  $data[]=$row;}$_SESSION['last'] += count($data);echo json_encode( $data )
Copy after login
Copy after login
Copy after login
;

我还有一个小问题,就是我数据库里面一共有 80多条记录,每次取出来九条,取出来8次,就取完了,但是我刷新页面的时候session还在,我就取不出来数据了,请问怎么解决一下呢?谢谢啊


session_start();if(! isset($_SESSION['last'])) $_SESSION['last'] = 0;$last = $_SESSION['last'];$sql="select * from xym_pic limit $last,9";$res=mysql_query($sql,$conn);while ($row=mysql_fetch_assoc($res)){  $data[]=$row;}$_SESSION['last'] += count($data);echo json_encode( $data )
Copy after login
Copy after login
Copy after login
;

我还有一个小问题,就是我数据库里面一共有 80多条记录,每次取出来九条,取出来8次,就取完了,但是我刷新页面的时候session还在,我就取不出来数据了,请问怎么解决一下呢?谢谢啊
瀑布流应该也不会用session存放吧?应该是和分页一样通过请求参数确定数据起始吧

取不到了就从头再取

取不到了就从头再取
现在就是因为取不到了,我判断如果结果集是空的话就销毁session,刷新页面还是取不出来数据


取不到了就从头再取
现在就是因为取不到了,我判断如果结果集是空的话就销毁session,刷新页面还是取不出来数据

结果集为空的话 我让session销毁了,但是他又会无限循环了
我想结果集为空了,就不取结果了,直到页面刷新,才重新计数
请问怎么解决呢

session_start();if(! isset($_SESSION['last'])) $_SESSION['last'] = 0;$last = $_SESSION['last']; $sql="select * from xym_pic limit $last,9";$res=mysql_query($sql,$conn);if(mysql_num_rows($res) ==0) {  $last = $_SESSION['last'] = 0;  $sql="select * from xym_pic limit $last,9";  $res=mysql_query($sql,$conn);}while ($row=mysql_fetch_assoc($res)){  $data[]=$row;}$_SESSION['last'] += count($data);echo json_encode( $data )
Copy after login



取不到了就从头再取
现在就是因为取不到了,我判断如果结果集是空的话就销毁session,刷新页面还是取不出来数据

结果集为空的话 我让session销毁了,但是他又会无限循环了
我想结果集为空了,就不取结果了,直到页面刷新,才重新计数
请问怎么解决呢
这个 代码最后的结果集 json是返回给一个JS文件的,js判断页面位置不停的加载新内容,我即使销毁了session,一加载也会重新开始获取数据!
这个很是头疼啊!

你究竟想要做什么?

你究竟想要做什么?
就是这个瀑布流,我想按条取出来最新的80条数据,然后就终止了,我刷新页面,再重新取数据!
现在这个瀑布流的机制是,静态页面调用js,js控制页面当前位置,距离底部多少个px之后获取php文件,这段php代码没问题,就是js获取的时候,是不是得判断一下如果json为空就不在获取呢?

现在是js只要滚动就会不断的获取内容,如果php里面结果集为空,他在获取一次也就会有数据了!

取完了就注销掉 session 或设置 $_SESSION 为空

js 那边当然也需要是否有数据返回,没有的话就不再发请求

取完了就注销掉 session 或设置 $_SESSION 为空

js 那边当然也需要是否有数据返回,没有的话就不再发请求

十分感谢啊,我把JS搞一下试试!
感谢您的耐心帮助哦!!!!!!!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

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)

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,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

See all articles