CodeIgniter Forums
PHP practices in CodeIgniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: PHP practices in CodeIgniter (/showthread.php?tid=14522)



PHP practices in CodeIgniter - El Forum - 01-06-2009

[eluser]XORd[/eluser]
Hi, folks! I am very excited about CI since the very first time it came out.
However, today my excitement was disturbed by the fact that I was not able to perform a
moderately complex output to my view - something that I would normally do this way (using my favourite ez_sql database class). I have the corresponding CI instructions as comments.

Code:
<?php

$items = $db->get_results("SELECT * FROM items"); // $query=$this->db->get('items');
                                                  // $data['items']=$query->result();
                                                  // $this->load->view('itemsview', $data);

$output = '<table>
              <tr>
                 <td>ID</td>
                 <td>Name</td>
                 <td>Lat Added SubItem</td>
              </tr>';

foreach($items as $item)                          
{
  $last_subitem = $db->get_row("SELECT *                  // gets a single row from the db
                                FROM sub_items            
                                WHERE item='$item->id'    // Ok, this should happen in the
                                ORDER BY datetime DESC"); // view; I am not quite confident
                                                          // about its implementation in CI

                                                         // the foreach loop happens in
                                                         // my view. How do I get the
                                                         // corresponding sub_item of
                                                         // each main item before the view?
  $output .= "<tr>
                  <td>$item->id</td>
                  <td>$item->name</td>
                  <td>$last_subitem->name</td>
              </tr>";
}

$output .= '</table>';

echo $output;

?&gt;

Please, give me an advice on the implementation of such a practice in CI.

Thank you for you time to read through the code!



--
XORd


PHP practices in CodeIgniter - El Forum - 01-06-2009

[eluser]jalalski[/eluser]
I'm missing the context of the above code. Is it in a controller? What errors are you getting? How is your $db getting initialized and is it an instance of CI's database layer? If this is a view, why are you doing the database stuff there and not in the controller/model layer?

In general, if you have a problem, give as much detail as you can...


PHP practices in CodeIgniter - El Forum - 01-06-2009

[eluser]Dready[/eluser]
Hello ! , let me try... ;-)

In the controller :

Code:
$this->load->view('item_table_begin');

$items = $this->db->get('items')->result();
foreach ( $items as $item ) {
    $last_subitem = $this->db->where('item',$item->id)->order_by('datetime','desc')->get('sub_items')->row();
    $this->load->view('item_table_row',array('item'=>$item,'last_subitem'=>$last_subitem));
}
$this->load->view('item_table_end');

In 'item_table_begin.php' view file :

Code:
<table>
              <tr>
                 <td>ID</td>
                 <td>Name</td>
                 <td>Lat Added SubItem</td>
              </tr>

In 'item_table_row.php' view file :

Code:
<tr>
                  <td>&lt;?= $item->id ?&gt;</td>
                  <td>&lt;?= $item->name ?&gt;</td>
                  <td>&lt;?= $last_subitem->name ?&gt;</td>
              </tr>

and in 'item_table_end.php' view file :

Code:
</table>

That's one of the possibilities. You can also prepare all item/subitem pairs (for example in an array) and loop directly inside the view : see Creating loops paragraph in the user guide chapter about views. Doing this you'll only need one view file.


PHP practices in CodeIgniter - El Forum - 01-06-2009

[eluser]Popcorn[/eluser]
To do that in CodeIgniter, the SQL or any data related functions would go into the model. This keeps it nicely separated from the rest of the code. The (x)HTML should always go into a view file.

I wouldn't take your approach Dready as it seems a bit much, but rather do something along the lines of :
Code:
<table>
  <thead>
    <tr>
      <th>Title</th>
    </tr>
  </thead>
  <tbody>
    &lt;?php foreach($variable as $row): ?&gt;
    <tr>
      <td>&lt;?php echo $row->username; ?&gt;</td>
    </tr>
    &lt;?php endforeach; ?&gt;
</table>



PHP practices in CodeIgniter - El Forum - 01-06-2009

[eluser]XORd[/eluser]
@jalalski : well, like I said, in this example I was using a database wrapper calles Ez_SQL by Justin Vincent. It is very convenient and drops my dev time significantly.

@Dready : Thank you for the accurate coding! Wink Your code produces exactly what I need.
However, I am a little disturbed by the fact that I need to have chunks of views here and there. I think I'm going to combine your method with Popcorn's.

@popcorn : yes, separation. That's why I chose CI Wink

Thank you!


PHP practices in CodeIgniter - El Forum - 01-07-2009

[eluser]Dready[/eluser]
@XORd & @popcorn : Ok , because I'm in a good day ;-) , here is the "one view to rule dem all" version :

In the controller :

Code:
$data = array();
$items = $this->db->get('items')->result();
foreach ( $items as $item ) {
    $last_subitem = $this->db->where('item',$item->id)->order_by('datetime','desc')->get('sub_items')->row();
    $data[] = array('item'=>$item,'last_subitem'=>$last_subitem);
}
$this->load->view('items_table',array('items'=>$data));

And the 'items_table' view file :

Code:
<table>
              <tr>
                 <td>ID</td>
                 <td>Name</td>
                 <td>Lat Added SubItem</td>
              </tr>
&lt;? foreach ( $items as $one ) { ?&gt;
              <tr>
                  <td>&lt;?= $one['item']->id ?&gt;</td>
                  <td>&lt;?= $one['item']->name ?&gt;</td>
                  <td>&lt;?= $one['last_subitem']->name ?&gt;</td>
              </tr>
&lt;?}?&gt;
</table>

Have a nice coding day !