PDA

View Full Version : Determine thread position in the threadlist


addamroy
02-07-2013, 12:35 AM
I have some ideas running around in my head but can't put my finger on how to make it happen.

Anyway what I want to accomplish somehow is...

possibly a conditional or a variable of some sort to determine a threads position in the threadlist. A conditional that I could use in SHOWTHREAD template.

For example,

In the showthread template I could add a line of code somewhere that says "Your thread is at X position in this forum" Where X would be the threads position in the thread list for that forum section.

Or I could use a conditional somehow, if X >= 20 then display a message that says "your thread is no longer on the first page.

Either way I'm just curious if there's any way to get the position of a thread, and have that be usable in the showthread template.

LifesGreatestGift
02-07-2013, 02:43 AM
How would you determine which thread of the user's to check for? Would you just want it to check their "latest" thread?

--------------- Added 1360208626 at 1360208626 ---------------

Will it be every forum, or just one?

addamroy
02-07-2013, 02:55 AM
I was thinking in just one forum for now, and yes that message would only show if it's their own thread.

I know how to show the message in certain forums, in the right spot and only for the thread owner, but can't for the life of me figure out a way to determine the thread's position so I can use that info too :(

LifesGreatestGift
02-07-2013, 03:02 AM
Will it be for the user's most recent thread? or all threads like "your threads are in position 8 and 14" or "your thread is in position 4" for just their most recent?

addamroy
02-07-2013, 03:14 AM
It will show ON the thread they are viewing.

For example - This thread is at position 7

LifesGreatestGift
02-07-2013, 03:20 AM
This also depends on your forum sort order. what would that be?

--------------- Added 1360212358 at 1360212358 ---------------

This should help you A LOT. I am not sure if it needs to go into showthread_start or showthread_complete but the code is sound. may need a bit of tweaking. Hope it helps.

$current_thread = $threadinfo['threadid'];
$current_thread_forum = $threadinfo['forumid'];
$thread_owner = $threadinfo['postuserid'];
$current_user = $bbuserinfo['userid'];
$forum_id = 2;

if ($current_thread_forum == $forum_id) {
if ($thread_owner == $current_user) {
$query = $vbulletin->db->query_first('set @row_num = 0; SELECT @row_num := @row_num + 1 as row_number,threadid,postuserid,title FROM thread WHERE forumid = ' . $forum_id . ' AND postuserid = ' . $current_user . ' ORDER BY dateline DESC LIMIT 1;');
$position = $query['row_number'];
$display = "This thread is at position: " . $position;
vB_Template::preRegister('SHOWTHREAD',array('displ ay' => $display));
}
}

Then you should be able to use {vb:raw display} in the SHOWTHREAD template.

addamroy
02-07-2013, 09:15 PM
Thanks for the help I appreciate it!

Unfortunately that didn't work using either showthread_complete or showthread_start :(

PS - My threads are sorted by start time

LifesGreatestGift
02-08-2013, 01:17 PM
It does work, run this query inside phpmyadmin or whatever you use.

set @row_num = 0; SELECT @row_num := @row_num + 1 as row_number,threadid,postuserid,title FROM thread WHERE forumid = # AND postuserid = ## ORDER BY dateline DESC LIMIT 1;

# = forumid is the forum you want to use the mod in
## = userid , just make it one to run test on admin first user

will return row of thread with its placement. if you remove "LIMIT 1" it will show more threads.

--------------- Added 1360336817 at 1360336817 ---------------

ill revise the code in a bit. i forgot something.

--------------- Added 1360346941 at 1360346941 ---------------

Here is the final working and tested code. It will show the position of the thread based on start date, not last reply. With new threads being position 1. (if you would like it the other way around change "DESC" to "ASC" in the $query)

$current_thread = $thread['threadid'];
$current_thread_forum = $thread['forumid'];
$thread_owner = $thread['postuserid'];
$current_user = $vbulletin->userinfo['userid'];
$forum_id = 2;

if ($current_thread_forum == $forum_id) {
if ($thread_owner == $current_user) {
$vbulletin->db->query_write('set @row_num = 0;');
$query = $vbulletin->db->query_read_slave(
'SELECT (SELECT @row_num := @row_num + 1)
AS row_number,threadid,postuserid
FROM ' . TABLE_PREFIX . 'thread
WHERE forumid = ' . $forum_id . '
ORDER BY dateline DESC'
);

while($query2 = $vbulletin->db->fetch_array($query)) {
if ($query2['threadid'] == $current_thread && $query2['postuserid'] == $current_user) {
$position = $query2['row_number'];
$display = "<p id='pos-text-main'><span>This thread is at position:</span> <span id='pos-num'>" . $position . "</span></p>";
}
}

vB_Template::preRegister('SHOWTHREAD',array('displ ay' => $display));
}
}

add that code to a new plugin with hook showthread_complete

Then add the following where you want it inside of SHOWTHREAD.
{vb:raw display}

I have added some span id's so you can style it how you'd like.

Be sure to change $forum_id to the forum you'd like this running in.

--------------- Added 1360347115 at 1360347115 ---------------

Here is a sample CSS to add to additional.css


p#pos-text-main {
background: rgb(255, 202, 202);
border: 1px solid rgb(184, 0, 0);
margin: 10px 0;
padding: 10px;
}
span#pos-num {
font-weight: bold;
}

It will only show for the thread author. Example attached.

addamroy
02-09-2013, 12:43 PM
Perfect!

LifesGreatestGift
02-09-2013, 01:10 PM
forgot to add the p and span to final code :) check now

addamroy
02-12-2013, 01:59 PM
LifesGreatestGift, is there anyway I could use a vb:if conditional inside showthread with that new position variable? I just want to wrap it around something. vb:if condition="$position >= x" or something like that.

LifesGreatestGift
02-12-2013, 04:55 PM
Theres no need, it will only display when the condition of the plugin is met. So just put it where you want and style it in the plugin.

addamroy
02-12-2013, 11:20 PM
Well I have another mod that shows a button somewhere else in the SHOWTHREAD template, and I want to essentially show one button there if the position is higher than a certain number, and another button if its lower than that number. Was hoping to accomplish that with a simple conditional, if it's possible anyway. No big need it's more of an extra.

LifesGreatestGift
02-12-2013, 11:34 PM
will only the thread starter see it or everyone? if everyone, the query will need to be ran for everyone.

--------------- Added 1360715675 at 1360715675 ---------------

right now, the query is just ran for the thread author.

addamroy
02-12-2013, 11:37 PM
I see what you mean. Yes it does still have to work for the thread owner, but there is a vbif conditional for that right, thread postuserid == bbuserinfo userid or something like that? So I could wrap that conditional around it so the query is still only run for the thread owner, then a conditional inside that for $position (assuming I can even use that as a conditional as a result of using the code you gave me)

--------------- Added 1360715918 at 1360715918 ---------------

Maybe i just don't know what I'm talking about lol :/

LifesGreatestGift
02-12-2013, 11:39 PM
I need to know, if everyone will be able to see the new button based on the condition you want to make, or just the thread author.

addamroy
02-13-2013, 01:01 AM
just the thread author

--------------- Added 1360724846 at 1360724846 ---------------

Right next to the new thread button is where I have it now. (it just doesn't change according to position) The only reason I mention that at all is because I was thinking of the 2 different hook locations you talked about, showthread_complete and showthread_start idk if that location makes a difference.

LifesGreatestGift
02-13-2013, 02:25 AM
does this work?

<vb:if condition="$position == 2">DO THIS</vb:if>

only way you would be able to tell, is if you are the author of the second thread (or whatever number you put in the conditional). Test it out. If it doesnt work, in the code I gave you

FIND
vB_Template::preRegister('SHOWTHREAD',array('displ ay' => $display));

REPLACE WITH
vB_Template::preRegister('SHOWTHREAD',array('displ ay' => $display));
vB_Template::preRegister('SHOWTHREAD',array('posit ion' => $position));

Then see if the conditional works.

addamroy
02-13-2013, 12:52 PM
I think it's stupid that I have to like someone else's post before I can like yours, LOL

That worked perfect, I had to add the second preregister line to make it work. Awesomeness. Thank you so much for the help, seriously appreciate it.