我必須從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的。
我還沒有建立樹的功能,我想我需要樹的功能,這是因爲:
- 我需要生成並從MySQL查詢/ IES陣列,結合網頁數據和翻譯。
- 我需要使用嵌套樹在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>
Ummmm ....什麼? – 2011-01-19 14:57:32