首页 > 解决方案 > 我使用相同的代码两次,我怎样才能使它更容易只使用一次?

问题描述

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

所以这是我的代码(好吧,我的查询也很糟糕,因为它是按我的最后评论日期排序的,而不是上次有人评论的时间,所以通知区域的时间顺序也很糟糕(可能会发生,来自 1 的评论) day ago 是第一个通知,当 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...

这是输出“xy x hours ago...”部分的输出:

$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...

标签: phpmysql

解决方案


您可以练习面向对象的编程,或者换句话说,OOP。通过这种方式,您可以将代码存储在方法中。当你想使用代码时,只需调用方法,更简单更干净,因为你不重复代码,而只是调用代码。尝试阅读http://php.net/manual/en/language.oop5.php


推荐阅读