php 無限級資料JSON格式及JS解析_PHP教程

來源:互聯網
上載者:User
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進行以下解析:
複製代碼 代碼如下:



這樣,兩個無限級的操作就完成了,結果:

珍惜勞動成果,雖說內容不多,可是也是一個一個字打上的,轉載請註明!!關注愛拼可樂吧

http://www.bkjia.com/PHPjc/322250.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/322250.htmlTechArticleFlash需要JSON格式的資料,於是,就有了如下代碼:(PHP實現,C#類似,JSON庫自己去下吧~~) 複製代碼 代碼如下: //--查詢使用者下線資訊,返回J...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.