search
HomeBackend DevelopmentPHP Tutorialphp 无限级数据JSON格式及JS解析_php实例

Flash需要JSON格式的数据,于是,就有了如下代码:(PHP实现,C#类似,JSON库自己去下吧~~)

复制代码 代码如下:

//--查询用户下线信息,返回JSON,用于flash
if(!empty($_GET['action'])&&!empty($_GET['invite'])){
//固定参数请求才会返回信息
if($_GET['action']=='getinfo'&&$_GET['invite']==1){
//数据实体,一个实体类
class UcInvite{
//---显示用的数据信息
public $fuid; //用户id
public $funame; //用户名
public $furl; //用户空间地址
//---显示用的数据信息
public $fchilds; //子类集合
}
$invitecount = 0; //总数量,用于记录全部下线数量
//递归实现方法
function GetShowTreeInvite($uid){
global $_SGLOBAL,$invitecount;
//组织sql语句并查询,表中fuid是下线的id,uid是父id,fusername是用户名字
$inv_sql = "select fuid,fusername from ".tname("invite")." where uid = ".$uid;
$inv_query = $_SGLOBAL['db']->query($inv_sql);
//下表及返回数组
$index = 0;
$inviteTree = Array();
$invitezcount = 0; //每个子类下的数量
//循环添加信息到数组
while($v = $_SGLOBAL['db']->fetch_array($inv_query)){
$ui = new UcInvite();
$ui->fuid = $v['fuid'];
$ui->funame = $v['fusername'];
$ui->furl = "/home/space.php?uid=".$v['fuid'];
//调用自己,递归查询子类信息
$ui->fchilds=GetShowTreeInvite($v['fuid']);
//插入返回的数组中
$inviteTree[$index]=$ui;
$index++;
$invitecount++;
$invitezcount++;
}
//记录当前下线的下线数量
$inviteTree['invitezcount'] = $invitezcount;
//返回数组信息
return $inviteTree;
}
$fuid = empty($_GET['fuid'])?$space[uid]:$_GET['fuid'];
//根据当前登录用户ID查询信息,并返回一个集合
$inviteTree = GetShowTreeInvite($fuid);
//记录总下线数
$inviteTree['invitecount'] = $invitecount;
//引入json库,这里用的是Services_JSON
//因为并不确定服务器都是PHP5.2以上版本,所以没有用自带的JSON函数
require_once('../plugins/JSON/JSON.php');
//json输出
$json = new Services_JSON();
echo $json->encode($inviteTree);
exit;
}else{
echo '请求参数错误!';
exit;
}
}
//--查询用户下线信息,返回JSON,用于flash

以上代码返回一个JSON字符串,如:
{'0':{'fuid':'950','funame':'郭梓原','furl':'/home/space.php?uid=950','fchilds':{'invitezcount':0}},'invitezcount':1,'invitecount':1}
这里只查询了一个
接下来用JS进行以下解析:
复制代码 代码如下:




这样,两个无限级的操作就完成了,结果截图:

珍惜劳动成果,虽说内容不多,可是也是一个一个字打上的,转载请注明!!关注爱拼可乐吧

Statement
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
What are the limitations of using PHP sessions?What are the limitations of using PHP sessions?Apr 30, 2025 am 12:04 AM

PHPsessionshaveseverallimitations:1)Storageconstraintscanleadtoperformanceissues;2)Securityvulnerabilitieslikesessionfixationattacksexist;3)Scalabilityischallengingduetoserver-specificstorage;4)Sessionexpirationmanagementcanbeproblematic;5)Datapersis

Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

What is the difference between PHP and ASP.NET?What is the difference between PHP and ASP.NET?Apr 28, 2025 pm 04:56 PM

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

See all articles

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

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),