CodeIgniter Forums
Accessing records from 2 tables in a single view - 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: Accessing records from 2 tables in a single view (/showthread.php?tid=42858)



Accessing records from 2 tables in a single view - El Forum - 06-22-2011

[eluser]shailendra[/eluser]
I have controller dashboard.php which to fetch data from "bids" and "projects" table and display records from both the tables into dashboard_view.php. But in dashboard_view.php I get records from only "projects" table. How do I get records from 2 tables without using join??

The code for dashboard.php is

Code:
<?php
class Dashboard extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
    }

    function index()
    {
        $this->load->helper('url');
        $data1['query']= $this->db->where('user_id', 1);
        $data1['query'] = $this->db->get('projects');
        $this->load->view('dashboard_view',$data1);

        $data2['query']= $this->db->where('user_id', 1);
        $data2['query'] = $this->db->get('bids');
        $this->load->view('dashboard_view',$data2);
    }
}
?>


and the code for dashboard_view.php is


Code:
<table width="95%" border="0" align="center" cellpadding="4" cellspacing="0" class="user1_inner">
        <tr>
          <td class="sectionhead">Bids</td>
        </tr>
        <tr>
          <td>          
          &lt;?php foreach($query->result() as $row):?&gt;
          <p>&lt;?php echo $row->bid_id; ?&gt;</p>
          <p>&lt;?php echo $row->proposal_description; ?&gt;</p>
          &lt;?php endforeach;?&gt;
          </td>
        </tr>
      </table>
      <br>
      <table width="95%" border="0" align="center" cellpadding="4" cellspacing="0" class="user1_inner">
        <tr>
          <td class="sectionhead">Projects</td>
        </tr>
        <tr>
        <td height="40">
          &lt;?php foreach($query->result() as $row):?&gt;
          <p>&lt;?php echo $row->proj_id; ?&gt;</p>
          <p>&lt;?php echo $row->proj_name; ?&gt;</p>
          &lt;?php endforeach;?&gt;
        </td>
        </tr>
    <tr>
          <td>Back Next link</td>
        </tr>
        <tr>
          <td><b>No records</b></td>
        </tr>
      </table>



Accessing records from 2 tables in a single view - El Forum - 06-22-2011

[eluser]Seb[/eluser]
There is more than one way to do it, but your same dashboard view is called twice with the two datasets.

Try this instead:

Code:
&lt;?php
class Dashboard extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
    }

    function index()
    {
        $data['projects']= $this->db->where('user_id', 1)->get('projects')->result();
        $data['bids']= $this->db->where('user_id', 1)->get('bids')->result();
        $this->load->view('dashboard_view',$data);
    }
}
?&gt;

and this for the view:

Code:
<table width="95%" border="0" align="center" cellpadding="4" cellspacing="0" class="user1_inner">
        <tr>
          <td class="sectionhead">Bids</td>
        </tr>
        <tr>
          <td>          
          &lt;?php foreach($bids as $row):?&gt;
          <p>&lt;?php echo $row->bid_id; ?&gt;</p>
          <p>&lt;?php echo $row->proposal_description; ?&gt;</p>
          &lt;?php endforeach;?&gt;
          </td>
        </tr>
      </table>
      <br>
      <table width="95%" border="0" align="center" cellpadding="4" cellspacing="0" class="user1_inner">
        <tr>
          <td class="sectionhead">Projects</td>
        </tr>
        <tr>
        <td height="40">
          &lt;?php foreach($projects as $row):?&gt;
          <p>&lt;?php echo $row->proj_id; ?&gt;</p>
          <p>&lt;?php echo $row->proj_name; ?&gt;</p>
          &lt;?php endforeach;?&gt;
        </td>
        </tr>
    <tr>
          <td>Back Next link</td>
        </tr>
        <tr>
          <td><b>No records</b></td>
        </tr>
      </table>



Accessing records from 2 tables in a single view - El Forum - 06-23-2011

[eluser]shailendra[/eluser]
Thanks mate for your help.......It worked with your code.


Accessing records from 2 tables in a single view - El Forum - 06-24-2011

[eluser]shailendra[/eluser]
With reference to above code, I wanted to echo no. of records so I wrote this code
Code:
function index()
    {
    
      $data['projects']= $this->db->where('user_id', 1)->get('projects')->result();
      $data['bids']= $this->db->where('user_id', 1)->get('bids')->result();
      echo $projects->num_rows();//This line gives me an error
      $this->load->view('dashboard_view',$data);
    }


How do I count no. of records?


Accessing records from 2 tables in a single view - El Forum - 06-24-2011

[eluser]bEz[/eluser]
Undecided

http://ellislab.com/codeigniter/user-guide/database/active_record.html

Don't take this as an insult, but certain questions like this are in the User Guide.
There are questions which are a bit trivial, but not something like this.


Accessing records from 2 tables in a single view - El Forum - 06-24-2011

[eluser]adityamenon[/eluser]
Code:
$data['projects']= $this->db->where('user_id', 1)->get('projects')->num_rows();



Accessing records from 2 tables in a single view - El Forum - 06-24-2011

[eluser]shailendra[/eluser]
I'm not feeling offended........but I read this page on user guide....but could not find a solution.....also read CI forums...got "count_all" but it didnt help as it does does not work with "where" clause.

But I found solution myself
Code:
echo count($projects);

My question to you is - below line of code works in the Blog tutorial.Why doesn't it work in this case?

Code:
echo $projects->num_rows();



Accessing records from 2 tables in a single view - El Forum - 06-25-2011

[eluser]shailendra[/eluser]
This line of code worked too. Thanks.
Code:
$data['projects']= $this->db->where('user_id', 1)->get('projects')->num_rows();



Accessing records from 2 tables in a single view - El Forum - 06-25-2011

[eluser]adityamenon[/eluser]
[quote author="shailendra" date="1309002169"]This line of code worked too. Thanks.
Code:
$data['projects']= $this->db->where('user_id', 1)->get('projects')->num_rows();
[/quote]

cheers.