views:

2348

answers:

3

I'm using the table class that autogenerates a table for me from an array of data from my database.

So in my model I have:

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

In my controller I have:

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

( this 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, say, 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, more or less ) and add whatever links I want, but I'd love to be able to use the autogeneration as much as possible, and there's got to be a way to do something as common as linking a table cell.

Any ideas?

Thanks!

EDIT: ( and edited again to improve the counting method )

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

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

A: 

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();
FryGuy
+1  A: 

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.

Click Upvote
Thanks, that's mostly right. The only change I had to make was as posted in my edited question.
Ian
Great :) Always happy to help another codeIgniter
Click Upvote
A: 

Thanks for the code.

sabmark