Simple Karma Challenge

phpBB3 and MOD challenges setup by the staff to test and challenge your phpBB3 coding skills.

Re: Simple Karma Challenge

Postby Pace » 26 Apr 2007, 10:50

Aye, it moves, and the end and beginning markers are fine.

I even modified the code to make it more like the way custom profile fields work, so I have the following in viewtopic:

Code: Select all
    while ($target = $db->sql_fetchrow($res))
    {
        include_once($phpbb_root_path . 'includes/functions_karma_p.' . $phpEx);
        $ka = new karma();
        $karma_cache = $ka->grab_karma_rank($target['user_karma']);
    }


And the following at the beginning of the functions file now:
Code: Select all
<?php

class karma
{
    var $karma_cache = array();

    function build_karma_cache()
    {
        global $db, $user, $auth;

        $this->profile_cache = array();
    }

    function grab_karma_rank($karma1)
    {


(and yes, I added a "}" at the end of the class ;) )

But now, I get this problem:
Code: Select all
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home.8/a/r/p/arpia/www/phpBB3/includes/functions_karma_p.php on line 14


(line 14, which happens to be the same line, the "function ?" line)
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male




phpBB Academy at StarTrekGuide
Support STG
Using PayPal Donate

Re: Simple Karma Challenge

Postby Handyman » 26 Apr 2007, 10:58

:shock: that thing is hyper critical of your code? you must be using IE to browse it ;)
I hope somebody else has an idea, because I'm out? I've never had that happen before.
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7459
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Postby Pace » 26 Apr 2007, 10:59

Safari ;)

Any other ideas?

Edit: here's the whole function file:
Spoiler:
Code: Select all
<?php

class karma
{
    var $karma_cache = array();

    function build_karma_cache()
    {
        global $db, $user, $auth;

        $this->profile_cache = array();
    }

    function grab_karma_rank($karma1)
    {
        global $user, $template;
        // when we expand this mod for ACP and database control,
        // we would probably use regular expressions to determine the values,
        // but for now, we can be somewhat static.
        $i = $karma1;
        switch ($i)
        {
            case ($i == 0 || $i == -0):
                $user_karma = $user->lang['karma_negative'][0];
            break;
            case ($i == -1):
                $user_karma = $user->lang['karma_negative'][1];
            break;
            case ($i == -2):
                $user_karma = $user->lang['karma_negative'][2];
            break;
            case ($i == -3):
                $user_karma = $user->lang['karma_negative'][3];
            break;
            case ($i == -4):
                $user_karma = $user->lang['karma_negative'][4];
            break;
            case ($i == -5):
                $user_karma = $user->lang['karma_negative'][5];
            break;
            case ($i <= -&& $i >= -7):
                $user_karma = $user->lang['karma_negative'][6];
            break;
            case ($i <= -&& $i >= -9):
                $user_karma = $user->lang['karma_negative'][7];
            break;
            case ($i <= -10):
                $user_karma = $user->lang['karma_negative'][8];
            break;
            // this last one would be "banned"
            // then start on the positive case...
            case ($i >= 1 && $i <= 5):
                $user_karma = $user->lang['karma_positive'][0];
            break;
            case ($i >= 6 && $i <= 12):
                $user_karma = $user->lang['karma_positive'][1];
            break;
            case ($i >= 13 && $i <= 21):
                $user_karma = $user->lang['karma_positive'][2];
            break;
            case ($i >= 22 && $i <= 32):
                $user_karma = $user->lang['karma_positive'][3];
            break;
            case ($i >= 33 && $i <= 45):
                $user_karma = $user->lang['karma_positive'][4];
            break;
            case ($i >= 46 && $i <= 50):
                $user_karma = $user->lang['karma_positive'][5];
            break;
            case ($i >= 51 && $i <= 67):
                $user_karma = $user->lang['karma_positive'][6];
            break;
            case ($i >= 68 && $i <= 86):
                $user_karma = $user->lang['karma_positive'][7];
            break;
            case ($i >= 87 && $i <= 99):
                $user_karma = $user->lang['karma_positive'][8];
            break;
            case ($i >= 100):
                $user_karma = $user->lang['karma_positive'][9];
            break;
        }

        if (!sizeof($this->karma_cache))
        {
            $this->build_karma_cache();
        }

        $template->assign_vars(array(
            'KARMA_LEVEL'    => $user_karma . " ($karma1)",
            'KARMA' => $user->lang['KARMA'],
        ));
    
        return
;
    }
}

?>


And here's the entirety of what karma code I've got in viewtopic:

Spoiler:
Code: Select all
   $sql = 'SELECT *
        FROM '
 . POSTS_TABLE . '
        WHERE topic_id = '
 . $topic_data['topic_id'];
    $res = $db->sql_query($sql);
    $poster = $db->sql_fetchrow($res);
    $db->sql_freeresult($res);
    $sql = 'SELECT *
        FROM '
 . USERS_TABLE . '
        WHERE user_id = '
 . $poster['poster_id'];
    $res = $db->sql_query($sql);
    while ($target = $db->sql_fetchrow($res))
    {
        include_once($phpbb_root_path . 'includes/functions_karmap.' . $phpEx);
        $ka = new karma();
        $karma_cache = $ka->grab_karma_rank($target['user_karma']);
    }
    $db->sql_freeresult($res);

I put this right before "// Load custom profile fields". Could that be why it doesn't work?
(I must admit I don't know exactly where I should put that piece of code?)

functions_karmap.php.zip
(849 Bytes) Downloaded 107 times
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

Re: Simple Karma Challenge

Postby CorniI » 26 Apr 2007, 11:56

hi
I can't you help, too, my only recommendatrion is, retype your Code and save it as a completly new File, and do not Copy & Paste, becuase i think the File is corrupt, I can't help you more :( Very Weird.
CorniI
CorniI
Crewman
Crewman
 
Posts: 6
Joined: 17 Apr 2007, 08:24
Location: Germany
Favorite Team: Sport kills!
Gender: Male
phpBB Knowledge: 6

ARGH

Postby Pace » 26 Apr 2007, 12:41

:banghead: :banghead: :banghead: :cry:


I got that file to work, by following CorniI's suggestion (it was indeed corrupted), but? it still doesn't work.
I still get the karma of the last poster.
:banghead: :banghead: :banghead:

:blackeye:

HoL, I think I'll let you explore the idea of "get_user_rank" (in functions_display.php), because I've had enough of PHP code for now.
Once someone figures out how the heck I can get the karma to work in individual posts, then I might get back into it. But for now, no more from me in this topic.

The code for the file is exactly the same, so no trouble there, if any of you should feel the urge to continue where I left off.

:write:
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

Re:

Postby tannie » 26 Apr 2007, 13:03

Pace wrote:
Code: Select all
    while ($target = $db->sql_fetchrow($res))



Am I wrong or should this look like..
Code: Select all
    while ($target == $db->sql_fetchrow($res))


I think if you are checking the condition for equality then there should be two equal signs, right?
I haven't gone through the entire code but this particular line just caught my eye.. :scratch:
Image
User avatar
tannie
Supporter
Supporter
 
Posts: 427
Joined: 09 Jul 2006, 10:50
Gender: Male

Re: Re:

Postby Handyman » 26 Apr 2007, 13:06

tannie wrote:
Pace wrote:
Code: Select all
    while ($target = $db->sql_fetchrow($res))



Am I wrong or should this look like..
Code: Select all
    while ($target == $db->sql_fetchrow($res))


I think if you are checking the condition for equality then there should be two equal signs, right?
I haven't gone through the entire code but this particular line just caught my eye.. :scratch:

in this particular line, you would not want 2 equals signs? a while loop assigns $target the array of the current row.
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7459
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: Re:

Postby tannie » 26 Apr 2007, 13:10

Handyman wrote:in this particular line, you would not want 2 equals signs? a while loop assigns $target the array of the current row.

Oh.. Thnx for correcting me there Handyman.. :doh:
Image
User avatar
tannie
Supporter
Supporter
 
Posts: 427
Joined: 09 Jul 2006, 10:50
Gender: Male

Re: Simple Karma Challenge

Postby Highway of Life » 26 Apr 2007, 16:31

Sorry it?s so frustrating, Pace... I wish there were enough of me that I could help everyone all the time, but time is my limit.

I still have to create the Thanks MOD, which is similar to Karma.
If you want, wait until I do the thanks MOD, then use it as inspiration for the Karma MOD.

-- Unless someone else would like to assist our Pace. ;)
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10

Postby Pace » 29 Apr 2007, 00:50

I just realised something: it's in both viewtopic.php and the function file that I'm missing some code.

This is the code for the custom profile fields function:

Spoiler:
Code: Select all
    /**
    * Assign fields to template, used for viewprofile, viewtopic and memberlist (if load setting is enabled)
    * This is directly connected to the user -> mode == grab is to grab the user specific fields, mode == show is for assigning the row to the template
    * @access public
    */
    
function generate_profile_fields_template($mode$user_id 0$profile_row false)
    {
        global 
$db;

        if (
$mode == 'grab')
        {
            if (!
is_array($user_id))
            {
                
$user_id = array($user_id);
            }

            if (!
sizeof($this->profile_cache))
            {
                
$this->build_cache();
            }

            if (!
sizeof($user_id))
            {
                return array();
            }

            
$sql 'SELECT *
                FROM ' 
PROFILE_FIELDS_DATA_TABLE '
                WHERE ' 
$db->sql_in_set('user_id'array_map('intval'$user_id));
            
$result $db->sql_query($sql);

            
$field_data = array();
            while (
$row $db->sql_fetchrow($result))
            {
                
$field_data[$row['user_id']] = $row;
            }
            
$db->sql_freeresult($result);

            
$user_fields = array();

            
// Go through the fields in correct order
            
foreach (array_keys($this->profile_cache) as $used_ident)
            {
                foreach (
$field_data as $user_id => $row)
                {
                    
$user_fields[$user_id][$used_ident]['value'] = $row['pf_' $used_ident];
                    
$user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident];
                }
            }

            return 
$user_fields;
        }
        else if (
$mode == 'show')
        {
            
// $profile_row == $user_fields[$row['user_id']];
            
$tpl_fields = array();
            
$tpl_fields['row'] = $tpl_fields['blockrow'] = array();

            foreach (
$profile_row as $ident => $ident_ary)
            {
                
$value $this->get_profile_value($ident_ary);

                if (
$value === NULL)
                {
                    continue;
                }

                
$tpl_fields['row'] += array(
                    
'PROFILE_' strtoupper($ident) . '_VALUE'    => $value,
                    
'PROFILE_' strtoupper($ident) . '_TYPE'    => $ident_ary['data']['field_type'],
                    
'PROFILE_' strtoupper($ident) . '_NAME'    => $ident_ary['data']['lang_name'],
                    
'PROFILE_' strtoupper($ident) . '_EXPLAIN'=> $ident_ary['data']['lang_explain'],

                    
'S_PROFILE_' strtoupper($ident)            => true
                
);

                
$tpl_fields['blockrow'][] = array(
                    
'PROFILE_FIELD_VALUE'    => $value,
                    
'PROFILE_FIELD_TYPE'    => $ident_ary['data']['field_type'],
                    
'PROFILE_FIELD_NAME'    => $ident_ary['data']['lang_name'],
                    
'PROFILE_FIELD_EXPLAIN'    => $ident_ary['data']['lang_explain'],

                    
'S_PROFILE_' strtoupper($ident)        => true
                
);
            }
        
            return 
$tpl_fields;
        }
        else
        {
            
trigger_error('Wrong mode for custom profile'E_USER_ERROR);
        }
    }


So far, all I've done is the "grab" part, I reckon. So how would I do the "show" part?

If you look at viewtopic.php, you'll see plenty of instances where "$cp_row" is called up, and that's the custom profile row. I can't seem to make any sense of what's going on (or at least, I never manage to make it work), so I hope this will point someone in the right direction?
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

PreviousNext

Return to phpBB3 Challenges at phpBB Academy

Who is online

Users browsing this forum: No registered users and 2 guests