Reputation: 114
I have a notification system. If someone posts a new comment to the topic you commented, then you get a notification. The problem is, that the code is looking awful. First I count how many notifications you have, so I can write it into the "notification bell", then I write the same code again, but this time to output the result (xy commented x hours ago...)
So here's my code (well, my queries are bad too, because it's sorted by last comment date from me, instead of the last time someone commented, so the notifications area time order is also bad (it can happen, that a comment from 1 day ago is the first notification, when a comment from 1 hour ago is behind it)):
$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...
Here's the one for outputting the "xy commented x hours ago..." part:
$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...
Upvotes: 0
Views: 155
Reputation: 208
You can practice object oriented programming or in other words, OOP. In that way you can store the code in methods. When you want to use the code, just call the method, it's easier and cleaner because you don't repeat the code, but you just call the code. Try reading http://php.net/manual/en/language.oop5.php.
Upvotes: 1