簡體   English   中英

生成具有遞歸數組的樹,該數組將兩個表中的數據組合在一起

[英]Generate a tree with recursive array combining data from two tables

我必須從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選擇pages ,然后在以下translations查找translationsWHERE module='pages' AND pk='4' AND locale='en'然后從頁面和翻譯后的文本值中獲取所需的所有信息。

我已經解釋了其翻譯系統的工作原理。 現在我在后端(和前端)遇到了問題,因為我需要用一種語言從此頁面構建一棵樹 我的想法是拋出一個遞歸數組,但我不知道如何合並數據,因為我認為這將是PHP,而不是MySQL。

我還沒有建立樹函數,我想我需要樹函數,因為:

  1. 我需要從MySQL查詢生成和數組,並結合頁面數據和翻譯。
  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的東西嗎?

先感謝您!

更新: Vincent請求的嵌套列表的示例

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>

我終於通過使用所需的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;
}

查詢更少-意味着加入:-)

想法-加入語言-這樣您每頁可獲得3行,然后將其處理為關聯的數組:-)(例如$ result [$ row [“ id”]] ['title'] = $ row ['title' ];)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM