Welcome Guest, Not a member yet? Register   Sign In
Use a query for charts in a view (user id) (NEW TO CODEIGNITER)
#1

Hello friends,

First of all i would like to apologize for maybe asking stupid questions, I am new to codeigniter and php/sql in general.

I am using a script called Cicool that i purchest on codecanyon.
so far so good, its really easy to make a crud and edit the style of the view to personalize the look and implement my own layout.
but i would like to make a view charts based on the date from users this to make a nice overview of their own data.

I have tried to do this in de view because i thought that would be easy to use it elsewhere if needed. (like a snippet)
with Cicool you have the option to add a users id to a table and then only show their own data from the database.
this works fine but when i use my own query it sums op data from all the rows, so also the data of other people.
so the query works, but not as supposed to.

In my view:
Code:
<?php
   if (!$this->aauth->is_admin()) {
      $this->db->where($this->table_name.'.log_user', get_user_data('id'));
      $urenlog = $this->db->query ('SELECT  SEC_TO_TIME( SUM( TIME_TO_SEC( `log_totaltimeflight` ) ) ) AS totaaluren FROM app_log');
  }
foreach ($urenlog->result() as $row)
{

          echo $row->totaaluren;
}
?>

I know that i am doing this all wrong and i should use the MVC where it is for.
But i lack to much knowledge to figure out this myself.

Also i am not asking for a copy past solution, I would like to learn and figure things out but for now i am stuck :-(

Sorry if the English doesn't make sense, i don't write that lot of English ;-)

Best regards,
Reply
#2

First of all, you said it yourself, you should never make database queries directly in the view. It will work but your code will become an unmaintainable mess in no time.

Now for your problem :
Quote:with Cicool you have the option to add a users id to a table and then only show their own data from the database.
this works fine but when i use my own query it sums op data from all the rows, so also the data of other people.
so the query works, but not as supposed to.

Look at the actual data in the database with phpMyAdmin or any other tool and make sure the user id is what you expect it to be. Then, make sure get_user_data('id') returns the current user id. You can also run the query in phpMyAdmin to confirm it returns the data you are expecting.

Are you logged in as admin? Because your query is only executed if you are a normal user:
PHP Code:
if (!$this->aauth->is_admin()) 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(01-02-2020, 05:27 AM)includebeer Wrote: First of all, you said it yourself, you should never make database queries directly in the view. It will work but your code will become an unmaintainable mess in no time.

Now for your problem :
Quote:with Cicool you have the option to add a users id to a table and then only show their own data from the database.
this works fine but when i use my own query it sums op data from all the rows, so also the data of other people.
so the query works, but not as supposed to.

Look at the actual data in the database with phpMyAdmin or any other tool and make sure the user id is what you expect it to be. Then, make sure get_user_data('id') returns the current user id. You can also run the query in phpMyAdmin to confirm it returns the data you are expecting.

Are you logged in as admin? Because your query is only executed if you are a normal user:
PHP Code:
if (!$this->aauth->is_admin()) 

Hey includebeer,
First of all thanks for your replay, i appreciate it.
Sorry for the late response i was abroad for a few days.
I have reviewed my work behavior and i did a few things different, I have it now as Followed.

In my Model:
Code:
  function get_data_count()
    {
        $urenlog = $this->db->query ('SELECT  SEC_TO_TIME( SUM( TIME_TO_SEC( `log_totaltimeflight` ) ) ) AS timeSum FROM app_log');
        $result = $urenlog->result();
        return $result;
    }


// The filter is still in place like this. (this works for the normal tables that are produced so i assume there is nothing wrong here.

    public function filter_avaiable() {

        if (!$this->aauth->is_admin()) {
            $this->db->where($this->table_name.'.log_user', get_user_data('id'));
        }

        return $this;
    }


In my Controller:
Code:
    function get_data_count(){
        $data = $this->Model_app_app->get_data_count()->result();
        $x['data'] = json_encode($data);
        
        $this->render('backend/standart/administrator/app_log/app_log_list', $x);
      }
And in my View a simple php line:
Code:
<?php echo $data;?>

I have the feeling that i am close, but still no cigar.
The controller and model are communicating as of the rest of the page is working but not my query :-(

The view is still working as normal it just does not show/output any data. (no obvious errors, but maybe i am missing something (Codeigniter is in production mode/envirement))
Reply




Theme © iAndrew 2016 - Forum software by © MyBB