我两次使用相同的代码,如何使其更易于仅使用一次?

时间:2018-10-23 07:29:35

标签: php mysql

我有一个通知系统。如果有人对您评论的主题发表新评论,则会收到通知。 问题是代码看起来很糟糕。首先,我计算一下您有多少个通知,因此我可以将其写到“通知铃”中,然后再次编写相同的代码,但是这次要输出结果(xy在x小时前发表了评论……)

这是我的代码(嗯,我的查询也很糟糕,因为它是根据我的上次评论日期而不是上次有人评论的时间排序的,因此通知区域的时间顺序也很糟糕(可能会发生, 1天前的评论是第一条通知,当它在1小时前的评论后面时)):

$comment_query = sql_query($conn, "SELECT p1.* FROM comment p1 INNER JOIN (SELECT max(date) MaxPostDate, user_id FROM comment WHERE user_id='$me' and deleted=0 GROUP BY topic_id, picture_id, news_id) p2 ON p1.user_id = p2.user_id AND p1.date = p2.MaxPostDate WHERE p1.user_id='$me' and deleted=0 ORDER BY p1.date DESC ");
if(sql_num($comment_query)!=0)
{
    while ($comment = sql_fetch($comment_query))
    {
        if($comment['topic_id']!=0)
        {
            $temp = sql_fetch(sql_query($conn, "SELECT class, url, name  FROM forum WHERE id='".$comment['topic_id']."' and deleted=0"));
            $temp2 = sql_fetch(sql_query($conn, "SELECT count(id) as count, date  FROM comment WHERE deleted=0 and topic_id='".$comment['topic_id']."'"));
            $comment_topic_id = $comment['topic_id'];
            $comment_id = $comment['id'];
            $comment2_query = sql_fetch(sql_query($conn,"SELECT count(id) AS cid FROM comment where topic_id=".$comment_topic_id ." and id<".$comment_id ." and deleted=0 "));
            $result = $comment2_query['cid'] + 1; //All the comments, until my comment
            if($comment['seen']=='0000-00-00 00:00:00') { //haven't yet seen it
                $unread = $temp2[0] - $result; //Unread comment count
                if($unread!=0)
                {
                    if((!empty($_GET['p'])) and $_GET['p']=='forum' and $_GET['c']==$temp['class'] and $_GET['x']==$temp['url']) //If I'm at the specific url (I'm watching the new comments, so update it)
                    {
                        $now = date('Y-m-d H:i:s');
                        sql_query($conn,"UPDATE comment SET seen='$now' WHERE user_id='$me' AND id='$comment_id' AND topic_id='.$comment_topic_id.' ");
                    }
                    else //increase number to add it to noficiation bell
                    {
                        $count++;
                        $forum_notif++;
                    }
                }
            }
            else
            {
                $last_time_seen = $comment['seen'];
                $count_comments = sql_fetch(sql_query($conn,"SELECT count(id) AS cid FROM comment where topic_id=".$comment_topic_id." and deleted=0 and date>'.$last_time_seen.' "));
                if($count_comments['cid']!=0) //If "seen" date is not 0000-00-00 00:00:00 then check if there are newer comments, since the current $comment['seen'] date
                {
                    if((!empty($_GET['p'])) and $_GET['p']=='forum' and $_GET['c']==$temp['class'] and $_GET['x']==$temp['url'])
                    {

                        $now = date('Y-m-d H:i:s');
                        sql_query($conn,"UPDATE comment SET seen='$now' WHERE user_id='$me' AND id='$comment_id' AND topic_id='.$comment_topic_id.' ");
                    }
                    else
                    {
                        $count++;
                        $forum_notif++;
                    }
                }
            }
        }
        elseif($comment['picture_id']!=0)
        {
           //same thing again for a different type of forum...

这里是输出“ x小时前评论过的xy ...”的部分:

$comment_query = sql_query($conn, "SELECT p1.* FROM comment p1 INNER JOIN (SELECT max(date) MaxPostDate, user_id FROM comment WHERE user_id='$me' and deleted=0 GROUP BY topic_id, picture_id, news_id) p2 ON p1.user_id = p2.user_id AND p1.date = p2.MaxPostDate WHERE p1.user_id='$me' and deleted=0 ORDER BY p1.date DESC ");
if(sql_num($comment_query)!=0)
{
    while ($comment = sql_fetch($comment_query))
    {
        if($comment['topic_id']!=0)
        {
            $temp = sql_fetch(sql_query($conn, "SELECT class, url, name  FROM forum WHERE id='".$comment['topic_id']."' and deleted=0"));
            $temp2 = sql_fetch(sql_query($conn, "SELECT count(id) as count, date  FROM comment WHERE deleted=0 and topic_id='".$comment['topic_id']."'"));
            $comment_topic_id = $comment['topic_id'];
            $comment_id = $comment['id'];
            $comment2_query = sql_fetch(sql_query($conn,"SELECT count(id) AS cid FROM comment where topic_id=".$comment_topic_id ." and id<".$comment_id ." and deleted=0 "));
            $result = $comment2_query['cid'] + 1; //All the comments, until my comment
            $get_date = sql_fetch(sql_query($conn,"SELECT date FROM comment WHERE topic_id=".$comment_topic_id." ORDER BY id DESC"));
            if($comment['seen']=='0000-00-00 00:00:00') {
                $unread = $temp2[0] - $result;
                if($unread!=0)
                {
                    $new_number = $temp2[0] - ($unread-1);
                    if($temp2[0]<=20) //20 comment appears on a page
                    {
                        ?>
                            <p class="notif"><a class="comments" href="/forum/<?php print $temp['class']; ?>/<?php print $temp['url']; ?>#<?php print $new_number; ?>"><?php print $unread; ?> new comments at <?php print ''.$temp['name'].' forum topic!<span class="when_notif">'.since_time($get_date['date']).'</span></a></p>'; //x hours ago
                            }
                            else
                            {
                                $limitation = 20;
                                $maxpage_comment = ceil($new_number / $limitation);//get page number
                                ?>
                                <p class="notif"><a class="comments" href="/forum/<?php print $temp['class']; ?>/<?php print $temp['url']; ?>/<?php print $maxpage_comment; ?>#<?php print $new_number; ?>"><?php print $unread; ?> new comments at <?php print ''.$temp['name'].' forum topic!<span class="when_notif">'.since_time($get_date['date']).'</span></a></p>';
                            }
                        }
                    }
                    else
                    {
                        $last_time_seen = $comment['seen'];
                        $count_comments = sql_fetch(sql_query($conn,"SELECT count(id) AS cid FROM comment where topic_id=".$comment_topic_id." and deleted=0 and date>'.$last_time_seen.' "));
                        if($count_comments['cid']!=0)
                        {
                            $new_number = $temp2[0] - ($count_comments['cid']-1);
                            if($temp2[0]<=20)
                            {
                                ?>
                                <p class="notif"><a class="comments" href="/forum/<?php print $temp['class']; ?>/<?php print $temp['url']; ?>#<?php print $new_number; ?>"><?php print $count_comments['cid']; ?> new comments at <?php print ''.$temp['name'].' forum topic!<span class="when_notif">'.since_time($get_date['date']).'</span></a></p>';
                            }
                            else
                            {
                                $limitation = 20;
                                $maxpage_comment = ceil($new_number / $limitation);
                                ?>
                                <p class="notif"><a class="comments" href="/forum/<?php print $temp['class']; ?>/<?php print $temp['url']; ?>/<?php print $maxpage_comment; ?>#<?php print $new_number; ?>"><?php print $count_comments['cid']; ?> newcomments at <?php print ''.$temp['name'].' forum topic!<span class="when_notif">'.since_time($get_date['date']).'</span></a></p>';
                            }
                        }
                    }
        elseif($comment['picture_id']!=0)
        {
           //same thing again for a different type of forum...

1 个答案:

答案 0 :(得分:1)

您可以练习面向对象的编程,也就是OOP。这样,您可以将代码存储在方法中。当您想使用代码时,只需调用该方法即可,因为它不重复代码,而是调用代码,因此更加简便明了。尝试阅读http://php.net/manual/en/language.oop5.php

相关问题