CodeIgniter Forums
Generate Table from DB Query - Newbie question - Printable Version

+- CodeIgniter Forums (
+-- Forum: Archived Discussions (
+--- Forum: Archived Development & Programming (
+--- Thread: Generate Table from DB Query - Newbie question (/thread-18160.html)

Generate Table from DB Query - Newbie question - El Forum - 04-27-2009

Hi. Newbie here (let me know if I'm doing things right or wrong on this forum).

For a given rental order I need to display a list of associated rental items. Three classes (tables) are modeled as follows:

rental_items(item_id, item_desc, mfr_pn, mfr_sn)
rental_orders(order_id, customer, salesman)
rental_orms(orm_id, order_id, item_id)

The index() of the default rentals.php controller puts all the records from 'rental_orders' in data['pending'] and sends it to the summary.php view for use as $pending.

class Rentals extends Controller {

    function Rentals()

    function index()
        $this->load->view('summary', $data);

    function pending( $order=0 )
        // get the requested pending rental order (gets a single record)
        $data['pending']=$this->db->get_where('rental_orders', array('order_id =' => $order));
        // get all the records from 'rental_orms' that have order_id=$order.  
        // this builds the list of item_id records to fetch from the 'rental_items' table
        $data['orms']=$this->db->get_where('rental_orms', array('order_id =' => $order));
        // calculate the list of rental items to display on the pending rental order page
        foreach( $data['orms']->result_array() as $itemq )
            $data['items']=$this->db->get_where('rental_items', array('item_id =' => $itemq['item_id']));
        $this->load->view('pending', $data);

When the requested url is
the value 2 is passed to the method called pending() in the rentals.php controller. The method pending() queries 'rental_orms' to retrieve all records having the user-selected order_id. The resulting records are stored in data['orms'].

Now my problem is here. Instead of generating the table of records from 'rental_orms', I want to generate a table of records from 'rental_items', based on the same list of returned item_id values from the previous query of 'rental_orms'.

In my pending.php view (not shown) $items only contains the last expected record. In my controller code above, how do I make data['items'] include all the records before my call to the pending.php view??

Thanks for any advice.

Sean Loving

Generate Table from DB Query - Newbie question - El Forum - 04-28-2009

Welcome to the CodeIgniter forums.

If I understand you correctly, this is what you need:
$data['items'][]=$this->db->get_where('rental_items', array('item_id =' => $itemq['item_id']));

The way it's done currently, it overwrites the $data['items'] variable. By appending a set of empty square brackets, $data['items'] is treated as an array, and the new database result is pushed onto the end of that array.

Hope this helps.

Generate Table from DB Query - Newbie question - El Forum - 04-29-2009

Yes that helps, thanks.
Seems like I'm still learning the difference between objects and arrays, and in CI I'm trying to understand when to use result() and result_array(). I saw the post on "num_rows() always showing 0", but I think OO might be my problem. I will explore in another post...