如何存储我的菜单结构

发布于 2024-11-14 17:21:22 字数 380 浏览 1 评论 0原文

我正在创建一个网站,我希望动态生成菜单。影响菜单的因素包括页面在站点层次结构中的位置、查看页面的用户的访问级别以及页面的类型(新闻文章、事件详细信息、产品页面等)。

最初我想创建一个 mysql 数据库,其中包含带有外键的菜单项和每个项目的附加详细信息(菜单项的目标 url、父菜单项、项目标题等)。但我开始遇到项目排序、多层菜单和多个菜单的问题。

然后我想,当我可以拥有一个具有明确规定的结构和顺序的 xml 文件时,为什么还要使用平面关系数据库呢?它很少会改变,页面必须查询 MySQL 数据库才能获取其内容,它可以包含一些菜单 ID。

你能看出这种方法有什么缺陷吗?除了将 xml 用作生成其他结构的模板之外,我对 xml 没有太多经验。在 php 和 javascript 中操作有多容易?

I am creating a web site and I want the menus to be dynamically generated. Factors that can effect the menus are the page's position in the site hierarchy, the access level of the user looking at the page, and the type of page (news post, event details, product page, etc).

Originally I thought of creating a mysql database containing menu items with foreign keys and additional details to each item, (target url of menu item, parent menu item, title of item, etc). But I started running into issues of item ordering, multi layered menus and multiple menus.

Then I thought why use a flat relational database when I can have a xml file that has the structure and order explicitly stated. It rarely will change, the pages will have to query the MySQL database to get their contents, it can include some menu ids.

Can you see any pitfalls of this method? I don't have alot of experience with xml other than using it as a template that I walk to generate other structures. How easy is it to manipulate in php and javascript?

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

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

发布评论

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

评论(2

深海夜未眠 2024-11-21 17:21:22

如果菜单层次结构只创建一次并且很少更新,则可以将对象存储为您选择的序列化数组/对象。这里的优点是 serialize()/deserialize() 比解析 XML 快得多。为此,您必须编写代码来更新序列化对象/数组,但您也必须对 XML 执行此操作。它还会保留对 XML 代码的偏好并降低可移植性。缺点是在 PHP 之外不太便携,优点是速度非常快。

如果您想让它更便携,您可以应用 json_encode/json_decode 而不是序列化。 Con 此 json_encode 比序列化慢,pro 更易于移植到其他语言,例如 saayyy、Javascript。

在 MySQL 的辩护中,您可以在层次结构中存储/添加/删除元素,而无需使用 邻接列表,让事情变得更加紧凑。如果列表变得很大并且可以随时更改为许多不同的格式,那么您可能会有点头脑扭曲,但速度会更快。

If the menu hierarchy was to be created once and updated extremely rarely, you could store the object as a serialised array/object of your choosing. The advantage here is that serialize()/deserialize() is significantly faster than parsing XML. For this you would have to write code to update the serialized object/array, but you would have to do that for XML as well. It would also keep the preference for the code to be XML and reduce portability. Con for this is not too portable outside of PHP, and pro it's really fast

You could apply the json_encode/json_decode instead of serialize if you wanted to keep it a bit more portable. Con for this json_encode is slower than serialization, pro more portable to other languages, like saayyy, Javascript.

In MySQL's defence, you can store/add/remove elements to a hierarchy without the method you described using an adjacency list, keeping things a bit more compact. Con a bit of a mind warp to get your head around it, pro faster if the list gets big and eternally changeable into many different formats.

也只是曾经 2024-11-21 17:21:22

它将与 XML 一起使用,例如 SimpleXML。然而 XML 有点占用内存和 CPU。

最舒适的方法是,当您可以将孔菜单结构保留为嵌套数组时。那么也许您可以直接在本机 PHP 中定义您的结构?您可以创建一个文件:

<?php return array(
/* put your nested array structure here */
);

并在您的逻辑中调用此结构,如下所示:

<?php
$structure = include('structure.php');

这项技术不太为人所知,但确实轻量级且直接!

否则,您可以使用 parse_ini_file() 或保留 XML 或直接将其解析为数组。

当您返回数据库解决方案时,也许“嵌套集”可以帮助您一次(如果您喜欢复杂但聪明的东西;-))。

It will work with XML, for example with SimpleXML. However XML is a bit memory and CPU hungry.

The most comfortable way would be, when you can hold the hole menu structure as a nested array. So maybe you can define your structure in directly in native PHP? You can create one file:

<?php return array(
/* put your nested array structure here */
);

And call this structure in your logic like:

<?php
$structure = include('structure.php');

This technique is not so well known but really lightweight and straight forward!

Otherwise you can use parse_ini_file() or stay with the XML or parse it directly to an array.

When you go back to a database solutions maybe "nested sets" could help you once (if you like complicated but clever stuff ;-) ).

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