改进PHP中的注释结构
我在PHP中有一个半工作的注释结构,我希望对其进行改进,以便无论回复的是什么注释,都能以正确的方式显示回复 下面我将展示的sort_comments函数是需要改进的 目前,我的评论数组结构采用以下格式:改进PHP中的注释结构,php,mysql,sorting,comments,Php,Mysql,Sorting,Comments,我在PHP中有一个半工作的注释结构,我希望对其进行改进,以便无论回复的是什么注释,都能以正确的方式显示回复 下面我将展示的sort_comments函数是需要改进的 目前,我的评论数组结构采用以下格式: Array ( [0] => Array ( [comment_id] => e465ce0a5301b8ed2eb66be06f768184f7727e3a [profile_id] => 8fa7a16
Array
(
[0] => Array
(
[comment_id] => e465ce0a5301b8ed2eb66be06f768184f7727e3a
[profile_id] => 8fa7a1679560876eaf2f8060abd916b692c719dc
[name] => Chris Moore
[parent_id] =>
[comment] => You can do that, easy peasy!
[type] => a
[ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
[registered] => 2013-11-19 14:34:41
[child] => Array
(
[0] => Array
(
[comment_id] => 68911c41a8cb13742dfd16f299aa3a2c9e87e16d
[profile_id] => 1dd36ac747735a3ee8a1d47750e1515ab7ac0d53
[name] => James Boyd
[parent_id] => e465ce0a5301b8ed2eb66be06f768184f7727e3a
[comment] => hello chris
[type] => a
[ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
[registered] => 2013-11-27 15:40:31
)
[1] => Array
(
[comment_id] => 7252cdab2c50dbb028e7b41f04bfb3fa7f6ff39d
[profile_id] => 8fa7a1679560876eaf2f8060abd916b692c719dc
[name] => Chris Moore
[parent_id] => e465ce0a5301b8ed2eb66be06f768184f7727e3a
[comment] => Test 14:17
[type] => a
[ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
[registered] => 2014-02-21 14:17:10
)
[2] => Array
(
[comment_id] => 3b221cd10ca36f7b723f19d6f53d87e42878280c
[profile_id] => 8fa7a1679560876eaf2f8060abd916b692c719dc
[name] => Chris Moore
[parent_id] => e465ce0a5301b8ed2eb66be06f768184f7727e3a
[comment] => Testing 14:23
[type] => a
[ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
[registered] => 2014-02-21 14:23:44
)
)
)
[1] => Array
(
[comment_id] => b94ce651fc010917e6b18fa6677c0941bacdcab0
[profile_id] => 8fa7a1679560876eaf2f8060abd916b692c719dc
[name] => Chris Moore
[parent_id] =>
[comment] => Testing
[type] => a
[ambition_id] => 85c39f39553d4a004778b8936fb5084daa77c80d
[registered] => 2014-02-20 16:11:38
)
)
如您所见,我使用了一个[child]数组,它附加了原始数组,请参见创建回复结构的数组索引1
我的职能:
function sort_comments($ar){
$comments = array();
foreach($ar as $item){
if(empty($item['parent_id'])){
$comments[] = $item;
}
else {
$parent_array = array_search($item['parent_id'],$comments);
//echo 'Search Array: '.$parent_array;
if(!is_numeric($parent_array)){
$comments[$parent_array]['child'][] = $item;
}
}
}
pA($comments);
return $comments;
}
function printComment($c){
$reg = commentDate($c['registered']);
$img = doesProfilePictureExistTwo($c['profile_id']);
?><div class="post-thumb">
<img src="<?=$img;?>" alt="comment_picture" />
</div>
<div class="post-content">
<p>
<a href="/x/view/<?=$c['profile_id'];?>/"><?=$c['name'];?></a>
<time datetime="<?=$c['registered'];?>" class="x_comment_time"><?=$reg;?></time>
</p>
<p><?=$c['comment'];?></p>
<ul class="x_comment_bottom_nav">
<li><a href="/x/view/comment/<?=$c['comment_id'];?>">permalink</a></li>
<li><a class="c_reply" href="#" data-id="<?=$c['comment_id'];?>">reply</a></li>
</ul>
<form id="<?=$c['comment_id'];?>" class="x_comments_form" method="post" action="/x/view/ambition/<?=$c['ambition_id'];?>/" style="display:none;">
<input type="hidden" name="action" value="x.add.ambition.comment"/>
<input type="hidden" name="x_parent_id" value="<?=$c['comment_id'];?>"/>
<textarea class="x_comment_reply_text_area" name="x_ambition_comment"></textarea>
<button class="btn">Add</button>
<button class="btn cancel_comment">Cancel</button>
</form>
</div><?
}
function parentComments($comments){
if(is_array($comments)){
foreach ($comments as $c){?>
<div class="post-container"><?
printComment($c);
childComments($c);
?></div>
<?}
}
}
function childComments($c){
if(array_key_exists("child",$c)){
foreach ($c['child'] as $child){?>
<div class="post-container-2">
<? printComment($child);
childComments($child); ?>
</div>
<?}
}
}
function getComments($db, $type, $t, $n, $id){
$query = " SELECT c.id AS comment_id, u.id AS profile_id, u.name AS name, c.parent_id, c.comment, '%s' AS type, '%s' AS ambition_id, c.registered
FROM %s c
LEFT JOIN x_user u
ON u.id = c.profile_id
WHERE c.%s = '%s'
ORDER BY c.registered
";
$query = sprintf($query, $type, $id, $t, $n, $id);
$result = $db->query($query) or die(mysql_error());
if($result){
$rows = $db->rows($result);
if($rows){
$comments = array();
for($i=0;$i<$rows;$i++){
$comments[] = $db->fetchArray($result);
}
$comments = sort_comments($comments);
return $comments;
}
}
}
函数排序\u注释($ar){
$comments=array();
foreach($ar作为$item){
if(空($item['parent_id'])){
$comments[]=$item;
}
否则{
$parent\u array=array\u search($item['parent\u id',$comments);
//回显“搜索数组:”。$parent_数组;
如果(!是数值($parent\u数组)){
$comments[$parent_array]['child'][]=$item;
}
}
}
pA(评论);
返回$comments;
}
函数printComment($c){
$reg=commentDate($c['registered']);
$img=doesProfilePictureExistTwo($c['profile_id']);
?>
“alt=”评论\图片“/>
这不是你的答案,但如果它能帮助你改变用回复打印评论的方法,那就值得一读了!:)我使用这种方法:在你的表中,有一个引用评论表id本身的评论id!这样你就会知道它是否有回复!然后在你的打印评论方法中,你首先检查这个特定的评论是否有回复回复与否!如果有,则使用参数(Reply=true)再次调用Print\u comment方法,请确保检查它是否为回复,然后应用您想要回复的内容!您是否可以向我展示任何类型的实现?我知道我的回复是否为回复,如果它们在parent_id字段中有id,则表示它们已回复了注释hmmm是的!当然!如果您想查看它的实际操作,您可能需要检查此URL: