Taylor
Taylor

Reputation: 3141

Codeigniter - The Next button changes the value and data on the page, but it the "active" class doesn't change

What I'm trying to do is create a pagination function. I have successfully created that. It works. The URI segment changes and everything. It's perfect. However, there is a small problem. When I press the "next" button or press another number to go to the next page, the "page 1" is still highlighted for some reason. I can't click it to go to the first page. The "Previous" button show up either. So after I go to a second page or any other page, other than 1, I cannot get back to the first page. The only way I can go back to the first page is by changing the URI segment.

I couldn't find an answer to this specific question, so any help is highly appreciated. Thanks.

My Model:

public function record_count_sitelog()
{
    $uid = $this->uri->segment(4);
    return $this->db->get_where('site_log', array('uid' => $uid))->num_rows();
}

public function fetch_users_sitelog($limit, $start)
{
    $this->db->order_by("time", "desc"); //ordering it in descending order
    $uid = $this->uri->segment(4);
    $this->db->limit($limit, $start);

    $query = $this->db->get_where('site_log', array('uid' => $uid));

    if ($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }
    return false;
}

My Controller:

public function site_log() {
    $uid = $this->uri->segment(4);

    $data = array( 'uid' => $uid
    );

    $this->load->model('Model_Admin_Pagination');
    $this->load->library('pagination');
    $this->load->library('table');

    $config = array();
    $config["base_url"] = base_url() . "admin/users/site_log/". $uid ."/";
    $this->db->select('*');
    $config["total_rows"] = $this->Model_Admin_Pagination->record_count_sitelog();
    $config["per_page"] = 20;
    $config["uri_segment"] = 4;
    $config["num_links"] = 3;
    $config['records'] = $this->db->select('*');
    //styling it
    $config['full_tag_open'] = '<div class="pull-right"> <ul class="pagination">';
    $config['full_tag_close'] = '</div></ul>';
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';
    $config['next_link'] = '→';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['prev_link'] = '←';
    $config['cur_tag_open'] = '<li class="active"><a>';
    $config['cur_tag_close'] = '</li></a>';
    $config['prev_tag_open'] = '<li>';
    $config['prev_tag_close'] = '</li>';
    $config['last_link'] = '»';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $config['first_link'] = '«';
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    //end styling it

    $page = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
    $data["results"] = $this->Model_Admin_Pagination->
    fetch_users_sitelog($config["per_page"], $page);
    $data["links"] = $this->pagination->create_links();

    $this->pagination->initialize($config);

    $this->load->view('admin/includes/header');
    $this->load->view('admin/view_site_log', $data);
}

My View:

    <?php
if (is_array($results))
{
    foreach ($results as $data) { ?>
        <div> <!--class="col-md-6 col-sm-6"-->
            <div class="well pretty-text">
                <p>
                    <h3><font color="#708090">Time:</font> <?php echo date("g:i A", strtotime($data->time)). ' on ' . date("F j, Y", strtotime($data->time)) ?><br></h3>
                    <b>Current URL:</b> <?php echo $data->current_url ?><br>
                    Referrer: <?php echo $data->referrer ?><br>
                    <b>Browser:</b> <?php echo $data->browser ?><br>

                    Mobile: <?php echo $data->mobile ?><br>
                    <b>Platform:</b> <?php echo $data->platform ?><br>
                <b>User Agent:</b> <?php echo $data->user_agent ?>

                </p>
            </div>
        </div>
    <?php } } ?>

<?php  echo $this->pagination->create_links(); ?>

Upvotes: 1

Views: 1588

Answers (1)

user2453646
user2453646

Reputation: 104

You said that your uid is $this->uri->segment(4);. If that's segment(4), then in your controller, you said $config["uri_segment"] = 4;

If your URL is something like: http://example.com/profile/view/1/10 where 1 is the uid, then you should change the 4 in $config["uri_segment"] = 4; to 5.

So I would replace this line (in your controller):

$config["uri_segment"] = 4;

With:

 $config["uri_segment"] = 5;

Upvotes: 1

Related Questions