Welcome Guest, Not a member yet? Register   Sign In
site_model error
#11

[eluser]davy_yg[/eluser]

controllers/admin/site.php

Code:
<?php

class Site extends CI_Controller
{
function index()
{
  $data = array();
  
  $records = $this->site_model->get_records();
  if (count($records) > 0)
  {
   $data['records'] = $query;
  }
  
  $this->load->view('options_view', $data);
}

function create()
{
  $data = array(
   'title' => $this->input->post('title'),
   'content' => $this->input->post('content')
  );
  
  $this->site_model->add_record($data);
  $this->index();
}
}

models/Site_model.php

Code:
<?php

class Site_model extends CI_Model {

function get_records()
{
  $query = $this->db->get('data');
  return $query->result();
}

function add_record($data)
{
  $this->db->insert('data', $data);
  return;
}

function update_record($data)
{
  $this->db->where('id', 14);
  $this->db->update('data', $data);
}

function delete_row()
{
  $this->db->where('id', $this->uri->segment(3));
  $this->db->delete('data');
}

}


A PHP Error was encountered

Severity: Notice

Message: Undefined property: Site::$site_model

Filename: admin/site.php

Line Number: 10

Fatal error: Call to a member function get_records() on a non-object in C:\xampp\htdocs\IndonusaCI\application\controllers\admin\site.php on line 10

That's the error.
#12

[eluser]xtremer360[/eluser]
For some reason it all stems right now on saying that your model is not loaded. So can you show the line that you have the autoload file in the models array..
#13

[eluser]davy_yg[/eluser]
Now, I get my site_model loaded correctly.

After getting all the form filled-in and I press submit, I wonder why I start going to this url:

http://localhost/IndonusaCI/index.php?site/create

Instead of processing the data in the form and nothing else happen.

#14

[eluser]Tpojka[/eluser]
Can't be told because we don't have view file to say.
Update post again with all relevant files.

And yes, modes should be named with lower case (small letter).
#15

[eluser]davy_yg[/eluser]

Here is the view file. The rest remains the same.

views/options_view.php

Code:
<!DOCTYPE html>

&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;untitle&lt;/title&gt;
&lt;style type="text/css" media="screen"&gt;
  label {display: block;}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
<h2>Create</h2>
&lt;?php echo form_open('site/create');?&gt;

<p>
      <label for="title">Title:</label>
   &lt;input type="text" name="title" id="title"&gt;
</p>

<p>
      <label for="content">Content:</label>
   &lt;input type="text" name="content" id="content"&gt;
</p>

<p>
   &lt;input type="submit" value="Submit"&gt;
</p>

&lt;?php echo form_close(); ?&gt;

<hr>

<h2>Read</h2>
&lt;?php if(isset($records)) : foreach($records as $row) : ?&gt;

<h2>&lt;?php echo $row->title; ?&gt;</h2>
<div>&lt;?php echo $row->content; ?&gt;</div>

&lt;?php endforeach; ?&gt;

&lt;?php else : ?&gt;
<h2>No records were returned</h2>
&lt;?php endif ; ?&gt;




&lt;/body&gt;
&lt;/html&gt;

#16

[eluser]Tpojka[/eluser]
Try with:

Code:
function create()
{
  $data = array(
   'title' => $this->input->post('title'),
   'content' => $this->input->post('content')
  );
  
  $this->site_model->add_record($data);
  if (true) $this->index(); //added check if something returned from model
}
#17

[eluser]davy_yg[/eluser]
ok I tried that and the result remains the same. It brings me to this url after I press submit:

http://localhost/IndonusaCI/index.php?site/create
#18

[eluser]Tpojka[/eluser]
Since it is very basic usage of CI, I would suggest you taking watch on CI tutorial series.
This is nice one. There is inserting data too. You have two series of 11 and 8 videos.
#19

[eluser]davy_yg[/eluser]
I have done the tutorial CI series and it works. I wonder why this one doesn't work.
#20

[eluser]Tpojka[/eluser]
You have to learn it from scratch.
It is bad practice skipping steps in learning.
You could see it now.

I am glad you made it.




Theme © iAndrew 2016 - Forum software by © MyBB