$this->user->data['user_id']) does not work on the online server [SOLVED]

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
caiocald
Registered User
Posts: 60
Joined: Mon Feb 26, 2018 9:32 pm
Location: Brasil
Name: B!
Contact:

$this->user->data['user_id']) does not work on the online server [SOLVED]

Post by caiocald » Tue Jun 18, 2019 1:59 pm

Hello guys!

I recently noticed that one of the functions in my extension is no longer working on my online server.
This is a basic function that modifies the push button text based on the query in the user database.

That is, if the user has already submitted a value on that form, the button text changes from "send" to "update".
However, such a function is only working on my test server (localhost).

Anyone have any suggestions for what might be happening?

Thank you!

Code: Select all

 $sql = 'SELECT COUNT(*) AS number_of_rows , avg(comp1) as comp1 , avg(comp2) as comp2 , avg(comp3) as comp3 , avg(comp4) as comp4 , avg(comp5) as comp5 , avg(user_id) as user_id
                FROM ' . $this->pontuacao_enem_table . '
                WHERE topic_id = ' . $this->request->variable('t', 0);
        $result = $this->db->sql_query($sql);
        while ($row = $this->db->sql_fetchrow($result))
        {
            $user_id = $row['user_id'];
            $number_of_rows = $row['number_of_rows'];
            $comp1 = Round($row['comp1'], 0);
            $comp2 = Round($row['comp2'], 0);
            $comp3 = Round($row['comp3'], 0);
            $comp4 = Round($row['comp4'], 0);
            $comp5 = Round($row['comp5'], 0);
        }
        $this->db->sql_freeresult($result);

if ($number_of_rows >0)
        {
            if ($user_id == $this->user->data['user_id']){
                $this->template->assign_vars(array(
                    'PONTUAR_BUTTON'    => $this->user->lang('PONTUACAO_ENEM_PONTUAR_ATT'),
                ));
            }else {
                $this->template->assign_vars(array(
                    'PONTUAR_BUTTON'    => $this->user->lang('PONTUACAO_ENEM_PONTUAR'),
                ));
Last edited by caiocald on Mon Aug 05, 2019 4:13 pm, edited 4 times in total.

User avatar
david63
Registered User
Posts: 16317
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: $this->user->data['user_id']) does not work on the online server

Post by david63 » Tue Jun 18, 2019 2:28 pm

I may be wrong but $user_id == [b]$this->user->data['user_id'])[/b] will always be false. You cannot have inline html tags in a PHP statement.

I suspect the reason is that your test server is on Windows whereas your production server is *nix

Also you should use the language object - not $this->user->lang()
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
caiocald
Registered User
Posts: 60
Joined: Mon Feb 26, 2018 9:32 pm
Location: Brasil
Name: B!
Contact:

Re: $this->user->data['user_id']) does not work on the online server

Post by caiocald » Tue Jun 18, 2019 7:29 pm

Hello,
I ended up putting these tags to highlight the excerpt but did not even realize that I forgot to take to do the post here, sorry.

Code updated.

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51758
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: $this->user->data['user_id']) does not work on the online server

Post by Brf » Tue Jun 18, 2019 7:38 pm

Your statement can only select one row, which is always the average of your data... So if there are two different users in your data for that topic, the returned "user_id" is the average of the actual user_id's which is likely not equal to the current user_id

User avatar
caiocald
Registered User
Posts: 60
Joined: Mon Feb 26, 2018 9:32 pm
Location: Brasil
Name: B!
Contact:

Re: $this->user->data['user_id']) does not work on the online server

Post by caiocald » Tue Jun 18, 2019 7:43 pm

I tried with GROUP BY but it did not work :(

User avatar
caiocald
Registered User
Posts: 60
Joined: Mon Feb 26, 2018 9:32 pm
Location: Brasil
Name: B!
Contact:

Re: $this->user->data['user_id']) does not work on the online server

Post by caiocald » Tue Jun 18, 2019 8:03 pm

I solved by removing avg from (user_ir)

Code: Select all

$sql = 'SELECT COUNT(*) AS number_of_rows , avg(comp1) as comp1 , avg(comp2) as comp2 , avg(comp3) as comp3 , avg(comp4) as comp4 , avg(comp5) as comp5 , (user_id) as user_id
Last edited by caiocald on Tue Jun 18, 2019 8:55 pm, edited 1 time in total.

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51758
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: $this->user->data['user_id']) does not work on the online server [SOLVED]

Post by Brf » Tue Jun 18, 2019 8:10 pm

nope. If you use "=", then it is an assignment, which is always true.

User avatar
caiocald
Registered User
Posts: 60
Joined: Mon Feb 26, 2018 9:32 pm
Location: Brasil
Name: B!
Contact:

Re: $this->user->data['user_id']) does not work on the online server

Post by caiocald » Tue Jun 18, 2019 9:27 pm

Code: Select all

$sql = 'SELECT COUNT(*) AS number_of_rows , avg(comp1) as comp1 , avg(comp2) as comp2 , avg(comp3) as comp3 , avg(comp4) as comp4 , avg(comp5) as comp5 , (user_id) as user_id
                FROM ' . $this->pontuacao_enem_table . '
                WHERE topic_id = ' . $this->request->variable('t', 0);

        $result = $this->db->sql_query($sql);
        while ($row = $this->db->sql_fetchrow($result))
        {
            $user_id = $row['user_id'];
            $number_of_rows = $row['number_of_rows'];
            $comp1 = Round($row['comp1'], 0);
            $comp2 = Round($row['comp2'], 0);
            $comp3 = Round($row['comp3'], 0);
            $comp4 = Round($row['comp4'], 0);
            $comp5 = Round($row['comp5'], 0);
        }
        $this->db->sql_freeresult($result);

        if ($number_of_rows >0)
        {
            $user_id_session = $this->user->data['user_id'];

            if (in_array($user_id_session, $user_id) == true ) {
                $this->template->assign_vars(array(
                    'PONTUAR_BUTTON'    => $this->user->lang('UPDATE'),
                ));
            }else {
                $this->template->assign_vars(array(
                    'PONTUAR_BUTTON'    => $this->user->lang('SEND'),
                ));
I am not able to list all the values ​​of the array user_id (database).

By doing an echo, it returns me only the value of the first line. How do I go through all the values ​​found to be able to match with the user_id_session?

Code: Select all

 if (in_array($user_id_session, $user_id) == true ) {
                $this->template->assign_vars(array(
                    'PONTUAR_BUTTON'    => $this->user->lang('UPDATE'),
                ));

User avatar
caiocald
Registered User
Posts: 60
Joined: Mon Feb 26, 2018 9:32 pm
Location: Brasil
Name: B!
Contact:

Re: $this->user->data['user_id']) does not work on the online server

Post by caiocald » Wed Jun 19, 2019 12:12 am

Code: Select all

public function display_average_score_in_viewtopic_body_page($event)
    {
        $this->user->add_lang('ucp');
        $this->user->add_lang_ext('red1000/pontuacao', 'common');

        $sql = 'SELECT COUNT(*) AS number_of_rows , avg(comp1) as comp1 , avg(comp2) as comp2 , avg(comp3) as comp3 , avg(comp4) as comp4 , avg(comp5) as comp5 , user_id as user_id
                FROM ' . $this->pontuacao_enem_table . '
                WHERE topic_id = ' . $this->request->variable('t', 0);

        $result = $this->db->sql_query($sql);

        while ($row = $this->db->sql_fetchrow($result))
        {
            $number_of_rows = $row['number_of_rows'];
            $comp1 = Round($row['comp1'], 0);
            $comp2 = Round($row['comp2'], 0);
            $comp3 = Round($row['comp3'], 0);
            $comp4 = Round($row['comp4'], 0);
            $comp5 = Round($row['comp5'], 0);
        }
        $this->db->sql_freeresult($result);

        if ($number_of_rows >0)
        {
            $sql = 'SELECT user_id as user_id
                FROM ' . $this->pontuacao_enem_table . '
                WHERE topic_id = ' . $this->request->variable('t', 0);
            $result = $this->db->sql_query($sql);
            $user_id = $this->db->sql_fetchrowset($result);
            $user_id_session = $this->user->data['user_id'];
            $this->db->sql_freeresult($result);

            foreach ($user_id as $user_ids):

                if ($user_ids['user_id'] == $user_id_session) {
                    $this->template->assign_vars(array(
                        'PONTUAR_BUTTON' => $this->user->lang('PONTUACAO_ENEM_PONTUAR_ATT'),
                    ));
                    break;


                }elseif ($user_ids['user_id'] != $user_id_session){
                    $this->template->assign_vars(array(
                        'PONTUAR_BUTTON' => $this->user->lang('PONTUACAO_ENEM_PONTUAR'),
                    ));

                }
            endforeach;
            

I can solve this by creating a new query in the database for users (user_id)

Code: Select all

 $sql = 'SELECT user_id as user_id
                FROM ' . $this->pontuacao_enem_table . '
                WHERE topic_id = ' . $this->request->variable('t', 0);
            $result = $this->db->sql_query($sql);
            $user_id = $this->db->sql_fetchrowset($result);
            $this->db->sql_freeresult($result);
And adding a foreach

Code: Select all

 foreach ($user_id as $user_ids):

                if ($user_ids['user_id'] == $user_id_session) {
                    $this->template->assign_vars(array(
                        'PONTUAR_BUTTON' => $this->user->lang('PONTUACAO_ENEM_PONTUAR_ATT'),
                    ));
                    break;


                }elseif ($user_ids['user_id'] != $user_id_session){
                    $this->template->assign_vars(array(
                        'PONTUAR_BUTTON' => $this->user->lang('PONTUACAO_ENEM_PONTUAR'),
                    ));

                }
endforeach;
            
I'm not sure if this is the best way or if there is any way to do this in a single query in the database.
But the conflicts between the sql_fetchrowset and sql_fetchrow functions did not let me unify them

thanks!

Post Reply

Return to “Extension Writers Discussion”