Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用查询结果在HTML表中生成链接。(编码点火器)_Php_Mysql_Codeigniter_Hyperlink_Html Table - Fatal编程技术网

Php 使用查询结果在HTML表中生成链接。(编码点火器)

Php 使用查询结果在HTML表中生成链接。(编码点火器),php,mysql,codeigniter,hyperlink,html-table,Php,Mysql,Codeigniter,Hyperlink,Html Table,我试图在表视图中显示两列,一列是文档的标题,另一列是文档的说明。在我选择的特定表中有一列名为“filename”,它存储上传文档的名称,该名称与文档的标题和描述关联 我很好奇,当我将“文件名”列中包含的数据设置为标题的超链接值时,如何只显示标题和描述?基本上,我希望他们能够下载一个文件,一旦他们点击它的名字 我相当肯定,我可以通过跳过表生成器并在视图中执行“foreach”来手动完成这项工作,以打印出结果集中的所有数据,但我愿意接受建议,因为这会导致代码马虎。下面是我的控制器的一个片段 <

我试图在表视图中显示两列,一列是文档的标题,另一列是文档的说明。在我选择的特定表中有一列名为“filename”,它存储上传文档的名称,该名称与文档的标题和描述关联

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

我相当肯定,我可以通过跳过表生成器并在视图中执行“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);
    }
} 

您可以使用select->CONCAT…,FALSE查询在查询中创建链接

**更新** 那么,您的表助手将接收一个数组,而您的db查询将返回一个数组。因此,您必须以一种创建数组的方式创建查询,该数组的创建方式与表帮助器所需的方式相同

你需要这样的东西:

$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));
您的查询可能如下所示:

$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函数将有一些“quote”问题。如果没有亲自测试,我不确定我的引语是否正确。看看那里的一些文档,这应该会有所帮助:

请在模型中进行查询我知道这是一个最佳实践,但我只是在这个实例中构建了几个小的静态页面。对于我上面提到的目的,您建议我在模型中放置查询,是否有任何特定的原因?如果是这样的话,你能详细说明一下吗?感谢你提供的信息,伙计;这是非常有帮助的。我现在明白了如何使用它来创建链接,但我不明白如何设置链接的文本以及它从数据库引用的文档的描述?没关系,我已经准备好了。谢谢你的帮助,学到了很多!