我必须从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