Welcome Guest, Not a member yet? Register   Sign In
A little help with my first app...
#1

[eluser]invision[/eluser]
I call it an app, but it's just going to be a basic guestbook Smile Something to get me started with CodeIgniter.

I'm hoping to retrieve records from my 'data' table in MySQL and ultimately display them in my view.

I've stumbled at the first hurdle though Sad Can someone explain where I've gone wrong?


Controller(guestbook.php):
Code:
<?php

class Guestbook extends Controller {

    function Guestbook()
    {
        parent::Controller();    
    }
    
    function index()
    {
      $data['cats'] = $this->Guestbook_model->getRecords();
      $this->load->vars($data);
      $this->load->view('guestbook');
    }
}

Model(guestbook.php):
Code:
<?php

class Guestbook_model extends Model {

    function Guestbook_model()
    {
        parent::Controller();    
    }
    
    function getRecords() {
          $Q = $this->db->get('data');
          if ($Q->num_rows() > 0){
            foreach ($Q->result_array() as $row){
              $data[$row['id']] = $row['title'];
            }
          }
          $Q->free_result();
          return $data;
    }

}

My error message is:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Guestbook::$Guestbook_model

Filename: controllers/guestbook.php

Line Number: 12


Many thanks for any pointers with this.

Hopefully I shouldn't have to come back too often.
#2

[eluser]n0xie[/eluser]
You forgot to load your model:

Code:
$this->load->model('Guestbook_model');
#3

[eluser]invision[/eluser]
Many thanks for the fast reply Smile

I'm now getting:
Code:
An Error Was Encountered

Unable to locate the model you have specified: guestbook_model

Any ideas?
#4

[eluser]n0xie[/eluser]
Quote:Model(guestbook.php):

Quote:Unable to locate the model you have specified: guestbook_model
#5

[eluser]invision[/eluser]
Sorted! Many thanks n0xie.

Going to play about with this some more tonight and see how I go.

Could you possibly give me some clues how I would take the data retrieved in my model and display it in the view?


Thanks so much again.
#6

[eluser]n0xie[/eluser]
Code:
// model
    function getRecords() {
          $query = $this->db->get('data');
          if ($query->num_rows() > 0)
          {
              return $query->result_array();
          }
          else
          {
              return FALSE;
          }
    }

Code:
// view
foreach ($cats as $row)
{
  echo $row['something'];
}
#7

[eluser]invision[/eluser]
Wow, n0xie, you're slowly becoming a hero of mine.

Many thanks for this. Now to study and learn from it then proceed with the rest of the build Smile

Thank You
#8

[eluser]invision[/eluser]
I had thought of starting a new thread, but would prefer to keep this open for a bit.

If I wanted to make a model/view/controller for deleting an entry from my 'data' table, how would I best do this?

I'd thought of this....

controller:
Code:
function delete()
    {
        $this->load->model('Guestbook_model');
          $this->Guestbook_model->deleteEntry($id);
        $this->load->view('delete_success');
    }

model:
Code:
function deleteEntry(){
        $this->db->query("DELETE FROM data");
        $this->db->where('id',$this->uri_segment('3'));        
    }

Does this look about right? I've a feeling it's not Sad

The idea being that they visit ... example.com/guestbook/index.php/delete/2 and it would remove that entry from my table, and return the user to the guestbook.


Many thanks for any pointers with this.
#9

[eluser]n0xie[/eluser]
You should use the active record class.

To answer your question
Code:
//controller
function delete($id = NULL)
    {
        $this->load->model('Guestbook_model');
          $this->Guestbook_model->deleteEntry((int) $id);
        $this->load->view('delete_success');
    }

// model
function deleteEntry($id)
{
  // just to make sure we don't do anything stupid
  if ($id > 0)
  {
        $this->db->where('id',$id);
        $this->db->delete('data');
  }
}

A word of caution though. You should always do any destructive action via a POST, not via a GET request, so I would advice against doing it this way. This is to prevent numerous security issues, the most simple being, what if the Googlespider would index your site and go to the url 'example.com/guestbook/index.php/delete/2'?
#10

[eluser]invision[/eluser]
Many thanks for the reply.

I'm hoping in time to move this to an Admin dashboard, and just prevent the Googlebot from spidering. Though that's a really good point you make.

Just out of interest, how would I do it via a _POST? Would I have to modify my view code?


Thanks once again for your patience with this. It's good to start small and get my head around it like this.




Theme © iAndrew 2016 - Forum software by © MyBB