Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter Table Class: Add a Link From a Generated Cell

I'm using the table class that auto-generates a table for me from an array of data pulled from my database.

Model:

function get_reports_by_user_id($userid)
{
    return $this->db->get_where('ss2_report',array('userid' => $userid))->result_array();
}

Controller:

function index()
{
    echo $this->table->generate($this->mymodel->get_reports_by_user_id('1234'));
}

The controller will eventually be moved to a view when I have it working. This generates the table just fine, but I'd like to add a link to a field. For example, the id column that would allow me to link to a page of data for just that report's id. I know I can just output the table the old fashioned way by hand. I can then add whatever links I want, but I'd love to be able to use the auto-generation as much as possible. There's got to be a way to do something as common as linking a table cell. Does anyone have any ideas?

EDIT:

User Java PHP has it mostly right below. Here's the code that makes it work:

function get_reports_by_user_id($userid)
{
    $rows = $this->db->get_where('ss2_report',array('userid' => $userid))->result_array();

    foreach ($rows as $count => $row)
    {
        $rows[$count]['id'] = anchor('report/'.$row['id'],$row['id']);
    }
    return $rows;
}

I just needed to replace the value in the original array with the anchor text version.

like image 773
Ian Avatar asked Mar 02 '23 02:03

Ian


2 Answers

The only way is, in the function get_reports_by_user_id() , you would loop through all the results and add the <a href> tag to the ids. Something like this:

function get_reports_by_user_id($userid)
{
   $rows=$this->db->get_where('ss2_report',array('userid' => $userid))->result_array();
   foreach ($rows as $row)
   {
     $row->id=anchor('site.com/some_controller/some_function/'.$row->id,$row->id);
   }
   return $rows;
}

I don't use CodeIgniter's database library so I'm not sure of what format it returns $rows in, but the code above should give you the general idea of what you need to do.

like image 52
Ali Avatar answered Apr 01 '23 18:04

Ali


One idea might be to do something like..

foreach ($row in $this->mymodel->get_reports_by_user_id('1234'))
{
    $row->id = anchor(site_url(array('report', 'user', $row->id)), $row->id);
    $this->table->add_row($row);
}
$this->table->generate();
like image 38
FryGuy Avatar answered Apr 01 '23 18:04

FryGuy