【问题标题】:Insert link attribute to tha data table of codeigniter pagination将链接属性插入到codeigniter分页的数据表中
【发布时间】:2015-05-11 09:34:16
【问题描述】:

这是我第一次使用codeigniter的分页,这是我在那里找到的最多的教程:

这是“Perusahaan”方法上的控制器,我将在这里调用分页函数。

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

    }

在模型中,我从数据库中获取数据:

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;          
    }

现在问题出在视图中:

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

即使它按预期成功获取数据,但我无法从getCompanyList 的行结果中插入指向每个名称的链接。

如何修改表中的数据以便我可以为每个名称提供a href 链接?

谢谢。



例如,从上面的查询中,我得到分页中的数据循环,如下所示:

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

但是我想为每个名字插入一个链接:

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

因为我确实通过以下方式回显数据:

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

我不知道如何插入指向每个$name 的链接。

【问题讨论】:

  • 可以在配置文件中修改。 link

标签: php codeigniter


【解决方案1】:

像这样转换你的模型函数。

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;
}

我认为它会起作用

【讨论】:

    【解决方案2】:

    您可以创建一个函数来重新格式化行值

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

    在视图中你可以像这样使用

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 2013-10-28
      • 2011-04-29
      • 1970-01-01
      • 2013-06-13
      相关资源
      最近更新 更多