MyBB Community Forums

Full Version: Getting a a value from one of the SQL tables to show up on forumdisplay
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Dear MyBB community,

thank you in advance for taking the time to read my request...and any assistance with my customization plans  Blush

I know that the "Rate thread" feature of MyBB has many opponents, but I personally find it very useful -- because in our MyBB board (v1.8.29), we have a little subforum for "TV show ratings", where each thread represents a TV show and people use the thread rating to vote how much they like each show.

So, recently my users have requested that the threads overview on the Forum Display page not only shows each thread's current average rating, but also tells the user which rating they have given to the thread. Of course, this should only work for members and not for guests.

From what I have gathered, this variable (which is stored in the mybb_threadratings table) is not yet obtained by forumdisplay.php, so I cannot simply add something to my template...but actually need to modify the PHP file to get this data from my SQL database.

Unfortunately, my PHP/SQL knowledge is preeetttty basic, and after 2 hours of reading guides and tutorials, this is what I have added to my forumdisplay.php so far (inserting it at the bottom of the file):

if($mybb->user['uid'])
{
 $query = $db->simple_select("threadratings", "rating", "uid='".$mybb->user['uid']."' AND tid='".$thread['tid']."'");
$myownrating = $db->fetch_array($query);
}
else
{
$myownrating = '';
}

...but this might be completely wrong, because adding neither {$myownrating} nor {$myownrating['tid']} to my forumdisplay_thread_rating template shows the data here.

If it isn't too much trouble...could somebody assist me with obtaining this value from the database and showing it only to members under the average star rating?

Thanks again!
(2022-02-02, 03:31 AM)fernhafen Wrote: [ -> ]adding neither {$myownrating} nor {$myownrating['tid']} to my forumdisplay_thread_rating template shows the data here.

Yep, you probably want {$myownrating['rating']} instead.
Thank you for your reply!

Unfortunately, it still didn't work. I added
<span style="color:#988">Your voting: {$myownrating['rating']} Stars</span>

to my template, but it only shows "Your voting: Stars".

So, I guess my PHP edit shown above is still not correct? Any further ideas? Do I maybe need to make it into a function or something?
(2022-02-02, 11:13 AM)fernhafen Wrote: [ -> ]So, I guess my PHP edit shown above is still not correct?

Yep, it shouldn't be at the bottom of the post - it needs to run for every thread, not once only. I don't have the time right now to help further, but I'll try to remember to get back to you.
Surely a query for forumdisplay to pull an array of {$myownrating['rating']} would include the tid.
It seems the query in the OP is more suitable for showthread.
Hey guys, thank you very much for your time and replies!

I figured that this might be a bit more complicated and, as a noob, I appreciate all assistance. Whenever you have the time, Laird!

@HLFadmin: Maybe I am mistaken but doesn't my query already include the tid (at the very end)?
Attached is a quick-n-dirty plugin that will hopefully be adequate for your purposes.

Place it in your inc/plugins directory, activate it via the ACP, and then edit a {$myownrating} variable into your forumdisplay_thread_rating template. For example, here's what that template looks like in my default theme after inserting that variable:

<td align="center" class="{$bgcolor}{$thread_type_class}" id="rating_table_{$thread['tid']}">
 <ul class="star_rating{$not_rated}" id="rating_thread_{$thread['tid']}">
 <li style="width: {$thread['width']}%" class="current_rating" id="current_rating_{$thread['tid']}">{$ratingvotesav}</li>
 </ul>
    {$myownrating}
 <script type="text/javascript">
 <!--
 Rating.build_forumdisplay({$thread['tid']}, { width: '{$thread['width']}', extra_class: '{$not_rated}', current_average: '{$ratingvotesav}' });
 // -->
 </script>
 </td>
Can be achieved this simple way:

-- forumdisplay.php / line 880:
SELECT t.*, {$ratingadd}t.username AS threadusername, u.username, r.rating AS userownrating
FROM ".TABLE_PREFIX."threads t
LEFT JOIN ".TABLE_PREFIX."users u ON (u.uid = t.uid)
LEFT JOIN ".TABLE_PREFIX."threadratings r ON (t.tid = r.tid) AND (r.uid = {$mybb->user['uid']})
WHERE t.fid='$fid' $tuseronly $tvisibleonly $datecutsql2 $prefixsql2
ORDER BY t.sticky DESC, {$t}{$sortfield} $sortordernow $sortfield2
LIMIT $start, $perpage

Just add another field to the SELECT (AS userownrating) and read your rating using the additional LEFT JOIN.

Drop the following variable wherever you want in Forum Bit templates:
$thread['userownrating']

Good luck!
[ExiTuS]
Isn't this community just absolutely brilliant? Thank you so much for two very helpful replies in such a short amount of time  Blush

Since both of you, Laird and ExiTuS, took the time to work on my problem, I tried out both of your solutions! First of all, let me thank you a lot!

@Laird: I installed your plugin (awesome!), and it actually showed something in that {$myownrating} variable...However, the data didn't match the respective thread ID. In other words, the "ownrating" seemed to be displayed for a different person or a different thread...

@Exitus: Wow, such a simple edit?! I tried it out and it absolutely worked Smile Thank you. It doesn't have the "ease-of-use" of Laird's solution (whose plugin also showed "Not yet rated" if you didn't vote), but it's a 100% perfect solution.
(2022-02-03, 03:41 PM)fernhafen Wrote: [ -> ]@Laird: I installed your plugin (awesome!), and it actually showed something in that {$myownrating} variable...However, the data didn't match the respective thread ID. In other words, the "ownrating" seemed to be displayed for a different person or a different thread...

Oops, I hooked in to the second hook too late, so the value for the previous (above) thread instead of the current one was being displayed. Have updated the attached plugin file in my previous post to v1.0.1 so as to fix this problem.
Pages: 1 2