Welcome Guest, Not a member yet? Register   Sign In
passing multiple table value by a single function
#1

[eluser]Paleleaves[/eluser]
Hi,

I'm a newbie in CI. I've a view file to display the data from two tables. I managed to get the data from one table and successfully displayed. How can I retrieve data from two tables using a sigle fuction and pass to the view file. Any examples or url in the forum will be really helpful.

thanks in advance
#2

[eluser]n0xie[/eluser]
I don't know if you mean you need to join the tables (the tables have some relation to eachother) or that you simply have data stored in seperate tables and want it displayed in one view. If it is the former, you need to do some reading on JOINS. If it is the latter here is a simple example:

Code:
// controller
$data['foo'] = $this->some_model->some_function();
$data['bar'] = $this->some_other_model->some_other_function();
$this->load->view('some_view',$data);

// view
var_dump($foo);
var_dump($bar);
#3

[eluser]Paleleaves[/eluser]
Hi,

thanks for the reply. But need a little clarification. Here begins; this is the default function index() in the controller.

Code:
function index ()
{

$sql['query']= &this;->dg->get ('property'); // Property table
$sql['query2']= &this;->dg->get ('anotherTable'); // connect to ANOTHER table

$this->load->view ('property_view', $sql); //
}

In the view file I've a foreach loop to display the values like this.

Code:
<?php foreach($query as $item):?>

  <li>&lt;?=$item->id?&gt;</li> //Value from property table. This is OK

<li>&lt;?=$item->Someid?&gt;</li> // Here I  want to display data from ANOTHER table? How can I do that

&lt;?php endforeach;?&gt;

Thank you for spend your time...
#4

[eluser]pistolPete[/eluser]
Can you post your database schema?
#5

[eluser]John_Betong[/eluser]
&nbsp;
&nbsp;
Here is a BIG KLUDGE to get the job doe and move on to your next problem Smile
Code:
&lt;?php
$sql['query1'] = array('apples','pears','bananas','coconuts','cherries','strawberries','guava','mango','pineapples','melon');
$sql['query2'] = array('OAK','PINE','CEDAR','BEECH','WALNUT','WILLOW','BIRCH','LARCH','HAZEL','HOLLY');
$sql['query3'] = array($sql['query1'], $sql['query2']);
$max   = count($sql['query1']);
echo '<ol>';
    for($i2=0; $i2<$max;    $i2++)
    {
        echo "<li>";
            echo     $sql['query3'][0][$i2];
        echo '</li>';
    
        echo '<li style="color:red">';
            echo     '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
        echo     $sql['query3'][1][$i2];
        echo '</li>';
    }
echo '</ol>';
?&gt;

&nbsp;
&nbsp;
&nbsp;
Output:
Code:
.

   1. apples
   2.      OAK
   3. pears
   4.      PINE
   5. bananas
   6.      CEDAR
   7. coconuts
   8.      BEECH
   9. cherries
  10.      WALNUT
  11. strawberries
  12.      WILLOW
  13. guava
  14.      BIRCH
  15. mango
  16.      LARCH
  17. pineapples
  18.      HAZEL
  19. melon
  20.      HOLLY
&nbsp;
&nbsp;
&nbsp;
#6

[eluser]Paleleaves[/eluser]
Hi,

Please click on this url to see the schema

schema

To be more specific I want to combine data from these two tables and pass to a view file. As marked in the schema image; pCat values to come reqType when the view files loads. that's all.

thanks
#7

[eluser]n0xie[/eluser]
Again, why not use a join?
#8

[eluser]Paleleaves[/eluser]
Hi,

Thank you very much for the all ..

But again stuck on a problem here.

Code:
$sql['query']= $this->db->query('SELECT * FROM property, ptype WHERE property.reqType = ptype.pID');

I managed to get the information from multiple tables using the above code but need to take the data from a third table along with this. Here is the code I tried but didn't work


Code:
$sql['query']= $this->db->query('SELECT * FROM property, ptype WHERE property.reqType = ptype.pID, district WHERE property.districtName = district.dID');

Any help will really appreciated!
#9

[eluser]Sinclair[/eluser]
Hi,

The selec you need to do to join the tables is:

Code:
select p.pID, p.reqType, pt.pCat from propery p, ptype pt where p.pID = pt.pID

Test the select in PhpMyAdmin or in the tool you use to test the selects.


Best Regards,
#10

[eluser]Paleleaves[/eluser]
thank you! This is much informative...




Theme © iAndrew 2016 - Forum software by © MyBB