2013-03-18 32 views
3

我有一個工作區及其父標識表。每個父母可以有任意數量的等級。從多維PHP數組中將父節點的值串起來

id workplace    parent 
1  WCHN     0 
2  Acute Services   1 
3  Paediatric Medicine 2 
4  Surgical Services  2 
5  Nursing and Midwifery 1 
6  Casual Pool   5 
7  Clinical Practice  5 

我需要創建一個單一的選擇輸入,列出了他們所有的父母工作場所的工作場所,有點像這樣:

<select> 
    <option>WCHN > Acute Services > Paediatric Medicine</option> 
    <option>WCHN > Acute Services > Surgical Services</option> 
    <option>WCHN > Nursing and Midwifery > Casual Pool</option> 
    <option>WCHN > Nursing and Midwifery > Clinical Practice</option> 
</select> 

通過修改this solution我已經能夠把我的簡單列表轉換成多維數組並輸出值對,但沒有完整的路徑。

<?php 
public function list_all_workplaces() 
{ 
    $temp = $result = array(); 
    $list = array(); 

    // Get the data from the DB 
    $table = mysql_query("SELECT * FROM workplaces"); 

    // Put it into one dimensional array with the row id as the index 
    while ($row = mysql_fetch_assoc($table)) { 
     $temp[$row['workplace_id']] = $row; 
    } 

    // Loop the 1D array and create the multi-dimensional array 
    for ($i = 1; isset($temp[$i]); $i++) 
    { 
     if ($temp[$i]['parent'] > 0) 
     { 
      $tmpstring = ($temp[$i]['workplace']); // workplace title 

      // This row has a parent 
      if (isset($temp[$temp[$i]['parent']])) { 

       $list[$i] = $temp[$temp[$i]['parent']]['workplace']." > ".$tmpstring; 
       //example output: Acute Services > Paediatric Medicine 

       // The parent row exists, add this row to the 'children' key of the parent 
       $temp[$temp[$i]['parent']]['children'][] =& $temp[$i]; 

      } else { 
       // The parent row doesn't exist - handle that case here 
       // For the purposes of this example, we'll treat it as a root node 
       $result[] =& $temp[$i]; 
      } 
     } else { 
      // This row is a root node 
      $result[] =& $temp[$i]; 
     } 

    } 
    // unset the 1D array 
    unset($temp); 

    //Here is the result 
    print_r($result); 
} 

例子的print_r()輸出:

[1] => WCHN > Acute Services 
[2] => Acute Services > Paediatric Medicine 

我在哪裏何去何從讓所有的家長工作場所到選擇的選擇嗎?

回答

1

我看不到你的代碼是如何產生print_r輸出你說你看到的,因爲你永遠不使用您創建的字符串。但是沿着這些路線的東西應該讓你走上前路 - 它會產生你需要的字符串。

用以下替換您for循環:

foreach ($temp as $i => $details) 
{ 
    $parentID = $details['parent']; 
    $tmpstring = ($details['workplace']); 
    if ($parentID > 0 && isset($temp[$parentID])) 
    { 
     $temp[$parentID]['children'][] =& $temp[$i]; 
     while ($parentID > 0 && isset($temp[$parentID])) 
     { 
      $tmpstring = $temp[$parentID]['workplace']." > ".$tmpstring; 
      $parentID = $temp[$parentID]['parent']; 
     } 
    } 
    $result[] = $tmpstring; 
} 

正如@Syx說,你需要從你的函數返回的東西太多,大概$result,然後用它來生成你<select>

+0

感謝馬克,這是完美的(我會更新我的問題,包括print_r($ result)調試我錯過了我的副本:) – Zoe 2013-03-18 05:00:55

1

我不明白你如何使用你的函數,如果沒有返回值。

如果您的數組構建正確,並且您希望它的方式以及詢問如何將結果導入html選擇字段,那麼這就是最簡單的部分。

讓我們快速回到你的功能 。 您的函數中沒有列出返回$ result的結果,因此調用時不會返回任何內容。

您需要將其添加到要啓動的函數的末尾。

然後,您將循環訪問數組以開始處理html。

$data = list_all_workplaces() 
foreach($data as $key => $value) 
{ 
    $options = "<option value='{$key}'>{$value}</option>"; 
} 
echo "<select>{$options}</select>";