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

Source: Internet
Author: User
Flash needs JSON format data, so, there are the following code: (PHP implementation, C # Similar, JSON library to go down yourself ~ ~)
Copy Code code as follows:

--Query user's offline information, return JSON, for Flash
if (!empty ($_get[' action ')) &&!empty ($_get[' invite ')) {
Fixed-parameter requests return information
if ($_get[' action ']== ' GetInfo ' &&$_get[' invite ']==1) {
Data entities, an entity class
Class ucinvite{
---data information for display
Public $fuid; User ID
Public $funame; User name
Public $furl; User space Address
---data information for display
Public $fchilds; Child class Collection
}
$invitecount = 0; Total quantity, used to record total number of off-line
Recursive implementation method
function Getshowtreeinvite ($uid) {
Global $_sglobal, $invitecount;
Organize SQL statements and queries, table Fuid is 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; Quantity under each subclass
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 for child class information
$ui->fchilds=getshowtreeinvite ($v [' fuid ']);
Insert in returned array
$inviteTree [$index]= $ui;
$index + +;
$invitecount + +;
$invitezcount + +;
}
Log the current number of offline
$inviteTree [' invitezcount '] = $invitezcount;
Return array Information
return $inviteTree;
}
$fuid = Empty ($_get[' fuid '])? $space [uid]:$_get[' Fuid '];
Query information based on the current login user ID and return a collection
$inviteTree = Getshowtreeinvite ($fuid);
Record total number of offline
$inviteTree [' invitecount '] = $invitecount;
Introducing the JSON library, which uses the Services_json
Because it is not certain that the server is PHP5.2 above, there is no use of the JSON function with the
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's offline information, return JSON, for Flash

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

<script type= "Text/javascript" src= "Http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" ></ Script>
<script type= "Text/javascript" >
Fuid User ID
Funame User Name
Furl User Home Address
Invitezcount number of child classes per user
Invitecount all subordinate numbers for this user
var str = "";
function Getshow (obj,qi,index) {
For (var one in obj)
{
for (var key in Obj[one])
{
index++;
if (key== "Fchilds" &&obj[one][key][' Invitezcount ']!=0) {
var aqi = Qi;
if (index>1) aqi+= ""
Getshow (Obj[one][key],aqi,index)
}
if (Key!= ' Fchilds ') {
if (key== "Fuid") {
STR + + qi+key+ "=" +obj[one][key] + ",";
}else{
STR + + key+ "=" +obj[one][key] + ",";
}
if (key = = "Furl") {
str+= "<br/>";
}
}
}
if (one== "Invitezcount" | | one== "Invitecount") {
Str+=qi+one+ "=" +obj[one]+ "<br/>";
}
}
}
Fixed parameter do=charadegarden&action=getinfo&invite=1,fuid is used for testing and does not fill out the user ID that will invoke the system's current login
var url = ' http://localhost/home/space.php?do=charadegarden&action=getinfo&invite=1&fuid=344 ';
$.get (url,function (HTML) {
Get JSON and convert to object
if (HTML) {
var obj = eval ("[" +html+ "]");
Getshow (Obj[0], "", 1)
document.write (str);
document.write ("<br/><br/><br/><br/>");
document.write ("<b>json format:</b><br/>");
document.write (HTML);
}
});
</script>

In this way, two infinite level of operation is completed, the result screenshot:

Cherish the fruits of labor, although the content is not much, but also a word hit, reprint please specify!! Pay attention to the love of Coke Bar

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.