0
votes

This is the first time I am using pagination of codeigniter, and this is the most tutorial I found out there:

This is the controller on the method of "Perusahaan", this is where I will call the pagination function.

function perusahaan() 
    {
        $this->load->library('pagination');
        $this->load->library('table');

        $this->load->model('info');

        $this->table->set_heading('ID', 'Perusahaan', 'Alamat', 'Email', 'Telephone') ; 

        $config["total_rows"] = $this->db->get('perusahaan')->num_rows();
        $config['base_url'] = 'http://localhost/femina_group/index.php/femina/perusahaan';
        $config['num_links'] = 5;
        $config["per_page"] = 5;
        $config["uri_segment"] = 3;
        $config["full_tag_open"] = '<div id="pagerr">'; 
        $config["full_tag_close"] = '</div>'; 

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

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

        $data['usaha'] = $this->info->getCOmpanyList($config["per_page"], $this->uri->segment(3));  

        $this->load->view('pages/perusahaan', $data); 

    }

In the model, I get the data from database:

function getCOmpanyList ($limit, $offset)
    {
        $this->db->select ('id, name, location, email, tlp');
        $this->db->from ('perusahaan');
        $this->db->limit($limit, $offset);
        $q = $this->db->get();
        /*
        if($q->num_rows() > 0)
            {
                foreach ($q->result() as $row)
                    {
                        $data[] = $row;
                    }
                return $data;
            }
        */
        return $q;          
    }

The problem now is in the View:

<div id="paginateTable">
    <?php 
    echo $this->table->generate($usaha);
    echo $this->pagination->create_links(); 
    ?>
</div>

Even though it successfully fetch the data as expected, but I can't inset a link to each name from the row result of getCompanyList.

How to modify that data in the table so that I can give it a href link for that each names?

Thank you.



For instance, from the query above I get the data looping in the pagination like this:

 <td>$name</td>
 <td>$address</td>
 <td>$email</td>
 <td>$tlp</td>

However I want to insert a link to each name:

 <td><a href="segment_3">$name</a></td>
 <td>$address</td>
 <td>$email</td>
 <td>$tlp</td>

Since I do echo the data by:

echo $this->table->generate($usaha);

I don't have any idea how to insert a link to each $name.

2
you can modify it in the config file. link - bekt

2 Answers

1
votes

Convert your model function like this.

function getCOmpanyList ($limit, $offset)
{
    $this->db->select ('id, name, location, email, tlp');
    $this->db->from ('perusahaan');
    $this->db->limit($limit, $offset);
    $results = $this->db->get()->result_array();
    foreach($results as &$result)
    {
        $result['name']='<a href="segment_3">'.$result['name'].'</a>';//change the name field as you wanted
    }
    return $results;
}

I think it will work

1
votes

you can create a function to reformat row values

function reformat($rows){
   foreach ($rows as $row)
   {
     $row->name=anchor('site.com/some_controller/some_function/'.$row->id,$row->name);
   }
   return $rows;
}

and in views you can use like this

echo $this->table->generate(reformat($usaha));