Welcome Guest, Not a member yet? Register   Sign In
Table class question
#1

[eluser]umbongo[/eluser]
Can the table class create links within the table??

I am loading an array (from a database) with the table class. This contains the text values i want in my table. Can I add url links with the table class so that i can have the table contents link to URLs dependent on their value (//how?) or do i have to do it without using the table class??
#2

[eluser]pickupman[/eluser]
Yes, and yes.
Code:
$this->load->helper('url');
$this->load->library('table');

$this->table->set_heading('ID', 'Title', 'Link');

foreach($items as $row)
{
  $this->table->add_row($row->id, $row->title, anchor('page/'.$row->id, 'View'));
}

echo $this->table->generate();

//Or use this foreach loop
<table>
  <thead>
    <tr>
      <td>ID</td>
      <td>Title</td>
      <td>Link</td>
    </tr>
   </thead>
   <tbody>
     &lt;?php foreaach($items as $row) :?&gt;
      <tr>
         <td>&lt;?php echo $row->id;?&gt;</td>
         <td>&lt;?php echo $row->title;?&gt;</td>
         <td><a >id;?&gt;">View</a></td>
      </tr>
      &lt;?php endforeach;?&gt;
   </tbody>
  </table>
So it's about 5 lines of code using the table class, but all php. Or its several lines of html with a little bit of php. Depends on people's opinion of how much php should be in views.

EDIT: Some reason my code is being butchered.
#3

[eluser]umbongo[/eluser]
thanks!

how much of the top code should be in views VS controllers??
#4

[eluser]pickupman[/eluser]
That's the call of the project. All projects I handle, I've got a design given to me, and I make sure the views are look as given. Some code the site first and don't want very much php in the views, so designers manipulate the views and try not screw things up.

It boils down to, what works best for you, and what will be the easiest to maintain going forward. I treat it similar to how themes are done in wordpress. I'll use as much html I can in the views, and echo a view fields out for the content in a foreach type loop. I try to prep/run helpers in the controller that the fields can be outputted directly in a view.
Code:
//Controller
$data['link'] = anchor('view/'.$id,'Read More');
$this->load->view('some_view', $data);

//View
<p>&lt;?php echo $link;?&gt;</p>




Theme © iAndrew 2016 - Forum software by © MyBB