PHP unlimited data JSON format and JS parsing

  • 2020-03-31 20:56:44
  • OfStack

Flash needs data in JSON format, so, we have the following code :(PHP implementation, similar to C#, JSON library to the next ~~)
 
//-- query user referrals and return JSON for flash
if(!empty($_GET['action'])&&!empty($_GET['invite'])){ 
//Fixed parameter requests return information
if($_GET['action']=='getinfo'&&$_GET['invite']==1){ 
//Data entity, an entity class
class UcInvite{ 
//-- data information for display
public $fuid; //The user id
public $funame; //The user name
public $furl; //User space address
//-- data information for display
public $fchilds; //Subclasses collection
} 
$invitecount = 0; //The total number is used to record the total number of referrals
//Recursive implementation method
function GetShowTreeInvite($uid){ 
global $_SGLOBAL,$invitecount; 
//Organize SQL statements and query, fuid in the table is the id of the referral,uid is the parent, fusername is the username
$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 subclass
//Loop to add information to the 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 itself, recursively querying subclass information
$ui->fchilds=GetShowTreeInvite($v['fuid']); 
//Insert into the returned array
$inviteTree[$index]=$ui; 
$index++; 
$invitecount++; 
$invitezcount++; 
} 
//Record the current number of referrals
$inviteTree['invitezcount'] = $invitezcount; 
//Return array information
return $inviteTree; 
} 
$fuid = empty($_GET['fuid'])?$space[uid]:$_GET['fuid']; 
//Queries for information based on the currently logged in user ID and returns a collection
$inviteTree = GetShowTreeInvite($fuid); 
//Record the total number of referrals
$inviteTree['invitecount'] = $invitecount; 
//Introduce the json library, where Services_JSON is used
//Since we are not sure that the server is PHP5.2 or above, we do not use the JSON function that comes with it
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 referrals and return JSON for flash

The above code returns a JSON string, such as:
{' 0 ': {' fuid' : '950', 'funame' : 'Guo Ziyuan', 'furl' : '/ home/space. PHP? Uid = 950 ', 'fchilds: {' invitezcount: 0}},' invitezcount ': 1,' invitecount ": 1}
There is only one query
Next, JS is used for the following analysis:
 
<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 page address
//Invitezcount is the number of subclasses per user
//Invitecount the number of all children of that 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 when testing, do not fill in the user ID that will call the current system login
var url = 'http://localhost/home/space.php?do=charadegarden&action=getinfo&invite=1&fuid=344'; 
$.get(url,function(html){ 
//Get the json and turn it into an 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, the operation of two infinite levels is completed, and the result is screenshot:

Cherish the results of labor, although the content is not much, but is also a word to play, reproduced please indicate! Attention (link: http://www.iphclub.com/wp/index.php/iph/201)


Related articles: