2011-01-19 29 views
0

我必須從PHP中的兩個MySQL表生成一個樹,並用HTML打印它。我的主要問題是我有兩張桌子在哪裏獲取數據。一個表是和其他翻譯因爲我有3種語言的網站和所有翻譯仍然在翻譯表中。使用遞歸數組合並數據生成一棵樹

此表的模式是:

id INT 
id_user INT 
id_parent INT 
order INT 
template INT 
image VARCHAR 
flag_active INT 
flag_extra INT 
edited INT 
created INT 

翻譯

id INT 
id_user INT 
locale VARCHAR 
module VARCHAR 
pk INT 
label VARCHAR 
value TEXT 
edited INT 

當我添加頁面我的值的頁面title, slug and text(不上模式,因爲這取決於語言環境)到翻譯中:

INSERT INTO pages (id_user, template, image, flag_active, flag_extra, edited, created) 
VALUES (1, 0, 'test.jpg', 1, 1, 12345, 12345) 

<!-- the pages.id is 4, for example, for next insert: --> 

INSERT INTO translations (id_user, locale, module, pk, label, value, edited) 
VALUES (1, 'en', 'pages', 4, 'title', 'This is the title in English', 12345) 

INSERT INTO translations (id_user, locale, module, pk, label, value, edited) 
VALUES (1, 'en', 'pages', 4, 'slug', 'this-is-the-title-in-english', 12345) 

INSERT INTO translations (id_user, locale, module, pk, label, value, edited) 
VALUES (1, 'en', 'pages', 4, 'text', 'This is the text in English', 12345) 

INSERT INTO translations (id_user, locale, module, pk, label, value, edited) 
VALUES (1, 'es', 'pages', 4, 'title', 'Este es el titulo en Español', 12345) 

INSERT INTO translations (id_user, locale, module, pk, label, value, edited) 
VALUES (1, 'es', 'pages', 4, 'slug', 'este-es-el-titulo-en-espanol', 12345) 

INSERT INTO translations (id_user, locale, module, pk, label, value, edited) 
VALUES (1, 'es', 'pages', 4, 'text', 'Este es el contenido en Español', 12345) 

然後,當我訪問一個頁面在一定的語言,PHP中的第一個我從表中pages選擇頁面,然後看在translations爲:WHERE module='pages' AND pk='4' AND locale='en'和我得到我所需要的所有信息從頁面和翻譯的文本值。

我已經解釋了它是如何工作的,我的翻譯系統。現在我在後端(和前端)出現問題,因爲我需要使用一種語言在此頁面上構建一棵樹。我的想法是拋出一個遞歸數組,但我不知道如何合併數據,因爲我認爲這將是一個PHP的事情,而不是MySQL的。

我還沒有建立樹的功能,我想我需要樹的功能,這是因爲:

  1. 我需要生成並從MySQL查詢/ IES陣列,結合網頁數據和翻譯。
  2. 我需要使用嵌套樹在HTML中生成<ol />列表。

對於第一個樹型數組,MySQL中的一個,我需要爲每個條目創建一個MySQL?或者這可以只用一個MySQL查詢來完成?代碼示例,未經測試直接寫入:

function mysql_tree($parent_id = 0) 
{ 
    $return = array(); 

    $query = "SELECT * FROM pages WHERE id_parent=$parent_id"; 

    $result = mysql_query($query); 

    if(mysql_num_rows($result) != 0) 
    { 
     $i = 0; 

     while($row = mysql_fetch_array($result)) 
     { 
      $return[$i] = array(
       'id' => $row["id"]; 
       //etc 
      ); 

      // Time to merge the data for each page from translations???? 

      $return[$i]["childs"] = mysql_tree($row["id"]); 

      $i++; 
     } 
    } 

    return $return; 
} 

對於第二個樹函數,我猜想類似於MySQL的東西?

預先感謝您!

UPDATE:文森特

Array(
    [0] => Array(
     [id] => 4 
     [id_user] => 1 
     [id_parent] => 0 
     [order] => 0 
     [template] => 1 
     [image] => NULL 
     [flag_active] => 1 
     [flag_extra] => 0 
     [edited] => 12345 
     [created] => 12345 
     [title] => This is the title in English 
     [slug] => this-is-the-slug-in-english 
     [childs] => Array(
      [0] => Array(
       [id] => 5 
       [id_user] => 1 
       [id_parent] => 4 
       [order] => 0 
       [template] => 1 
       [image] => NULL 
       [flag_active] => 1 
       [flag_extra] => 0 
       [edited] => 12345 
       [created] => 12345 
       [title] => This is the title in English 2 
       [slug] => this-is-the-slug-in-english-2 
       [childs] => NULL 
      ) 
     ) 
    ) 
) 

和HTML樹請求嵌套列表的例如:

<ol> 
    <li class="id_4"><a href="/pages/this-is-the-slug-in-english">This is the title in English</a> 
     <ol> 
      <li class="id_5"><a href="/pages/this-is-the-slug-in-english-2">This is the title in English 2</a></li> 
     </ol> 
    </li> 
</ol> 
+0

Ummmm ....什麼? – 2011-01-19 14:57:32

回答

1

我終於通過使用所需的MySQL查詢來解決這個問題。我發佈了代碼,因爲可以對其他用戶有用。我使用的是CodeIgniter,但它可以和普通的mysql_query()一起工作。

PS:請注意,我使用西班牙名稱,順便說一句,這很容易理解。

public function tree($id_padre = 0, $locale = null) 
{ 
    $return = array(); 

    if(false == is_numeric($id_padre)) 
     return $return; 

    if(null == $locale) 
     return $return; 

    $query = $this->db->where("id_padre", $id_padre)->get("paginas"); 

    if($query->num_rows() > 0) 
    { 
     $i = 0; 

     foreach($query->result_array() as $row) 
     { 
      $translation = (array) $this->get($row["id"], $locale); // This returns the "title", "slug" and "text" from the translations tables 

      $data["id"] = $row["id"]; 
      $data["id_padre"] = $row["id_padre"]; 
      $data["orden"] = $row["orden"]; 

      $data = array_merge($data, $translation); 

      $data["childs"] = $this->tree($row["id"], $locale); 

      $return[$i] = $data; 

      unset($data); 

      $i++; 
     } 

     return $return; 

    } 
    else { 
     return NULL; 
    } 
} 

和HTML樹:

function backend_tree($array = null, $class = null) 
{ 
    if(null == $array) 
     return false; 

    $return = '<ol'; 

    if(null != $class) 
     $return .= ' class="'.$class.'"'; 

    $return .= '>'; 

    foreach($array as $item) 
    { 
     $return .= '<li id="id-'.$item["id"].'"><div>'.$item["titulo"]; 
     $return .= '<div class="edit">'.anchor("/admin/carta/edit/".$item["id"], " ", array("class" => "sortableEdit")).' '.anchor("/admin/carta/delete/".$item["id"], " ", array("class" => "sortableDelete")).'</div>'; 
     $return .= '</div>'; 
     $return .= backend_tree($item["childs"]); 
     $return .= '</li>'; 
    } 

    $return .= '</ol>'; 

    return $return; 
} 
0

減查詢 - 意味着JOIN :-)

理念 - 加入吧與語言 - 所以你得到每行3行,並將其處理到相關數組: - )(例如。$ result [$ row [「id」]] ['title'] = $ row ['title'];)