2016-10-07 105 views
0

這些是數據,因爲它們在MySQL表科瑞樹狀陣列或子陣列

表A(任務)

task_id | name | description 
----------------------------- 
    1  | soccer| fora 
----------------------------- 
    2  | sussam| forb 
----------------------------- 
    3  | sosssi| forc 
----------------------------- 
    4  | sillly| ford 

於此項目表格乙里程碑

mile_id | name | task_id 
------------------ 
    1  | task1mi | 1 
------------------ 
    2  | task2mi | 1 
------------------- 
    3  | task3mi | 3 

我期待使一個treeview數組,類似於每個任務的父類里程碑,作爲任務ID的子數組。

什麼的print_r()函數將返回(和PHP所需的輸出MySQL查詢後)

array(   
      name=>'soccer', 
      description =>'fora' 
      task_id=>array(
        mile_id=>'1', 
        name=>'task1mi'     
      ) 
     ) 

任何建議

+0

這是否一個純粹的陣列或MySQL表? – hassan

+0

從MySQL返回使用PHP,即時通訊尋找從MySQL結果建立一個數組樹 – Alphy

+0

爲什麼你不使用連接或子查詢,而不是返回一個數組,然後將它們轉換爲另一個視圖! – hassan

回答

0

你可以做下面的方式,但它是不好的表現。我不知道確切的要求和你使用的鏤空。所以我給出了基本的想法。

$sqlparent = "select * from tasks";// get your task details from database. 
$parentData = $sqlparent;// result from sql function. 
foreach($parentData as $key=>$value) 
{ 
    $sql = "select * from Milestones where task_id='".$value['task_id']."'";// find the Milestones of the task 
$milestonesResult = mysql_featch_array($sql); // run sql and get data from database it is giving you single record you need to create loop here. as i don`t know what are you using for PHP. 
$value['milestoneslist'] = $milestonesResult; //task_id=>array(here you can not use task_id => array as it is already there. if you do it it will over-right the task_id 
} 

我希望你能從中得到啓發。

0

您可以使用下面的代碼爲您的解決方案,因爲我以爲

$sql = "select table_b.*,table_a.name as aname, table_a.description as description as aname from table_b left join table_a on table_b.task_id=table_a.task_id" 
$result = mysql_query($sql); 

//based on above query result you will get array on mysql_fatch_assoc 

$result_array = array(); 
while($result = mysql_fatch_assoc($result)){ 
    $tmp_arr = array(); 
    $tmp_arr['mile_id'] = $result['mile_id']; 
    $tmp_arr['name'] = $result['name']; 

    $result_array[$result['task_id']]['name'] = $result['aname']; 
    $result_array[$result['task_id']]['description'] = $result['description']; 
    $result_array[$result['task_id']]['task_id'][] = $tmp_arr; 
} 


$result_array = array_values($result_array); 
print_r($result_array);