PDA

View Full Version : Number of views per announcement


Zzed
04-08-2002, 10:00 PM
I wasn't able to find this hack anywhere. So I wrote this.

This hack will show the number of views per announcement.

Tables affected: announcement
Templates affected: forumdisplay_announcement
Files affected: forumdisplay.php and announcement.php

First we need to add a new field to announcement:

alter table announcement add visits smallint(5) unsigned DEFAULT 0 NOT NULL;

Then we need to modify forumdisplay_announcement template:

Look for the following:

<smallfont><br>($announcement[usertitle])</smallfont></td>
<td bgcolor="#F1F1F1"><normalfont>-</normalfont></td>
<td bgcolor="#DFDFDF"><normalfont>-</normalfont></td>

And replace it with the following:

<smallfont><br>($announcement[usertitle])</smallfont></td>
<td bgcolor="#F1F1F1"><normalfont>-</normalfont></td>
<td bgcolor="#DFDFDF"><normalfont>$announcement[visits]</normalfont></td>

In forumdisplay.php:

Look for the following:

if ($announcement=$DB_site->query_first("SELECT announcementid,startdate,title,user.username,user. userid,user.usertitle,user.customtitle
FROM announcement
LEFT JOIN user
ON user.userid=announcement.userid
WHERE startdate<=$datenow AND enddate>=$datenow
AND $forumlist
ORDER BY startdate DESC
LIMIT 1")) {
if ($foruminfo[allowratings]) {
$thread[rating]='clear.gif';
eval("\$threadrating = \"".gettemplate('forumdisplaybit_threadrate')."\";");
$backcolor = '#DFDFDF';
$bgclass = "alt2";
} else {
unset($threadrating);
$backcolor = '#F1F1F1';
$bgclass = "alt1";
}

if ($announcement[customtitle]==2)
$announcement[usertitle] = htmlspecialchars($announcement[usertitle]);
$announcement[postdate]=vbdate($dateformat,$announcement[startdate]);

if ($announcement[startdate]>$bbuserinfo[lastvisit]) {
$announcement[icon]='newannounce.gif';
} else {
$announcement[icon]='announce.gif';
}
eval("\$announcement = \"".gettemplate('forumdisplay_announcement')."\";");
$doneannouncements = 1;
}

And replace it with the following:

if ($announcement=$DB_site->query_first("SELECT announcementid,startdate,title,user.username,user. userid,user.usertitle,user.customtitle,visits
FROM announcement
LEFT JOIN user
ON user.userid=announcement.userid
WHERE startdate<=$datenow AND enddate>=$datenow
AND $forumlist
ORDER BY startdate DESC
LIMIT 1")) {
if ($foruminfo[allowratings]) {
$thread[rating]='clear.gif';
eval("\$threadrating = \"".gettemplate('forumdisplaybit_threadrate')."\";");
$backcolor = '#DFDFDF';
$bgclass = "alt2";
} else {
unset($threadrating);
$backcolor = '#F1F1F1';
$bgclass = "alt1";
}

if ($announcement[customtitle]==2) {
$announcement[usertitle] = htmlspecialchars($announcement[usertitle]);
}
$announcement[postdate]=vbdate($dateformat,$announcement[startdate]);

if ($announcement[startdate]>$bbuserinfo[lastvisit]) {
$announcement[icon]='newannounce.gif';
} else {
$announcement[icon]='announce.gif';
}
eval("\$announcement = \"".gettemplate('forumdisplay_announcement')."\";");
$doneannouncements = 1;
}

In announcement.php

Look for the following:

$announcements=$DB_site->query("
SELECT
announcementid,startdate,enddate,announcement.titl e,pagetext,user.*,userfield.*
".iif($avatarenabled,",avatar.avatarpath,NOT ISNULL(customavatar.avatardata) AS hascustomavatar,customavatar.dateline AS avatardateline","")."
FROM announcement
LEFT JOIN user ON user.userid=announcement.userid
LEFT JOIN userfield ON userfield.userid=announcement.userid
".iif ($avatarenabled,"LEFT JOIN avatar ON avatar.avatarid=user.avatarid LEFT JOIN customavatar ON customavatar.userid=announcement.userid","")\
."
WHERE startdate<='$datenow' AND enddate>='$datenow' AND $forumlist ORDER BY startdate DESC");

And replace it with the following:

$announcements=$DB_site->query("
SELECT
announcementid,startdate,enddate,announcement.titl e,pagetext,visits,user.*,userfield.*
".iif($avatarenabled,",avatar.avatarpath,NOT ISNULL(customavatar.avatardata) AS hascustomavatar,customavatar.dateline AS avatardateline","")."
FROM announcement
LEFT JOIN user ON user.userid=announcement.userid
LEFT JOIN userfield ON userfield.userid=announcement.userid
".iif ($avatarenabled,"LEFT JOIN avatar ON avatar.avatarid=user.avatarid LEFT JOIN customavatar ON customavatar.userid=announcement.userid","")\
."
WHERE startdate<='$datenow' AND enddate>='$datenow' AND $forumlist ORDER BY startdate DESC");

In announcement.php

Look for the following:

$counter++;
//$allowhtml = 1;
//$announcebits .= getpostbit($post);

And annd the following directly below it:

$DB_site->query("UPDATE announcement set visits=visits+1 where announcementid=$post[announcementid];");


That's it. :)

MarkB
04-09-2002, 09:18 PM
YES!! Been wanting something like this for ages :D Thanks! Works great :)

Admin
04-10-2002, 10:09 AM
Great, now we can prove that no one, but no one, reads the announcements. :D

Two things:
a) Instead of updating the counter for every announcement, you could just run a query that goes more or less like this:
UPDATE announcement SET visits = visits+1 WHERE forumid IN ($forumlist);
(generate $forumlist if needed). That way you only add one query instead of one per announcement. :)

b) Use the shutdown queries of vBulletin. :) An example can be found in showthread.php:
if ($noshutdownfunc) {
$DB_site->query("UPDATE thread SET views=views+1 WHERE threadid='$threadid'");
} else {
$shutdownqueries[]="UPDATE LOW_PRIORITY thread SET views=views+1 WHERE threadid='$threadid'";
}

Zzed
04-10-2002, 10:49 AM
As always, I am most grateful for the wonderful advice. :)

Thank you. :)

Jawelin
04-10-2002, 07:42 PM
Very nice....
Should we wait for a upgrade, so ? :p
Thnx

Zzed
04-10-2002, 08:01 PM
Sure. ;)

I'll post an update later tonite. :)

Xelation
04-10-2002, 09:00 PM
also for the forumdisplay.php file, when your trying to find the first part it should be....

if ($announcement=$DB_site->query_first("SELECT announcementid,startdate,title,user.username,user. userid,user.usertitle,user.customtitle
FROM announcement
LEFT JOIN user
ON user.userid=announcement.userid
WHERE startdate<=$datenow AND enddate>=$datenow
AND $forumlist
ORDER BY startdate DESC
LIMIT 1")) {
if ($foruminfo[allowratings]) {
$thread[rating]='clear.gif';
eval("\$threadrating = \"".gettemplate('forumdisplaybit_threadrate')."\";");
$backcolor = '#1C5780';
$bgclass = "alt2";
} else {
unset($threadrating);
$backcolor = '#13486D';
$bgclass = "alt1";
}

if ($announcement[customtitle]==2) {
$announcement[usertitle] = htmlspecialchars($announcement[usertitle]);
}
$announcement[postdate]=vbdate($dateformat,$announcement[startdate]);

if ($announcement[startdate]>$bbuserinfo[lastvisit]) {
$announcement[icon]='newannounce.gif';
} else {
$announcement[icon]='announce.gif';
}
eval("\$announcement = \"".gettemplate('forumdisplay_announcement')."\";");
$doneannouncements = 1;
}

and not

if ($announcement=$DB_site->query_first("SELECT announcementid,startdate,title,user.username,user. userid,user.usertitle,user.customtitle,visits
FROM announcement
LEFT JOIN user
ON user.userid=announcement.userid
WHERE startdate<=$datenow AND enddate>=$datenow
AND $forumlist
ORDER BY startdate DESC
LIMIT 1")) {
if ($foruminfo[allowratings]) {
$thread[rating]='clear.gif';
eval("\$threadrating = \"".gettemplate('forumdisplaybit_threadrate')."\";");
$backcolor = '#1C5780';
$bgclass = "alt2";
} else {
unset($threadrating);
$backcolor = '#13486D';
$bgclass = "alt1";
}

if ($announcement[customtitle]==2)
$announcement[usertitle] = htmlspecialchars($announcement[usertitle]);
$announcement[postdate]=vbdate($dateformat,$announcement[startdate]);

if ($announcement[startdate]>$bbuserinfo[lastvisit]) {
$announcement[icon]='newannounce.gif';
} else {
$announcement[icon]='announce.gif';
}
eval("\$announcement = \"".gettemplate('forumdisplay_announcement')."\";");
$doneannouncements = 1;
}


you forgot to add 2 brackets, just thought I would let you know... it could be confusing for newbies. ;)

Crazy Mofo
04-16-2002, 01:53 AM
so any updates for this?

coz i get this error

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/domain/domain119/web/forum/announcement.php on line 34

inetd
04-16-2002, 06:10 PM
ZZed, pleaser release update this wonderful hack! :)

Zzed
04-16-2002, 07:57 PM
Ok, I updated it. :)

It is all in the original post. And I updated the attachment. :)

mnsour
04-17-2002, 06:44 PM
thanks

it's nice

inetd
04-18-2002, 06:07 PM
Zzed! Thanks! I install this hack!

Chris M
04-18-2002, 06:21 PM
Nice hack...

I might install it later if I have the time...

Satan

Sho
08-21-2002, 04:11 AM
Unfortunately, the code we have to search for has changed in 2.2.6, and I am reluctant to experiment with it. Zzed, could you provide us with updated instructions?

Boofo
08-21-2002, 04:39 AM
Zzed,

Here is the code I have for the forimdisplay.php part. How do I do the code to replace it?

// get announcements
$datenow=time();
$forumlist=getforumlist($forumid,'forumid');
$doneannouncements = 0;
$announcementbit="";
$announcements=$DB_site->query("SELECT announcementid,startdate,title,user.username,user. userid,user.usertitle,user.customtitle
FROM announcement
LEFT JOIN user
ON user.userid=announcement.userid
WHERE startdate<=$datenow AND enddate>=$datenow
AND $forumlist
ORDER BY startdate DESC");
while($announcement=$DB_site->fetch_array($announcements)) {
if ($foruminfo[allowratings]) {
$thread[rating]='clear.gif';
eval("\$threadrating = \"".gettemplate('forumdisplaybit_threadrate')."\";");
$backcolor = '{secondaltcolor}';
$bgclass = "alt2";
} else {
unset($threadrating);
$backcolor = '{firstaltcolor}';
$bgclass = "alt1";
}

if ($announcement[customtitle]==2) {
$announcement[usertitle] = htmlspecialchars($announcement[usertitle]);
}
$announcement[postdate]=vbdate($dateformat,$announcement[startdate]);

if ($announcement[startdate]>$bbuserinfo[lastvisit]) {
$announcement[icon]='newannounce.gif';
} else {
$announcement[icon]='announce.gif';
}
eval("\$announcementbit .= \"".gettemplate('forumdisplay_announcement')."\";");
$doneannouncements = 1;
}
$announcement=$announcementbit;

Sho
08-21-2002, 04:45 AM
Just got it working for 2.2.6.

forumdisplay.php:

Search for:

if ($announcement=$DB_site->query_first("SELECT announcementid,startdate,title,user.username,user. userid,user.usertitle,user.customtitle
FROM announcement
LEFT JOIN user
ON user.userid=announcement.userid
WHERE startdate<=$datenow AND enddate>=$datenow
AND $forumlist
ORDER BY startdate DESC
LIMIT 1")) {
if ($foruminfo[allowratings]) {
$thread[rating]='clear.gif';
eval("\$threadrating = \"".gettemplate('forumdisplaybit_threadrate')."\";");
$backcolor = '{secondaltcolor}';
$bgclass = "alt2";
} else {
unset($threadrating);
$backcolor = '{firstaltcolor}';
$bgclass = "alt1";
}

if ($announcement[customtitle]==2) {
$announcement[usertitle] = htmlspecialchars($announcement[usertitle]);
}
$announcement[postdate]=vbdate($dateformat,$announcement[startdate]);

if ($announcement[startdate]>$bbuserinfo[lastvisit]) {
$announcement[icon]='newannounce.gif';
} else {
$announcement[icon]='announce.gif';
}
eval("\$announcement = \"".gettemplate('forumdisplay_announcement')."\";");
$doneannouncements = 1;
}

Replace with:

if ($announcement=$DB_site->query_first("SELECT announcementid,startdate,title,user.username,user. userid,user.usertitle,user.customtitle,visits
FROM announcement
LEFT JOIN user
ON user.userid=announcement.userid
WHERE startdate<=$datenow AND enddate>=$datenow
AND $forumlist
ORDER BY startdate DESC
LIMIT 1")) {
if ($foruminfo[allowratings]) {
$thread[rating]='clear.gif';
eval("\$threadrating = \"".gettemplate('forumdisplaybit_threadrate')."\";");
$backcolor = '{secondaltcolor}';
$bgclass = "alt2";
} else {
unset($threadrating);
$backcolor = '{firstaltcolor}';
$bgclass = "alt1";
}

if ($announcement[customtitle]==2) {
$announcement[usertitle] = htmlspecialchars($announcement[usertitle]);
}
$announcement[postdate]=vbdate($dateformat,$announcement[startdate]);

if ($announcement[startdate]>$bbuserinfo[lastvisit]) {
$announcement[icon]='newannounce.gif';
} else {
$announcement[icon]='announce.gif';
}
eval("\$announcement = \"".gettemplate('forumdisplay_announcement')."\";");
$doneannouncements = 1;
}

announcement.php:

Search for:

$announcements=$DB_site->query("
SELECT
announcementid,startdate,enddate,announcement.titl e,pagetext,user.*,userfield.*
".iif($avatarenabled,",avatar.avatarpath,NOT ISNULL(customavatar.avatardata) AS hascustomavatar,customavatar.dateline AS avatardateline","")."
FROM announcement
LEFT JOIN user ON user.userid=announcement.userid
LEFT JOIN userfield ON userfield.userid=announcement.userid
".iif ($avatarenabled,"LEFT JOIN avatar ON avatar.avatarid=user.avatarid LEFT JOIN customavatar ON customavatar.userid=announcement.userid","")."
WHERE startdate<='$datenow' AND enddate>='$datenow' AND $forumlist ORDER BY startdate DESC");
while ($post=$DB_site->fetch_array($announcements)) {

Replace with:

$announcements=$DB_site->query("
SELECT
announcementid,startdate,enddate,announcement.titl e,pagetext,visits,user.*,userfield.*
".iif($avatarenabled,",avatar.avatarpath,NOT ISNULL(customavatar.avatardata) AS hascustomavatar,customavatar.dateline AS avatardateline","")."
FROM announcement
LEFT JOIN user ON user.userid=announcement.userid
LEFT JOIN userfield ON userfield.userid=announcement.userid
".iif ($avatarenabled,"LEFT JOIN avatar ON avatar.avatarid=user.avatarid LEFT JOIN customavatar ON customavatar.userid=announcement.userid","")."
WHERE startdate<='$datenow' AND enddate>='$datenow' AND $forumlist ORDER BY startdate DESC");
while ($post=$DB_site->fetch_array($announcements)) {

Everything else works still like Zzed told us.

Btw, 'morning Boofo. Slept only 3 1/2 hours. :(

Boofo
08-21-2002, 05:02 AM
That won't work for me, Sho. I don't have the if ($announcement thing at all.

Sho
08-21-2002, 05:07 AM
Well, it's for 2.2.6 - you use 2.2.5, if I remember correctly?

Boofo
08-21-2002, 05:10 AM
That's right. :)

Zzed
08-21-2002, 07:27 AM
I'm sorry guys. I am on vacation in Vancouver BC. And my resources are limited. I have this hack working on my 2.2.6. I will have to take a closer look at the problem that was reported by Boofo When I come back this coming weekend. Please accept my apologies.

Boofo
08-21-2002, 07:29 AM
Great! I will be looking forward to your solution. Have a great "rest of your vacation" and we'll see you then. :)

Originally posted by Zzed
I'm sorry guys. I am on vacation in Vancouver BC. And my resources are limited. I have this hack working on my 2.2.6. I will have to take a closer look at the problem that was reported by Boofo When I come back this coming weekend. Please accept my apologies.

Zzed
08-27-2002, 09:27 PM
Boofo, all you have to do is replace your current query statement:
$announcements=$DB_site->query("SELECT announcementid,startdate,title,user.username,user. userid,user.usertitle,user.customtitle
FROM announcement
LEFT JOIN user
ON user.userid=announcement.userid
WHERE startdate<=$datenow AND enddate>=$datenow
AND $forumlist
ORDER BY startdate DESC");


With this:

$announcements=$DB_site->query("SELECT announcementid,startdate,title,user.username,user. userid,user.usertitle,user.customtitle,visits
FROM announcement
LEFT JOIN user
ON user.userid=announcement.userid
WHERE startdate<=$datenow AND enddate>=$datenow
AND $forumlist
ORDER BY startdate DESC");


I just added the visits to the end of the first line. ;)

Boofo
08-27-2002, 10:08 PM
Thanks Zzed. Sorry about the newbie question. :)

Zzed
08-27-2002, 10:10 PM
You're welcome. :)

mike_tcis
10-10-2002, 03:15 PM
Does this hack still work for 2.2.8?