Welcome Guest, Not a member yet? Register   Sign In
Showing results from a specific user
#1

[eluser]Unknown[/eluser]
i am having an issue with displaying results that are specific to a user, like timesheet entries

Controller:
Code:
class Internal extends Controller {
    
    function Internal()
    {
        parent::Controller();
        $this->load->helper('date');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->model('entry_model');
        session_start();
    }

    function index()
    {
        $userlist = $this->session->userdata('username');
        $data['entry'] = $this->db->get('entries');
        $data['client'] = $this->db->get('clients');
        $data['job'] = $this->db->get('jobs');
        $data['task'] = $this->db->get('tasks');
        $data['entry'] = $this->session->userdata('username');
        $data['user_results'] = $this->entry_model->getResults($userlist);
        
        $this->load->view('internal_view', $data);
        
        if ($this->session->userdata('logged_in') != TRUE)
        {
            redirect('login');
        }
        
        
    }
    
    function time_insert()
    {
        $this->db->insert('entries', $_POST);
        redirect('internal');
    }
    function time_edit()
    {
        $this->load->helper('date');
        $this->db->where('entryId', $this->uri->segment(3));
        $data['entry'] = $this->db->get('entries');    
        $this->load->view('edit_time_view', $data);
    }
    function time_update()
    {
        $this->load->helper('date');
        $this->db->where('entryId', $_POST['entryId']);
        $this->db->update('entries', $_POST);
        redirect('internal');
    }
    function time_delete($id)
    {
        $this->db->where('entryId', $id);
        $this->db->delete('entries', $this->entries);
        redirect('internal');
    }

}

Model:

Code:
class Entry_model extends Model {

    function Entry_model()
    {
        parent::Model();
    }

    function getResults ($userlist, $usertimes = TRUE)
    {
        $this->db->like('user', $userlist);
        $this->db->orderby('e_date');
        $query = $this->db->get('entries');
        
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $time) {
                if ($usertimes) {    
                    $output = '<div class="sumEntry">';
                    $output .= '<div class="sumDate">' . $time->e_date . '</div>';
                    $output .= '<div class="sumClient">' . $time->client . '</div>';
                    $output .= '<div class="sumJob">' . $time->jobNumber . '</div>';
                    $output .= '<div class="sumTask">' . $time->task . '</div>';
                    $output .= '<div class="sumHours">' . $time->hours . ':' . $time->minutes .'</div>';
                    $output .= '<div class="sumEdit">edit entry link</div>';
                    $output .= '<div class="sumEdit">delete entry link</div>';
                    $output .= '</div>';
                }
            }
            return $output;
        } else {
            return '<p>Sorry, no results returned.</p>';
        }
    }
}

How it is initiated:

The user logs in and the username is held as a session variable, the user then adds their time for the day and the entry appears in the list.
As more entries are added the list gets longer and only shows that users times, other users times will only be viewable by them.


What actually happens:

user logs in and starts adding entries but only the lasts entry appears in the list for that user.



So the login works and the user specific data appears but the foreach loop seems no to be working.

If i remove the model and just use the foreach in the view it works but shows all users data, ideal for an administrator but not for the users.

please if you are able to help i have 2 other projects that could benifit from this solution also.


Many thanks


Mark
#2

[eluser]Nick_MyShuitings[/eluser]
Your problem is here:

Quote:foreach ($query->result() as $time) {
if ($usertimes) {
$output = '<div class="sumEntry">';

in the first line you are overwriting. I'd suggest $output[$i] = ... that way you can run a quick foreach in your view... either that or in your model function you need to declare $output = ''; first and then change the quoted line to .=
#3

[eluser]Unknown[/eluser]
Home run all the way. thanks so much.

looking a a previous search function i used the code

Code:
$output = '';


is there but has a table tag nested in it.

you are a star


thanks

Mark
#4

[eluser]Calvin Froedge[/eluser]
Hi Mark,

Just to provide a pointer (hope this is helpful). You might consider separating your code so that your model only contains the query (and not any HTML tags) and returns the query results to the controller. Then, you can pass that data to the view. You could probably thin down your model to something like this:

Code:
function getResults ($userlist, $usertimes = TRUE)
    {
        $this->db->like('user', $userlist);
        $this->db->orderby('e_date');
        $query = $this->db->get('entries');
        
        if ($query->num_rows() > 0) {
            return $query;
        } else {
            $error = 'Sorry, no results returned.';
            return $error;
        }
    }
#5

[eluser]InsiteFX[/eluser]
Also you do not need the session_start();

CodeIgniter sessions starts it.

Make sure you load the session library.

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB