-1

I have an php application in Codeigniter. There are 3 pages model.php , controller.php and view.php

model.php

<?php
    class Activity_insert extends CI_Model {
        public function  activity()
        {
            echo "it works";
        }
    }
    ?>

controller.php

public function viewemp($q = NULL)
         {
            $this->load->helper('form');
            $this->load->library('form_validation');
                $this->load->model('activity_insert');
             $data['activity_log'] = $this->activity_insert->activity();
                $this->load->view('header');
                $this->load->view('sidebar');
                $this->load->view('success',$data);
                $this->load->view('footer');
         }

view.php

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>GATT</title>
  </head>
  <body class="hold-transition skin-blue sidebar-mini">
    <div class="wrapper">

    </div>
  </body>
  </html>

after compiling my view page ahowing like this

 it works <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>GATT</title>
      </head>
      <body class="hold-transition skin-blue sidebar-mini">
        <div class="wrapper">

        </div>
      </body>
      </html>

data is coming at starting of page. and if I did not call $activity_log anywhere in view.php it still showing same text "it works" at very begining of page. please help to resolve it.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Raj
  • 37
  • 4

2 Answers2

1

In Model make this

echo "it works";

to this

return "it works";

When your function meets and echo it will print the value to browser as they met. No data will be come back through function. But if you used return it will return the data with your call back function. So you can use that value where you need.

What is the difference between PHP echo and PHP return in plain English?

Community
  • 1
  • 1
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • yes it works, thanks. But could me tell me why use return and not echo. – Raj Jun 24 '16 at 21:09
  • @Raj If this help please **[Mark it as ACCEPT**](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). Hence [**VOTE UP** will lead my TAG count](http://meta.stackexchange.com/questions/173399/how-to-upvote-on-stack-overflow) – Abdulla Nilam Feb 27 '17 at 06:58
0

The echo command prints a text at the moment when it's called. So, you should only call it when you need to show some text to the user. That will usually happen in the view.

Now, when you want to return some text to a function and use it at a later point, you should use return instead of echo and store that in some variable for later use. In your case, in the model you should have

public function  activity()
{
    return "it works";
}

so, when you call $data['activity_log'] = $this->activity_insert->activity() the data that the activity function returns will be assigned to the $data['activity_log'] variable. If you don't return anything, but do an echo as you do right now, the string will be printed before loading the view (which is why it appears at the beginning of the html) and, since the function is not returning anything, the $data['activity_log'] variable will be empty.

trajchevska
  • 942
  • 7
  • 13