Elliott Elliott - 1 year ago 59
PHP Question

Generating Links Within a HTML Table Using Query Results. (Codeigniter)

I’m trying to display two columns in my table view, one being a title of the document, the next being a description of the document. I have a column in the particular table which I am selecting named “filename” that stores the name of the uploaded document which is associated with it’s title and description.

I’m curious as to how I would manage to display only the title and description, while setting the data contained in the “filename” column as the hyperlink value of the title? (Basically, I’m wanting them to be able to download a document once they click on it’s name)

I’m fairly certain that I can pull this off by manually by skipping the table generator and doing a “foreach” in the view to print out all the data from the resultset, but I’m open to suggestions, as this would make for sloppy code. Here’s a snippet of my controller below.

class blah extends CI_Controller {
public function troubleshooting() {

$config['base_url'] = 'http://somewebsite.com/troubleshooting';
$config['total_rows'] = $this->db->get('document')->num_rows();
$config['per_page'] = 10;
$config['num_links'] = 10;
$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);

Answer Source

You can create the link in your query with a select->(CONCAT(...), FALSE) query.


** UPDATE ** Well, your table helper is going to receive an array - and your db query is going to return an array. So, you have to create your query in a way that will create your array the way you need it for the table helper.

You need something like this:

$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

Your query might look like this:

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

The trick is your CONCAT function will have some 'quote' issues. Without actually testing this myself, I'm not sure I've got the quotes right. Take a look at some docs there and that should help: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download