2012-08-16 40 views
0

我在MySQL的2個表如何設計一個數組結構並使用PHP編碼爲Json?

ID  Name 
===================== 
1  Section1 
2  Section2 

類別

ID  SectionID  Name 
========================================= 
1   1   Category1 
2   1   Category2 
3   2   Category3 

這是我現在有:

$sql_section = "select * from section";<br> 
$sql_category = "select * from category";<br> 
$result_section = mysql_query($sql_section) or die("Could not execute query."); 
$result_category = mysql_query($sql_category) or die("Could not execute query."); 

echo json_encode(???????); 

而且我想編碼JSON在PHP中得到看起來像這樣的結果:

{sections:[ 
{sectionName: "Section1", categoryList: [{categoryName: "category1"}, 
       {categoryName: "category2"}]}, 
{sectionName: "Section1", categoryList: [{categoryName: "category3"}]}<br> 
]} 

的我如何設計一個數組,看起來像這樣的任何線索?

+0

請不要使用過時的mysql_XXX功能,使用庫MySQLi或PDO。 – Barmar 2012-08-16 07:30:54

+0

爲什麼?任何建議的文章閱讀? – Vicheanak 2012-08-16 11:54:43

+0

讀http://stackoverflow.com/questions/548986/mysql-vs-mysqli-in-php – Barmar 2012-08-16 15:00:26

回答

2
$arr = array('sections' => array()); 
$arr['sections'][] = array('sectionName' => array('categoryList' => array(array('categoryName' => 'Category 1'), array('categoryName' => 'Category 2')))); 
$arr['sections'][] = array('sectionName' => array('categoryList' => array(array('categoryName' => 'Category 3'), array('categoryName' => 'Category 4')))); 
echo json_encode($arr); 

輸出://

{"sections":[ 
    {"sectionName": 
     {"categoryList": 
     [{"categoryName":"Category 1"}, 
      {"categoryName":"Category 2"}]} 
     }, 
    {"sectionName": 
     {"categoryList": 
     [{"categoryName":"Category 3"},{"categoryName":"Category 4"}]}}]} 

你只需要使用變量替換的字符串值,並把它的循環,以創建所需的數據集。

+0

這對像我這樣的初學者真的很有幫助。謝謝! – Vicheanak 2012-08-16 11:54:10

0

像這樣的東西應該工作。

$sections = mysql_query("select * from section") or die("Could not execute query."); 
$result = array(); 
if(mysql_num_rows($sections)>0) { 
    while($section = mysql_fetch_assoc($sections)) { 
     $result['sections'][$section['ID']] = $section['Name']; 
     $categories = mysql_query("select * from category where SectionID='".mysql_real_escape_string($section['ID'])."'"); 
     if(mysql_num_rows($categories)>0) { 
      while($category = mysql_fetch_assoc($categories)) { 
        $result['sections'][$section['ID']]['categoryList'][$category['ID']] = $category['Name']; 
      } 
     } 
    } 
} 

echo json_encode($result); 

它會像下面那樣輸出,而不是sectionName作爲索引我使用了節ID比較好。類別相同。

{sections:[ 
{sectionID: "SectionName", categoryList: [{categoryID: "categoryName"}, 
       {categoryName: "category2"}]}, 
{sectionID: "SectionName", categoryList: [{categoryID: "categoryName"}]}<br> 
]} 
0
$sections = array(); 
$categories = array(); 
while ($row = mysql_fetch_object($result_section)) 
    $sections[$row->ID] = array('sectionName' => $row->Name, 'categoryList' => array()); 
while ($row = mysql_fetch_object($result_category)) 
    $sections[$row->sectionID]['categoryList'][] = array('categoryName' => $row->Name); 
相關問題