2011-10-07 41 views
0

我想在我的表格视图中显示两列,一列是文档的标题,另一列是文档的描述。我在特定的表中选择了一个名为“文件名”的列,该列存储与其标题和描述相关联的上载文档的名称。使用查询结果在HTML表格中生成链接。 (Codeigniter)

我很好奇我将如何设置包含在“文件名”列中的数据作为标题的超链接值时只显示标题和说明? (基本上,我希望他们能够下载一个文件,一旦他们点击它的名称)

我相当肯定,我可以通过跳过表生成器和做一个“foreach”以便打印出结果集中的所有数据,但我愿意接受这些建议,因为这会造成草率的代码。下面是我的控制器的一个片段。

<?php 
class blah extends CI_Controller { 
    public function troubleshooting() { 
     $this->load->library('pagination'); 
     $this->load->library('table'); 

     $config['base_url'] = 'http://somewebsite.com/troubleshooting'; 
     $config['total_rows'] = $this->db->get('document')->num_rows(); 
     $config['per_page'] = 10; 
     $config['num_links'] = 10; 
     $this->pagination->initialize($config); 
     $data['records'] = $this->db->get('document', $config['per_page'],$this->uri->segment(3)); 
     $this->db->select('doc_title, filename, description, category_id, product_id'); 
     $this->db->where('category_id = 1'); 
     $this->db->where('product_id = 1'); 
     $this->db->order_by('doc_title', 'asc'); 
     $this->load->view('blah/troubleshooting.php', $data); 
    } 
} 
+0

请在模型中进行查询 –

+0

我明白这是一种最佳做法,但我只是在此实例中构建了几个小静态页面。是否有任何特定的原因具体到我上面提到的目的,您建议我在模型中放置查询?如果是这样,你能否详细说明一下? – Elliott

回答

0

您可以使用select->(CONCAT(...), FALSE)查询在查询中创建链接。

http://codeigniter.com/forums/viewthread/105687/#531878

**更新** 那么,你的表助手将要接收的阵列 - 和你的数据库查询将返回数组。所以,你必须以一种方式创建你的查询,以便以你需要的方式为表助手创建你的数组。

你需要的东西是这样的:

$records = [ 
    'doc_title' => 'My Title', 
    'filename' => '<a href="path/to/filename.php">filename.php</a>', 
    'description' => 'My description text here.', 
    'category_id' => 5, 
    'product_id' => 56 
] 

您的查询可能是这样的:

$this->db->select('doc_title'); 
$this->db->select('CONCAT("<a href=path/to/'.filename.'>".'filename'."</a>")', FALSE); 
$this->db->select('description, category_id, product_id'); 
$this->db->where('category_id = 1'); 
$this->db->where('product_id = 1'); 
$this->db->order_by('doc_title', 'asc'); 

$records = $this->db->get('document', $config['per_page'],$this->uri->segment(3)); 

关键是你的CONCAT功能会有一些 '报价' 的问题。如果没有亲自测试,我不确定我是否有正确的引用。看看那里的一些文档,这应该有所帮助:http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat

+0

欣赏信息,男人;这是非常有用的。我现在明白我如何使用它创建链接,但我不明白我如何设置链接的文本以及它从数据库引用的文档的描述? – Elliott

+0

我会用我的回复更新我的回答... – swatkins

+0

没关系,让它去交配。感谢您的帮助,学到了很多! – Elliott