Welcome Guest, Not a member yet? Register   Sign In
First Question from a new CI user
#1

[eluser]trope[/eluser]
I am expanding a bit on the blog tutorial.

I am passing a "success" message to my view, which tells the user the record was created successfully.

The problem is, when I insert the records and return to the "admin_view" view, no records are shown (even though they are there).

It appears the URL is wrong when it loads the view.

Here is my newbie code:

Code:
function index()
    {
        
        $data = array();

        /* Check to see if we have records available */
        if($query = $this->admin_model->get_records() )
        {
            
                $data['records'] = $this->admin_model->get_records();
              
    
        }
        
        $this->load->view('admin_view', $data);
        
    }
    
    
    function add_post(){

        $this->load->view('admin_create_view');
        
    }
    
    
    function create_new_post(){
        
        $data = array(
            'title' => $this->input->post('title'),
            'body' => $this->input->post('body'),
            'date_posted' => date("Y-m-d")
        );
        
        $this->admin_model->add_record($data);

        $data['notification_message'] = "You have successfully created a new blog post";
        
        $this->load->view("admin_view",$data);
        
    }

So when I insert a record, it DOES display the success message, but no records are displayed. And the URL after the insert appears not to have changed.

I have attached screenshots of the error.

Thanks, and glad to be here!

John
#2

[eluser]Buso[/eluser]
how about posting the get_records() function? Smile

Also, you are running it twice. Not that that's the problem, but you should avoid doing that
#3

[eluser]trope[/eluser]
Here is my get records function
Code:
function get_records()
    {

        $query = $this->db->get("entries");
        return $query->result();

    }

Could you explain where and what I am running twice? Thanks!
#4

[eluser]liamr[/eluser]
Code:
if($query = $this->admin_model->get_records() )

This line! Maybe try

Code:
/* Check to see if we have records available */

$query = $this->admin_model->get_records()

if($query)
{
    
         $data['records'] = $query;

}

Smile
#5

[eluser]umefarooq[/eluser]
Hi here is nice message library will help you to solve your problem call this library at you create_new_post function
http://codeigniter.com/wiki/Message/
Code:
function create_new_post(){
        $this->load->library('message');
        $data = array(
            'title' => $this->input->post('title'),
            'body' => $this->input->post('body'),
            'date_posted' => date("Y-m-d")
        );
        
        $this->admin_model->add_record($data);

         $this->message->set('You have successfully created a new blog post', 'success', TRUE);
        
//      $this->load->view("admin_view",$data);

         redirect();
        
    }

try to redirect after saving your data it will clear post data if you are not redirecting and somebody refresh the page it will add same data again for more information check the library wiki.
#6

[eluser]trope[/eluser]
Thanks! These suggestions really helped me!




Theme © iAndrew 2016 - Forum software by © MyBB