在 Codeigniter、Mysql 中使用带有 JOIN 的嵌套查询

发布于 2025-01-08 19:05:09 字数 1079 浏览 1 评论 0原文

我的 codeigniter 项目中有两个数据库表。作为简化的总结,

page                                        page_lang
-------------------------------------       ----------------------------
id_page | id_menu | id_parent | level       id_page |  title     | etc..
-------------------------------------       ----------------------------
   1    |    1    |     0     |   0            1    | Name 1     | etc..
   2    |    1    |     1     |   1            2    | Name 1.1   | etc..
   3    |    1    |     2     |   2            3    | Name 1.1.1 | etc..
   4    |    1    |     2     |   1            4    | Name 1.2   | etc.

我试图创建一个下拉选择框,其中包含所有嵌套有缩进的页面作为输出;

<option value="id_page">Name 1</option>
<option value="id_page">&nbsp;»&nbsp;Name 1.1</option>
<option value="id_page">&nbsp;&nbsp;&nbsp;-&nbsp;Name 1.1.1</option>
<option value="id_page">&nbsp;»&nbsp;Name 1.2</option>

在这种情况下,我想需要加入 page 和 page_lang 并创建一个递归循环。

但我致力于设计尽可能最快的代码。感谢您的帮助。

i have two db tables in my codeigniter project. As simplified summary,

page                                        page_lang
-------------------------------------       ----------------------------
id_page | id_menu | id_parent | level       id_page |  title     | etc..
-------------------------------------       ----------------------------
   1    |    1    |     0     |   0            1    | Name 1     | etc..
   2    |    1    |     1     |   1            2    | Name 1.1   | etc..
   3    |    1    |     2     |   2            3    | Name 1.1.1 | etc..
   4    |    1    |     2     |   1            4    | Name 1.2   | etc.

I am trying to create a dropdown select box which contains all page nested with indents as output like;

<option value="id_page">Name 1</option>
<option value="id_page"> » Name 1.1</option>
<option value="id_page">   - Name 1.1.1</option>
<option value="id_page"> » Name 1.2</option>

In this case, in need join page and page_lang and create a recursive loop, i quess.

But I am stacked on designing the fastest possible code. Thank you for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白色秋天 2025-01-15 19:05:09

您的递归函数将如下所示

function recursivePageOptions( $level, $nodes ) {
  $set = array();
  foreach ($nodes as $node) {
    $nest = '';
    for($x=1; $x<=$level; $x++)
      $nest.= '  ';
    $page = '<option value="'.$node['page']['id'].'">';
    $page.= $nest . $node['page']['title'] . '</option>';
    $set[] = $page;
    if (isset($node['children'])) {
      $set = array_merge(
        $set,
        recursivePageOptions( $level+1, $node['children'] );
      );
    }
  }
  return $set;
}

因此,在调用此递归函数之前,您需要做的就是将页面信息放入如下所示的数组结构中:

[
  'My Homepage' => [
    'page' => ['My Homepage', 24, ... ],
    'children' => [
      'Level 1 Page' => [
        'page' => ['Level 1 Page', 39, ... ],
        'children' => [
          'Level 2 Page' => [
            'page' = ['Level 2 Page', 51, ... ]
          ]
        ]
      ],
      'Another Level 1 Page' =< [
        'page' => ['Another Level 1 Page', 56, ... ]
      ]
    ]
  ]
]

由您来详细了解这部分,本质上您将从数据库中获取行并循环遍历它们,以生成类似于上面的数组结构。

Your recursive function will look something like this

function recursivePageOptions( $level, $nodes ) {
  $set = array();
  foreach ($nodes as $node) {
    $nest = '';
    for($x=1; $x<=$level; $x++)
      $nest.= '  ';
    $page = '<option value="'.$node['page']['id'].'">';
    $page.= $nest . $node['page']['title'] . '</option>';
    $set[] = $page;
    if (isset($node['children'])) {
      $set = array_merge(
        $set,
        recursivePageOptions( $level+1, $node['children'] );
      );
    }
  }
  return $set;
}

So what you need to do before this recursive function is called is get your page information into an array structure that looks like this:

[
  'My Homepage' => [
    'page' => ['My Homepage', 24, ... ],
    'children' => [
      'Level 1 Page' => [
        'page' => ['Level 1 Page', 39, ... ],
        'children' => [
          'Level 2 Page' => [
            'page' = ['Level 2 Page', 51, ... ]
          ]
        ]
      ],
      'Another Level 1 Page' =< [
        'page' => ['Another Level 1 Page', 56, ... ]
      ]
    ]
  ]
]

Its up to you to figure this part out in detail, essentially you will be getting rows out of the database and looping through them in such a way as to generate an array structure like the one above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文