Welcome Guest, Not a member yet? Register   Sign In
Models causing blank pages
#1

[eluser]phused[/eluser]
I'm having an issue with my models, each time I load a model on one of my controllers the page appears blank. For example, this is one of my controllers:

Code:
<?php
class Tickets extends Controller {

    function Tickets()
    {
        parent::Controller();
        $this->load->model('Tickets_model');
    }

    function index()
    {
        $this->load->view('header');
        $this->load->view('navigation');
        $this->load->view('tickets_index');
        $this->load->view('footer');
    }
    
    function view()
    {
        $this->load->view('header');
        $this->load->view('navigation');
        $this->load->view('tickets_view');
        $this->load->view('footer');
    }

}
?>

My tickets model has the following code, fairly simple and easy:

Code:
<?php

class Tickets_model extends Model {

    function Tickets_model()
    {
        // Call the Model constructor
        parent::Model();
    }

    // Connect to database
    $this->load->database();

    function display_open_tickets
    {
        $query = $this->db->query('SELECT id, title, priority FROM tickets');
        
        foreach ($query->result_array() as $row)
        {
            echo $row['id'];
            echo $row['title'];
            echo $row['priority'];
        }
                
    }
}

?>

When I try to access /tickets page, it appears blank. However, once I remove the $this->load->model('Tickets_model'); it loads perfectly fine. I have tried to uncomment certain sections of the model to see if it was a specific part of it, but it all combinations lead to the Tickets index() page rendering blank.

Any ideas?
#2

[eluser]Pascal Kriete[/eluser]
Hey Xirgo,

Let's start with the syntax errors. I see two right now:
1) The database loading call should be in a method, in this case the constructor is probably the best choice. Classes are not processed top-down, and can't contain any logic outside of class methods.
2) Your second method declaration needs a pair of parentheses to be valid.

Blank pages usually indicate that the script has one or more syntax errors and the PHP interpreter can't parse it. A lot of setups don't show parse errors by default, but there is a PHP directive called 'display_errors' that will turned them on.
Errors sneak in quite a lot, so it's nice to have a development setup that will show them. Production should, of course, fail silently (or not at all Smile ).

Let us know how that goes.
Welcome to CodeIgniter.
#3

[eluser]phused[/eluser]
Thanks for the information, it was helpful. I have updated my code and also edited my PHP configuration although I seem to be getting another error which I'm sure isn't!

My code is looking like this now:

Code:
<?php

class Tickets_model extends Model {

    function Tickets_model()
    {
        // Call the Model constructor
        parent::Model();
    }


    function display_open_tickets()
    {

        // Connect to database
        $this->load->database();
        
        $query = $this->db->query('SELECT id, title, priority FROM tickets');
        
        foreach ($query->result_array() as $row)
        {
            echo $row['id'];
            echo $row['title'];
            echo $row['priority'];
        }
                
    }
}

?>

This is the error I'm getting now:

Quote:Parse error: syntax error, unexpected '{' in /var/www/html/CodeIgniter/system/application/models/tickets_model.php on line 3

What am I doing wrong, any ideas?

Thanks in advance Smile
#4

[eluser]Pascal Kriete[/eluser]
Hmm... I copy-pasted your post, loaded it, and it worked fine Smile . Maybe a typo in the original file?
#5

[eluser]phused[/eluser]
Just to confirm, the model's file name is tickets_model.php and the controller file name is tickets.php, right?
#6

[eluser]Pascal Kriete[/eluser]
The controller (file) name is irrelevant in this case, but the model has to be called tickets_model.php .




Theme © iAndrew 2016 - Forum software by © MyBB