i am trying to obtain different columns from a query.
I am using this function inside controller file:
public function fetch_data(){
$result = $this->db->query("select * from table1 oa
inner join table2 kio on oa.id = kio.id_table_1
inner join table3 joi on kio.col3 = joi.col3_id_table_2
where joi.processed = 1
and joi.approved = 1
and not exists (select 1
from table3 t3
where t3.processed = 0
and t3.approved = 0
and t3.col3_id_table_2 = joi.col3_id_table_2)");
return $result;
}
This is MySQL statement:
https://www.db-fiddle.com/f/adanvQ9JYtTVvbsqq5SBNh/0
I am calling that function in this view:
<?php include('header.php'); ?>
<html>
<head>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h2 align="center">TABLE:USERS</h2>
<thead>
<th>id</th>
<th>COL2</th>
<th>ID_TABLE_1</th>
<th>COL2</th>
<th>COL3</th>
<th>COL3_ID_TABLE2</th>
<th>PROCESSED</th>
<th>APPROVED</th>
</thead>
<tbody>
<?php
if ($fetch_data->num_rows() > 0)
{
foreach($fetch_data->result() as $record) {
echo "<tr>
<td>".$record['id']."</td>
<td>".$record['COL2']."</td>
<td>".$record['ID_TABLE_1']."</td>
<td>".$record['COL2']."</td>
<td>".$record['COL3']."</td>
<td>".$record['COL3_ID_TABLE2']."</td>
<td>".$record['PROCESSED']."</td>
<td>".$record['APPROVED']."</td>
</tr>";
}
}
?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
but, i got this error message: Undefined variable: fetch_data
What am i doing wrong?
EDIT:
I tried to run this code in the view:
<?php
$this->fetch_data();
$myVar = fetch_data();
if ( $myVar->num_rows() > 0 ) {
foreach ( $myVar->result() as $record ) {
echo "<tr>"
. "<td>" . $record['id'] . "</td>"
. "<td>" . $record['COL2'] . "</td>"
. "<td>" . $record['ID_TABLE_1'] . "</td>"
. "<td>" . $record['COL2'] . "</td>"
. "<td>" . $record['COL3'] . "</td>"
. "<td>" . $record['COL3_ID_TABLE2'] . "</td>"
. "<td>" . $record['PROCESSED'] . "</td>"
. "<td>" . $record['APPROVED'] . "</td>"
. "</tr>";
}
}
BUT now i got this error:
Type: Error
Message: Call to undefined method CI_Loader::fetch_data()
fetch_data() function is inside CONTROLLER
Take a look: