Jeann Pierre Jeann Pierre - 1 year ago 78
PHP Question

How to Pass ID variable to controller in CodeIgniter

I have a list of messages and when the click on the title it takes them to another view where they can see the expanded message.

This is the view from which I click the link.

Postings View.

<a href="<?=base_url();?>index.php?/Message/display<?=$row['id']?>">Link</a>

Message Controller:

class Message extends CI_Controller {

var $TPL;

public function __construct()

private function display()

$query = $this->db->query("SELECT FROM messages WHERE id = '$id';");

$this->TPL['message'] = $query->result_array();

$this->template->show('Message', $this->TPL);


public function index()



Message View

<? foreach ($threads as $row) { ?>
<div class="row">
<div class="message">
<h3><?= $row['title']?></h3>
<p><?= $row['message']?></p>
<p><?= $row['member']?></p>
<? $int++;?>
<? } ?>

Answer Source

This is easy. Change your code as follwoing

<a href="<?php echo site_url('message/display').'/'.$row['id'];?>">Link</a>

Then change you display method as public and send ID param there like

public function display($id){
    $this->db->where('id', $id);
    $query = $this->db->get('messages');
    $this->TPL['message'] = $query->result_array();
    $this->template->show('Message', $this->TPL);

And finally remove $int=0 & $int++; from view file as you are not using this. Now test

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