Welcome Guest, Not a member yet? Register   Sign In
contents of php file shows in browser window
#1

[eluser]entropy[/eluser]
when I run the command:

'$this->load->model('Database_construct');'

in the main controller (testing), while the file 'database_construct.php' in the 'application/models' directory, and the syntax of this file correct as far as I can see (twenty-times checked), it doesn't run but shows the contents of the file 'database_construct.php' in the browser window.

What is wrong here?

(I don't include the contents of 'database_construct.php' here for privacy reasons)
#2

[eluser]TWP Marketing[/eluser]
Can you post your test controller code in which the model is used?
I would like to see how you are loading the views in the controller and the name of the view files.
#3

[eluser]entropy[/eluser]
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

/**
  * Index Page for this controller.
  *
  * Maps to the following URL
  *   http://example.com/index.php/welcome
  * - or -  
  *   http://example.com/index.php/welcome/index
  * - or -
  * Since this controller is set as the default controller in
  * config/routes.php, it's displayed at http://example.com/
  *
  * So any other public methods not prefixed with an underscore will
  * map to /index.php/welcome/<method_name>
  * @see http://ellislab.com/codeigniter/user-guide/general/urls.html
  */
public function index()
{
  $this->load->model('Database_construct');
  $this->load->view('top');
  $this->load->view('middle');
  $this->load->view('end');
}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */


I include a censured version of the model:
Code:
class Database_construct extends CI_Model {

  function __construct()
  {
    parent::__construct();
}
  
function Create_user_table()
{
    $this->db->query('
     CREATE TABLE ... (
...
    );'
  );
}
  
}

This is the very first test of the very first time I use CI.
#4

[eluser]GlenW[/eluser]
It sounds like PHP isn't running on your testing environment. What's your setup?
#5

[eluser]entropy[/eluser]
[quote author="GlenW" date="1350415497"]It sounds like PHP isn't running on your testing environment. What's your setup?[/quote]
I use wampp locally succesfully with other sites for developing.
#6

[eluser]TWP Marketing[/eluser]
May not be related, but you are loading three views in your controller:
Code:
...
public function index()
{
  $this->load->model('Database_construct');
  $this->load->view('top');
  $this->load->view('middle');
  $this->load->view('end');
}
..
These will overwrite each other and the server will be confused, or only one of them will be displayed by the browser.
If you have sub-views (top,middle,end) then you need a template to display them:
a template view file (/application/views/somename.php)
Code:
&lt;html&gt;
&lt;head&gt;
... html head code goes here
&lt;/head&gt;
&lt;body&gt;
&lt;?php
echo $top_view;
echo $middle_view;
echo $end_view
?&gt;
&lt;/body&gt;
&lt;/html&gt;
In your controller, write the sub-views to variables using the second parameter (boolean true) of the loader view function:
[EDIT] See the User Guide: http://ellislab.com/codeigniter/user-gui...oader.html for the format of the load->view() method
Code:
...
public function index()
{
  $this->load->model('Database_construct');

  // we assume that you use your model functions here to prepare data for the views


  $data['top_view'] = $this->load->view('top',true); // write the html code to a data var
  $data['middle_view'] = $this->load->view('middle',true); // write the html code to a data var
  $data['end_view'] = $this->load->view('end',true); // write the html code to a data var

   // finally, send your template view to the browser and pass the array $data to that view for display
  $this->load->view('somename',$data); // this is the only view that goes to the browser
}
..
#7

[eluser]entropy[/eluser]
I found the answer:

First of all, all php files must begin with:

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Then I also have to connect to the database.

Then it works Big Grin

Thanks for the interest in this topic of mine Wink
#8

[eluser]GlenW[/eluser]
[quote author="TWP Marketing" date="1350416902"]
May not be related, but you are loading three views in your controller:
[/quote]

Probably because the tutorial does the same:

Code:
public function view($page = 'home')
{
  
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
  // Whoops, we don't have a page for that!
  show_404();
}

$data['title'] = ucfirst($page); // Capitalize the first letter

$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);

}

Also, I do the same in my controllers and it works fine.
#9

[eluser]TWP Marketing[/eluser]
Ahha, I've been using the template approach for so long I forgot about the other option. Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB