栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

PHP和MySQL中的嵌套注释

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

PHP和MySQL中的嵌套注释

我在博客文章中做了类似的事情。但是,我只是尝试了相同的数据。当您说 嵌套注释时 ,最好以这种方式使用 嵌套函数

function getComments($parent){    # Get the data from SQL.    # Check if it has nested comments.    if (hasComments())        getComments($child); # $child is the ID of the current comment.}

为此,我已将您的数据导入MySQL并尝试了以下操作:

<?php    mysql_connect('localhost', 'root');    mysql_select_db('nestedcomments');    function getComments($parent)    {        $res = mysql_query("SELECT * FROM `nestcomm` WHERe `parent` = $parent");        if (mysql_num_rows($res))        { echo "<ul>n"; while (($dat = mysql_fetch_array($res)) !== false)     echo "<li>", $dat["text"], getComments($dat["id"]), "</li>n"; echo "</ul>n";        }        else echo ($parent === 0) ? 'No Comments!' : "";    }    getComments(0);?>

就像我之前说过的那样,我使用了嵌套函数,并且正如您所问的那样,这种输出几乎是相同的(不带花括号):

<ul><li>This is the parent first comment!<ul><li>This is the reply for the first parent comment!<ul><li>This is a reply for the first reply of the parent comment!</li><li>This is a third reply for the first parent comment!</li></ul></li><li>This is another reply for first parent comment!</li></ul></li><li>This is gonna be parent second comment!<ul><li>This is a reply for the second comment!</li></ul></li><li>This is fourth parent comment!</li></ul>

希望这会有所帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/426250.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号