【问题标题】:Generating Links Within a HTML Table Using Query Results. (Codeigniter)使用查询结果在 HTML 表中生成链接。 (代码点火器)
【发布时间】:2011-12-02 23:35:29
【问题描述】:

我试图在我的表格视图中显示两列,一列是文档的标题,下一列是文档的描述。我在选择的特定表中有一个名为“文件名”的列,其中存储了与标题和描述相关联的上传文档的名称。

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

我相当确定我可以通过跳过表生成器并在视图中执行“foreach”以打印出结果集中的所有数据来手动完成此操作,但我愿意接受建议,因为这会导致草率的代码。下面是我的控制器的 sn-p。

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

【问题讨论】:

  • 请在模型中查询
  • 我知道这是一个最佳实践,但我只是在这个实例中构建了几个小的静态页面。是否有任何特定于我上面提到的目的的特定原因,您建议我在模型中放置查询?如果有,请您详细说明一下?

标签: php mysql codeigniter hyperlink html-table


【解决方案1】:

您可以使用select-&gt;(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

【讨论】:

  • 欣赏信息,伙计;这非常有帮助。我现在明白了如何使用它来创建链接,但我不明白如何设置链接的文本以及它从数据库中引用的文档的描述?
  • 没关系,去吧,伙计。感谢大家的帮助,学到了很多!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 2012-12-20
  • 2012-04-06
相关资源
最近更新 更多