user3810730
user3810730

Reputation:

PHP MVC can't access controller

I have this simple PHP code where I have created a new model with a celebrity name, a controller that accesses the celebrity name from the model, and a view to display the name. For some reason using the path below I can't display the name accessing the controller. I can't find out what I am missing or doing wrong. Thanks for the help!

http://localhost/ci/Starcontroller/guess

Controller->

    <?php
class Starcontroller extends CI_Controller{
    function __construct() {parent::__construct();}
    function guess()
    {
        $this->load->model('starmodel');
        $newstarr = $this->starmodel->lookup();
        $this->load->view('starview',$newstarr);
    }
}   
?>

Model->

<?php
class starmodel extends CI_Model{
    function __construct()
    {
        parent::__construct();
    }
    public function lookup(){

    $celebrity = "Anna";
    return $celebrity;
    }
}
?>

View ->

<html>
<head></head>
<body>

    <?php
        echo $newstarr;
    ?>
</body> 
</html>

Upvotes: 1

Views: 790

Answers (2)

Linus
Linus

Reputation: 909

First initial alphabet of Class name and File name of controller and model are in capital letter's check your model class name

Then in controller change to this

<?php

class Starcontroller extends CI_Controller {

   function __construct() {
      parent::__construct();
      // Would load model in here and other helpers and libraries.
      // So can access through all functions on controller.
      // $this->load->model('starmodel');
   }

   function guess() {
      $this->load->model('starmodel');
      $newstarr['data'] = $this->starmodel->lookup();
      $this->load->view('starview',$newstarr);
   }

}   

In view

<?php echo $data ?>

Upvotes: 1

Tpojka
Tpojka

Reputation: 7111

From controller, you would like to pass an associative array of data to the view. So change this part:

$newstarr = $this->starmodel->lookup();
$this->load->view('starview',$newstarr);

To this:

$data['newstarr'] = $this->starmodel->lookup();
$this->load->view('starview',$data);

Rest of your code may stay the same.

Upvotes: 0

Related Questions