GrumpyCrouton
GrumpyCrouton

Reputation: 8621

codeigniter helloworld app not running for some reason

I'm trying to follow a tutorial here which is just a simple helloworld that pulls data from a database.

I'm using CI 3.0.6.

I have never used codeigniter before, but I wanted to try a framework.

Anyways, I think I followed the tutorial correctly (I do think the tutorial uses a slightly older version of codeigniter as some of the paths are a bit different) but I cannot get this to work, it gives me the error The page you requested was not found.

I have a few questions on this.

The tutorial tells me to use a model, but what is a model?

How can I get this working? If I can get this working properly I have no doubt I can use it as a learning experience to expand my project.

My code: helloworld.php (Controller @ BASEDIR/application/controllers)_:

<?php
    class Helloworld extends Controller{
        function index()
        {
            $this->load->model('helloworld_model');

            $data['result'] = $this->helloworld_model-><span class="sql">getData</span>();
            $data['page_title'] = "CI Hello World App!";

            $this->load->view('helloworld_view',$data);
        }
    }
?>

helloworld_model.php (Model @ BASEDIR/application/models):

<?php
class Helloworld_model extends Model {

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

    function getData()
        {
            //Query the data table for every record and row
            $query = $this->db->get('data');

            if ($query->num_rows() > 0)
            {
                //show_error('Database is empty!');
            }else{
                return $query->result();
            }
        }

}
?>

helloworld_view (view @ BASEDIR/application/views):

<html>
    <head>
        <title><?=$page_title?></title>
    </head>
    <body>
        <?php foreach($result as $row):?>
        <h3><?=$row->title?></h3>
        <p><?=$row->text?></p>
        <br />
        <?php endforeach;?>
    </body>
</html>

my db info:

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'private',
    'username' => 'private',
    'password' => 'private',
    'database' => 'database',
    'dbdriver' => 'mysql';
    'dbprefix' => 'va',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

& my autoload:

$autoload['libraries'] = array('database');

I'm trying to access the page by going to http://myurl.com/dev/index.php/helloworld/

/dev/ is my installation directory.

Again, I've never used a framework before so this question is probably really simple to fix.

Upvotes: 0

Views: 160

Answers (2)

elddenmedio
elddenmedio

Reputation: 1030

If you read a documentation controller enter link description here

you can see that in application/controller/Name.php the name of the controller needs to be capitalize, inside the class you need to have (remember extends CI_Controller)

<?php
class Controller_name extends CI_Controller {

        public function view($page = 'home')
        {
        }
}

the call to the model is ok $this->load->model('helloworld_model');m but remember that in applicatoin/models/Model_name.php needs to be capitalized too, and insede you need to have (remember extends CI_Model)

<?php 
class Blog_model extends CI_Model {

        public function __construct()
        {
                // Call the CI_Model constructor
                parent::__construct();
        }

Yes, you needs to autoload database, but in you database config (application/config/database.php) change dbdriver to mysqli

Upvotes: 1

TheDrot
TheDrot

Reputation: 4337

Well if you followed that tutorial completely then you need to access that page without index.php in url.

Also use PHP5 constructors so change:

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

To:

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

This also makes no sense:

if ($query->num_rows() > 0)
{
    //show_error('Database is empty!');
}
else
{
    return $query->result();
}

The code inside if/else should be switched. If number of rows returned is greater than 0 then return result else show error that database is empty.

And last thing in controller:

$data['result'] = $this->helloworld_model-><span class="sql">getData</span>();

Store only database result to data variable, pass it to view and wrap whatever elements of the data in the view.

$data['result'] = $this->helloworld_model->getData();

Upvotes: 2

Related Questions