在父子表单无序列表中打印分层数据php?
我在父子层次结构中的 mysql 表中有数据,例如;
|---------+-----------+-------------|
| msg_id | parent_id | msg |
|---------+-----------+-------------|
| 1 | NULL | msg1 |
| 2 | NULL | msg2 |
| 3 | NULL | msg3 |
| 4 | 1 | msg1_child1 |
| 5 | 1 | msg1_child2 |
| 6 | 3 | msg3_child1 |
|---------+-----------+-------------|
我需要以父子无序列表格式显示它,例如
-msg1
-msg1-child1
-msg2-child2
-msg2
-msg3
-msg3-child1
我该怎么做?我需要帮助,特别是如何在表单的层次结构中显示它。
I have data in mysql table in a parent child hierarchy like;
|---------+-----------+-------------|
| msg_id | parent_id | msg |
|---------+-----------+-------------|
| 1 | NULL | msg1 |
| 2 | NULL | msg2 |
| 3 | NULL | msg3 |
| 4 | 1 | msg1_child1 |
| 5 | 1 | msg1_child2 |
| 6 | 3 | msg3_child1 |
|---------+-----------+-------------|
I need to display it in a parent-child unordered list format like
-msg1
-msg1-child1
-msg2-child2
-msg2
-msg3
-msg3-child1
How do I do it? I need help especially how could I display it in a hierarchy on a form.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,从后端到前端工作...
您可以从 php 脚本调用单个非递归存储过程 (sproc),它会为您生成消息层次结构。这种方法的优点是你只需要从 php 到数据库进行SINGLE调用,而如果你使用内联 SQL,那么你将进行与级别一样多的调用(至少) 。另一个优点是,由于它是一个非递归存储过程,因此性能非常好,并且还可以使您的 php 代码保持美观和干净。最后,我必须郑重声明,调用存储过程比任何其他方法更安全、更高效,因为您只需向应用程序用户授予执行权限,并且存储过程需要的数据库往返次数比任何其他方法都要少。其他方法包括参数化查询,单个查询需要至少 2 次调用(其中 1 个用于在数据库中设置查询模板,另一个用于填充参数)
因此,以下是从 MySQL 命令行调用存储过程的方法。
这是它创建的结果集。
好的,现在我们有能力通过简单地使用我们需要的任何起始节点调用我们的存储过程来获取完整或部分消息树,但是我们将如何处理结果集?
在这个例子中,我决定用它生成一个 XML DOM,然后我需要做的就是转换 (XSLT) XML,我们将拥有一个嵌套消息网页。
PHP 脚本
PHP 脚本相当简单,它只是连接到数据库、调用存储过程并循环结果集来构建 XML DOM。请记住,我们只调用数据库一次。
XML 输出
这是 php 脚本生成的 XML。如果您将此 XML 保存在文件中并在浏览器中打开它,您将能够展开和折叠级别。
现在,如果您愿意,您可以放弃构建 XML DOM 并使用 XSL 呈现网页,也许只是循环结果集并直接呈现消息。我选择这种方法只是为了使我的示例尽可能全面且信息丰富。
MySQL脚本
这是一个完整的脚本,包括表、存储过程和测试数据。
该答案的完整来源可以在这里找到:http://pastie.org/1336407。正如您已经注意到的,我省略了 XSLT,但您可能不会采用 XML 路线,如果您这样做,网络上也会有大量示例。
希望这对您有所帮助:)
编辑:
添加了更多数据,以便您拥有多个根消息 (msg_ids 1,9,14)。
现在,如果您只想获取特定于根节点的消息(起始消息),您可以调用原始存储过程,传入所需根节点的起始 msg_id。使用上面的新数据将是 msg_ids 1,9,14。
您可以传入您喜欢的任何 msg_id,因此如果我想要 msg 1-2-2-1 下面的所有消息,那么您将传入 msg_id = 6:
但是,如果您想要所有根的所有消息,那么您可以调用我创建的这个新存储过程,如下所示:
主要问题是,随着消息表的增长,它将返回大量数据,这就是为什么我专注于一个更具体的存储过程,它只为给定的根获取消息节点或起始 msg_id。
我不会发布新的存储过程代码,因为它实际上与原始代码相同,但您可以在这里找到所有修改:http:// Pastie.org/1339618
您需要进行的最后更改是在 php 脚本中,该脚本现在将调用新的存储过程,如下所示:
希望这会有所帮助:)
OK working from the backend towards the front-end...
You could call a single non recursive stored procedure (sproc) from your php script which generates the message hierarchy for you. The advantage of this approach is you only need to make a SINGLE call from php to your database whereas if you use inline SQL then you'll be making as many calls as there are levels (at a minimum). Another advatange is that as it's a non recursive sproc it's extremely performant and it also keeps your php code nice and clean. Finally, and I have to say this for the record, that calling stored procedures is more secure and more efficient than any other method because you only need to GRANT execute permissions to your app user and stored procedures require less round trips to the database than any other methods including parameterised queries which require at least 2 calls for a single query (1 to setup the query template in the db, the other to populate the params)
So here's how you'd call the stored procedure from the MySQL command line.
and here's the resultset it creates.
Ok, so now we have a the ability to fetch a full or partial message tree by simply calling our sproc with whatever starting node we require but what are we going to do with the resultset ??
Well in this example I've decided we're going to generate an XML DOM with it, then all I need to do is transform (XSLT) the XML and we'll have a nested messages web page.
PHP script
The php script is fairly simple, it just connects to the database, calls the sproc and loops the resultset to build the XML DOM. Remember we're only calling into the db once.
XML output
This is the XML that the php script generates. If you save this XML in a file and open it in your browser you'll be able to expand and collapse the levels.
Now you could forego building the XML DOM and using XSL to render a web page if you wish and perhaps just loop the resultset and render the messages directly. I've simply chosen this method to make my example as comprehensive and informative as possible.
MySQL script
This is a complete script including tables, sprocs and test data.
The full source for this answer can be found here : http://pastie.org/1336407. As you'll have noted already I've omitted the XSLT but you probably wont go the XML route and if you do there are heaps of examples on the web.
Hope you find this helpful :)
EDIT:
Added a little more data so you have more than one root message (msg_ids 1,9,14).
Now if you want to just get the messages that are specific to a root node (starting message) you can call the original stored procedure passing in the starting msg_id of the root you require. Using the new data above that would be msg_ids 1,9,14.
you can pass in any msg_id you like so if I want all of the messages below msg 1-2-2-1 then you would pass in msg_id = 6:
However, if you want all of the messages for all of the roots then you can call this new sproc I've created as follows:
The main problem with this is as your message table grows it's going to be returning lots of data which is why I was focusing on a more specific sproc that only fetched messages for a given root node or starting msg_id.
I wont post the new sproc code as it is virtually the same as the original but you can find all the amendments here : http://pastie.org/1339618
The final change you'll need to make is in the php script which will now call the new sproc as follows:
Hope this helps :)
或者,接近那个。
Or, close to that.