Page 1 of 1

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

Posted: Tue Jun 18, 2019 1:59 pm
by caiocald
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'),
                ));

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

Posted: Tue Jun 18, 2019 2:28 pm
by david63
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()

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

Posted: Tue Jun 18, 2019 7:29 pm
by caiocald
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.

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

Posted: Tue Jun 18, 2019 7:38 pm
by Brf
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

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

Posted: Tue Jun 18, 2019 7:43 pm
by caiocald
I tried with GROUP BY but it did not work :(

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

Posted: Tue Jun 18, 2019 8:03 pm
by caiocald
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

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

Posted: Tue Jun 18, 2019 8:10 pm
by Brf
nope. If you use "=", then it is an assignment, which is always true.

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

Posted: Tue Jun 18, 2019 9:27 pm
by caiocald

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'),
                ));

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

Posted: Wed Jun 19, 2019 12:12 am
by caiocald

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!