0

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:

image

JustToKnow
  • 785
  • 6
  • 23
  • `fetch_data` is a function, not a variable, so cannot be treated as such. Call the function and assign the results to a variable. – aynber Oct 14 '19 at 18:07
  • Hi @aynber thank you for replying!.I tried to do that but for some reason it is not working for me. How would you modify it? – JustToKnow Oct 14 '19 at 18:10
  • fetch_data()->num_rows() fetch_data()->result() – Danny Oct 14 '19 at 18:12
  • @AbraCadaver Hi buddy, i tried to do that but i got this error: Type: ParseErro Message: syntax error, unexpected '->' (T_OBJECT_OPERATOR) – JustToKnow Oct 14 '19 at 18:12
  • Hi @Danny Yeah, i tried to do that but i got this error: Type: ParseError Message: syntax error, unexpected '->' (T_OBJECT_OPERATOR) – JustToKnow Oct 14 '19 at 18:13
  • Try to get the output you want inside the function first to verify that is all working correctly – Danny Oct 14 '19 at 18:15

1 Answers1

0

You need to assign the return of the fetch_data function to a variable so you can access it.

$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>";
    }
}

Aquila Sagitta
  • 438
  • 3
  • 9