PHP Infinite-level data JSON format and JS parsing _php instance

Source: Internet
Author: User
Flash needs JSON format data, so, there is the following code: (PHP implementation, C # Similar, JSON library to go down to it ~ ~)
Copy CodeThe code is as follows:
--Query user downline information, return JSON, for Flash
if (!empty ($_get[' action ')) &&!empty ($_get[' invite '])) {
Fixed parameter request will return information
if ($_get[' action ']== ' GetInfo ' &&$_get[' invite ']==1 ') {
Data entity, an entity class
Class ucinvite{
---Display of data information
Public $fuid; User ID
Public $funame; User name
Public $furl; User space Address
---Display of data information
Public $fchilds; Sub-class Collection
}
$invitecount = 0; Total number, used to record total number of downline
Recursive implementation method
function Getshowtreeinvite ($uid) {
Global $_sglobal, $invitecount;
Organization SQL statement and query, table Fuid is offline Id,uid is the parent id,fusername is the user name
$INV _sql = "Select Fuid,fusername from". Tname ("Invite"). "Where uid =". $uid;
$INV _query = $_sglobal[' db ']->query ($inv _sql);
The following table and the returned array
$index = 0;
$inviteTree = Array ();
$invitezcount = 0; The number under each sub-class
Loop to add information to an array
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 '];
Call yourself, recursively query subclass information
$ui->fchilds=getshowtreeinvite ($v [' fuid ']);
Insert in the returned array
$inviteTree [$index]= $ui;
$index + +;
$invitecount + +;
$invitezcount + +;
}
Record the number of downline in the current downline
$inviteTree [' invitezcount '] = $invitezcount;
Returning array information
return $inviteTree;
}
$fuid = Empty ($_get[' Fuid ')? $space [uid]:$_get[' Fuid '];
Queries information based on the current logged-in user ID and returns a collection
$inviteTree = Getshowtreeinvite ($fuid);
Record total number of downline
$inviteTree [' invitecount '] = $invitecount;
Introduction of the JSON Library, here is Services_json
Because it is not certain that the server is PHP5.2 or above, it does not use its own JSON function
Require_once ('.. /plugins/json/json.php ');
JSON output
$json = new Services_json ();
echo $json->encode ($inviteTree);
Exit
}else{
Echo ' request parameter Error! ';
Exit
}
}
--Query user downline information, return JSON, for Flash

The above code returns a JSON string such as:
{' 0 ': {' fuid ': ' 950 ', ' funame ': ' Guo Zi original ', ' furl ': '/home/space.php?uid=950 ', ' fchilds ': {' Invitezcount ': 0}}, ' Invitezcount ': 1, ' Invitecount ': 1}
Only one is queried here.
Next use JS for the following parsing:
Copy CodeThe code is as follows:



In this way, two infinite-level operations are completed, resulting in:

Cherish the fruits of labor, although the content is not much, but also a word in a dozen, reproduced please specify!! Pay attention to the love and fight Coke.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.