TinyPortal

Development => Block Codes => Topic started by: Paragaya on March 01, 2009, 08:50:45 AM

Title: [Block] Top 10 Karma
Post by: Paragaya on March 01, 2009, 08:50:45 AM
Add new php blok :)



SMF 1.1:

global $db_prefix, $scripturl;

$members_result = db_query("
SELECT ID_MEMBER, realName, karmaGood
FROM {$db_prefix}members
ORDER BY karmaGood DESC
LIMIT 10", __FILE__, __LINE__);
$members = array();
while ($row_members = mysql_fetch_assoc($members_result))
{
$members[] = array(
'name' => $row_members['realName'],
'id' => $row_members['ID_MEMBER'],
'karma' => $row_members['karmaGood'],
'href' => $scripturl . '?action=profile;u=' . $row_members['ID_MEMBER'],
'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['ID_MEMBER'] . '">' . $row_members['realName'] . '</a>'
);
}
mysql_free_result($members_result);

if (empty($members))
return;

echo '
<ul style="padding: 0 0 0 2em;">';

foreach ($members as $member)
echo '
<li>', $member['link'], ' - ', $member['karma'], '</li>';

echo '
</ul>';


SMF 2.0:

global $smcFunc, $scripturl;

$members_result = $smcFunc['db_query']('', '
SELECT id_member, real_name, karma_good
FROM {db_prefix}members
ORDER BY karma_good DESC
LIMIT 10');
$members = array();
while ($row_members = $smcFunc['db_fetch_assoc']($members_result))
{
$members[] = array(
'name' => $row_members['real_name'],
'id' => $row_members['id_member'],
'karma' => $row_members['karma_good'],
'href' => $scripturl . '?action=profile;u=' . $row_members['id_member'],
'link' => '<a href="' . $scripturl . '?action=profile;u=' . $row_members['id_member'] . '">' . $row_members['real_name'] . '</a>'
);
}
$smcFunc['db_free_result']($members_result);

if (empty($members))
return;

echo '
<ul class="normallist">';

foreach ($members as $member)
echo '
<li>', $member['link'], ' - ', $member['karma'], '</li>';

echo '
</ul>';


Thanks Paragaya (http://www.tinyportal.net/index.php?action=profile;u=47242) YaÃ,,ŸÃ,,±z... (http://www.simplemachines.org/community/index.php?action=profile;u=102749) , Blue Dream (http://www.simplemachines.org/community/index.php?action=profile;u=118168)
Title: Re: [Block] Top 10 Karma
Post by: kfrost on May 14, 2009, 11:50:55 PM
Paragaya,

Thank you so much, I've been thinking about something like this.  Question, I added this to our forum, SMF 1.18, TP 1.03, you can view the at www.berewickforums.com, the bullet items are centered justified, and I'd like to get them left justified.  If you take a look at the link above you should see how the existing justification causes some items to take up 2 lines.

I'm a developer but with zero PHP experience.  Any help on how to correct this problem would be greatly appreciated.

Doh, actually figured it out myself.  Was going to delete this but in case somebody else runs into this.

To get everything to the left, I did the following.

<ul style="padding: 0 0 0 0; margin: 0 0 0 0">';
Title: Re: [Block] Top 10 Karma
Post by: JPDeni on May 15, 2009, 12:02:56 AM
It has to do with your style.css file in your theme. When I went to your site, I viewed it with the default theme and the list is left-justified.

You could probably use in-line css if you wanted to, though. Just add it to the <li> tag. Something like


<li style="text-align:left;">', $member['link'], ' - ', $member['karma'], '</li>';


You can put anything there, just like you would for a regular html page.
Title: Re: [Block] Top 10 Karma
Post by: kfrost on May 15, 2009, 12:06:23 AM
Thanks JP,

I've got it how I want it now.  There is a problem with this block, it only displays positive Karma points instead of the total between Positive and Negative.

P.S. Maybe I should clarify, it's a problem if your displaying totals.  I assume there should be a way to do a column based on sum and use it.  I'm assumming this is going to be like most T-SQL but if anybody has this I would appreciate it.

I was assumming something such as this would work but apparently not.

SELECT ID_MEMBER, realName, karmaGood - karmaBad as
             karmaTotal
   FROM {$db_prefix}members
   ORDER BY karmaTotal DESC
   LIMIT 10", __FILE__, __LINE__);

Thanks
Title: Re: [Block] Top 10 Karma
Post by: kfrost on May 15, 2009, 12:33:23 AM
Hmm, if I just use karmaBad, the correct figures show up.  If do a karmaGood + karmaBad as karmaTotal, that seems to work.

However, karmaGood - karmaBad, I'm getting figures such as 18446744073709551615

If you go into Forum Profile Information, there is a total label.  Is this by chance stored in the database, or does anybody know how that's being done to migrate the functionality over to this code block?

Title: Re: [Block] Top 10 Karma
Post by: JPDeni on May 15, 2009, 12:53:50 AM
I remember having problems like that before, so I searched out the code I wrote for it. This seems to work:


SELECT realName, (karmaGood - karmaBad) as Karma, ID_MEMBER,


You need the parentheses.
Title: Re: [Block] Top 10 Karma
Post by: kfrost on May 15, 2009, 12:58:58 AM
JP,

First thanks for you help here.  I added the parens as you suggested and still getting the same results.  The problems are with users with say 1 applaud and 5 smites.  It's like it going when it's dropping below zero, instead of going to a negative number, it's looping back at the highest number for an integer or whatever these fields are and counting back down.  Kind of what you might see with an unsigned int?

Title: Re: [Block] Top 10 Karma
Post by: saks on November 22, 2011, 08:43:03 AM
How show total karma for current logged user ?