Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 线程化/嵌套式/层次化注释_Php_Sql - Fatal编程技术网

Php 线程化/嵌套式/层次化注释

Php 线程化/嵌套式/层次化注释,php,sql,Php,Sql,我很难解决一些问题。我刚刚在线使用了一个线程评论示例,对手动输入的数据进行了测试,效果非常好 但是这种设置有点奇怪。它使用路径方法,因此第一条注释的路径为01,对该注释的回复的路径为01_01,而对第一条注释的另一条回复的路径为01_02。您可以在我的屏幕截图(上图)中看到每个名称旁边的相对路径 这个方法执行得非常好,因为我已经针对很多很多评论对它进行了测试。我的问题是计算下一个回复路径。例如,假设我的用户在Jeremy Clarkson的评论上单击[reply],该评论的路径为01_01_0

我很难解决一些问题。我刚刚在线使用了一个线程评论示例,对手动输入的数据进行了测试,效果非常好

但是这种设置有点奇怪。它使用路径方法,因此第一条注释的路径为01,对该注释的回复的路径为01_01,而对第一条注释的另一条回复的路径为01_02。您可以在我的屏幕截图(上图)中看到每个名称旁边的相对路径

这个方法执行得非常好,因为我已经针对很多很多评论对它进行了测试。我的问题是计算下一个回复路径。例如,假设我的用户在Jeremy Clarkson的评论上单击[reply],该评论的路径为01_01_01。下一个序列是01_01_02,但Kim Bauer的评论已经使用了这个序列。我想我可以做一个小查询
从注释中选择*,路径像'01_01_%'
并选择最后一行并向其中添加1,但是Chloe O'Brien的注释有01_01_01_01,这会影响这个结果

有人能解释一下我如何计算下一条正确的回复路径吗

我的修复:

$SQL = "SELECT * FROM comments ORDER BY path ASC;";

while($row = $STH->fetch()) {
    $nesting_depth = strlen($row['path']) - strlen(str_replace('_','',$row['path']));
    $left_margin = $nesting_depth * 40; // 40 pixels nesting indent

    echo '<div class="comment_item" style="margin-left:'.$left_margin.'px;">';
        echo '<strong>'.htmlspecialchars($row['author_name']).'<br>';
        echo htmlspecialchars($row['comment']).'<br>';
    echo '</div>';
}
我通过以下操作计算下一个可用路径:

SELECT path
FROM blog_comments
WHERE path LIKE '01_01___'
ORDER BY path DESC
LIMIT 1

$last_path = $row[0];
$last_path_suffix = substr($last_path,strrpos($last_path,'_')+1);
$next_path_suffix = str_pad($last_path_suffix+1,2,'0',STR_PAD_LEFT);
$next_path = substr($last_path,0,strlen($last_path)-strlen($last_path_suffix)).$next_path_suffix;
如果有人想使用这种方法,打印方式如下:

$SQL = "SELECT * FROM comments ORDER BY path ASC;";

while($row = $STH->fetch()) {
    $nesting_depth = strlen($row['path']) - strlen(str_replace('_','',$row['path']));
    $left_margin = $nesting_depth * 40; // 40 pixels nesting indent

    echo '<div class="comment_item" style="margin-left:'.$left_margin.'px;">';
        echo '<strong>'.htmlspecialchars($row['author_name']).'<br>';
        echo htmlspecialchars($row['comment']).'<br>';
    echo '</div>';
}
$SQL=“按路径ASC从注释顺序选择*”;
而($row=$STH->fetch()){
$nesting_depth=strlen($row['path'])-strlen(str_replace('''',''.$row['path']);
$left\u margin=$nesting\u depth*40;//40像素嵌套缩进
回声';
echo“”.htmlspecialchars($row['author_name'])。
; 回显htmlspecialchars($row['comment'])。
; 回声'; }
您可以使用两个下划线搜索两位数字。下划线是只匹配一个未知字符的通配符。而且您应该转义文字下划线,因为
是一个通配符

SELECT * FROM comments WHERE path LIKE '01\_01\___' ESCAPE '\'

我认为最好放弃路径的想法,选择一个简单的
id
parentid
解决方案。@PeeHaa:我再给这个解决方案两个小时的耐心,然后我可能会切换到
id>parent\u id
method:)在parentid旁边,你可能想了解一下所谓的嵌套集。每种方法都有优点和缺点,因此了解更多替代方法的不同方法通常是很好的。@ShadowStorm您在github的某个地方有这种方法的示例源代码吗?我很高兴您添加了上一次更新,解释了两个下划线的含义。我写信只是想问:)我会尝试一下,然后告诉你。谢谢。我做了一些轻微的调整。首先,ESCAPE不是一个MySQL函数(我的错误是没有指定它)。我也很难转义非通配符下划线,因此,我使用了LIKE
'01\u 01\u\u'
,现在看起来效果很好。