Andrew
Andrew

Reputation: 4441

CodeIgniter PHP MySQL Query to View

I'm missing something here, I can't get variables to pass through the model to the view via controller.

I'm recieving: The website encountered an error while retrieving site. It may be down for maintenance or configured incorrectly. currently.

If I remove the 2nd $this->load->model('testingsearch'); then I get the following error message:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: HW::$testingsearch

Filename: controllers/hw.php

Line Number: 63

MODEL:

<?php

class TestingSearch extends Model
{
function Messages()
{
    parent::Model();
}

function getMessages($id)
{
    $this->db->distinct();
    $this->db->select('*');
    $this->db->where('id', $id);
    $result = $this->db->get('HWC');

    if (!$result) {
        return false;
    } else {
        return $result;
        echo $result;
    }
}
}
?>

CONTROLLER:

<?php

class HW extends CI_Controller {

function Thiscontroller()
{
  parent::Controller();
  $this->load->database(); // This should be autoloaded

  $this->load->model('testingsearch');
}


function id($id='') {
       $this->load->model('testingsearch');
         $data['records'] = $this->testingsearch->getMessages($id);

   $this->load->view('searchresults', $data);

}

}

VIEW:

<ul>
<?php foreach ($records->result() as $row) { ?>
<li><?php echo $row->id; ?></li>
<li><?php echo $row->ModelName; ?></li>
<li><?php echo $row->Color; ?></li>
<? } ?>
</ul>

Upvotes: 1

Views: 4022

Answers (3)

Jhonathan H.
Jhonathan H.

Reputation: 2713

Wrong extension of your MODEL

 // model should extend CI_Model
    class TestingSearch extends CI_Model{
        // then add additional constructor to call the model
        function __construct(){
              paret::__construct();
        }
    }


 // in controller
class HW extends CI_Controller {
 // change your call to the constructor by using
 function HW(){
     parent::CI_Controller()
     // then load the model
     $this->load->model('testingsearch');
  }
 }

Upvotes: 0

Vimal Patel
Vimal Patel

Reputation: 607

Try this: Please make change in ::::::

MODEL code:

function getMessages($id)
{
    $this->db->distinct();
    $this->db->select('*');
    $this->db->where('id', $id);
    $result = $this->db->get('HWC');

    return $result->result();
}

VIEW code:-

<ul>
<?php foreach ($records as $row) { ?>
<li><?php echo $row->id; ?></li>
<li><?php echo $row->ModelName; ?></li>
<li><?php echo $row->Color; ?></li>
<? } ?>
</ul>

It will solve your problem.................

Upvotes: 2

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

You are not sending anything to controller. Only you are sending indicator whethere model function returning true or false. return the object

MODEL:

Class TestingSearch extends CI_Model
{
    function __construct()
    {
        parent::__construct();
    }

    function getMessages($id)
    {
        $this->db->distinct();
        $this->db->select('*');
        $this->db->where('id', $id);
        $result = $this->db->get('HWC');

        return $result->result();
    }
}

CONTROLLER:

class HW extends CI_Controller 
{
    function __construct()
    {
      parent::__construct();
      $this->load->database(); // This should be autoloaded

      $this->load->model('testingsearch');
    }

    function id($id='') 
    {
        $this->load->model('testingsearch');
        $data['records'] = $this->testingsearch->getMessages($id);
        $this->load->view('searchresults', $data);
    }
}

VIEW:

<ul>
    <?php foreach ($records->result() as $row) { ?>
        <li><?php echo $row->id; ?></li>
        <li><?php echo $row->ModelName; ?></li>
        <li><?php echo $row->Color; ?></li>
    <? } ?>
</ul>

Upvotes: 0

Related Questions