2011-01-19 81 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'];)