Welcome Guest, Not a member yet? Register   Sign In
return false in model - how to use this info in controller
#1

[eluser]tinawina[/eluser]
Hi there - I'm putting together a comment form. It's in the first stages and so is pretty simple/straightforward. Once the comment is submitted, we want to update the user table's "comments" field which simply tracks how many comments a user has submitted. We'll use that # in displays throughout the site. With that done we want to just save the comment data to the comments table.

I'm running into a problem where I check to make sure that the dbase query to update the comment field in the user table was successful. If so, I want to do a few more things and then put up a success message. If not, I want to stop what I'm doing and show a failure message. I keep getting a success message even when clearly the database query didn't work -- as in, nothing is updated/inserted and I should see the failure message. Good news is, if the query did work everything goes as planned.

I gotta admit: return true/return false mystifies me when I use these in a model. How do I actually use "return false" in my controller when I get to it in my model? Total newbie question - thanks so much for any insight!

my controller:

Code:
class Comments extends Controller
{

    function Comments()
    {
        parent::Controller();
        $this->load->helper(array('url', 'form'));
        $this->load->library('validation');
        $this->base = $this->config->item('base_url');
        $this->css = $this->config->item('css');
        $this->tinymce = $this->config->item('tinymce_simple');
        $this->title = "What's your take?";
        $this->content_type = 'research_listing';
    }

    function index()
    {
        // The $display array is used to pass data to the view files.
        $display['title'] = $this->title;
        $display['base'] = $this->base;
        $display['css'] = $this->css;
        $display['tinymce'] = $this->tinymce;
        $display['editor'] = 'yes';
        $display['content_type'] = $this->content_type;

        // The $rules array is used to ensure we get the data we need. Part of the validation library.
        $rules['subject']    = "required|htmlspecialchars";
        $rules['comment']    = "required";
        $this->validation->set_rules($rules);

        // The $fields array is used to repopulate the form fields with the submitted data in case someone is sent back to the form.
        $fields['subject']    = 'Subject';
        $fields['comment']    = 'Comment';
        $this->validation->set_fields($fields);
        $this->validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->validation->run() == FALSE)
        {
            $this->load->view('comment_form', $display, array('error' => ' ' ));
        }
        else
        {
            $this->load->model('handle_comments');
            $this->handle_comments->add();
            if ($this->handle_comments->add() === FALSE)
            {
                $this->load->view('failure');
            }
            else
            {
                $display['subject'] = $this->input->post('subject');
                $display['comment'] = $this->input->post('comment');
                $this->load->view('success', $display);
            }
        }
    }

my model:

Code:
class Handle_comments extends Model {

    function Handle_comments()
    {
        parent::Model();
    // loading database library automatically.
    }

    function add()
    {

    // Find out how many comments are on file for this user -
        $user_id = $this->input->post('user_id');
        if ( $this->db->select('comments')->from('end_users')->where('id', $user_id) )
        {
            $query = $this->db->get();
            foreach ($query->result() as $row)
            {
    //    Increase the number in the "comments" field in End User table by one.
    //    The number is used in site page displays to show how many comments the user has posted to date.
                $comments = $row->comments + 1;
                $data = array('comments' => $comments);
                $this->db->where('id', $user_id);
                $this->db->update('end_users', $data);

    // Save new comment data.  For now, just hard-coding the user_id into a hidden form field.
                $data = array(
                                'user_id'        => $user_id,
                                'content_id'    => $this->input->post('content_id'),
                                'content_type'    => $this->input->post('content_type'),
                                'subject'        => $this->input->post('subject'),
                                'comment'        => $this->input->post('comment'),
                                'date_created'    => date('m-d-Y'),
                                'date_mktime'    => mktime(0,0,0,date('m'),date('d'),date('Y'))
                            );
                $add_data = $this->db->insert_string('comments', $data);
                $query = $this->db->query($add_data);
            }
        }
        else
        {
            return false; // when I get this, i try to pass it to the controller but clearly it's not getting there....
        }

    }
}
#2

[eluser]Crafter[/eluser]
Just note that you are calling add twice. Is this what you want.
Code:
$this->handle_comments->add();
            if ($this->handle_comments->add() === FALSE)

Syntatically, a better way to write your if expression should be
Code:
$this->handle_comments->add();
            if ( ($somevar = $this->handle_comments->add() ) === FALSE)

You other problem is that $this->db->select() builds the SQL query to be executed, and is probably returning true.

You will beed tp rebuild your first if() statement to get this to run as desired.
#3

[eluser]tinawina[/eluser]
Hi and thanks. This is now working properly. Here's the final code - a little more condensed because I started using global variables ($this->title directly in my included header file rather than defining the variable in the controller and then passing it into a $data['title'] array and then using $title in the included header file). Anyway, hope this helps someone else out. Happy coding!

my controller:
Code:
class Comments extends Controller
{

    function Comments()
    {
        parent::Controller();
        $this->load->helper(array('url', 'form'));
        $this->load->library('validation');

    // These global variables are used in the header/footer includes and view files.
        $this->base = $this->config->item('base_url');
        $this->css = $this->config->item('css');
        $this->tinymce = $this->config->item('tinymce_simple');
        $this->editor = 'yes';
        $this->title = "What's your take?";
    }

    function index()
    {

        // The $rules array is used to ensure we get the data we need. Part of the validation library.
        $rules['subject']    = "required|htmlspecialchars";
        $rules['comment']    = "required";
        $this->validation->set_rules($rules);

        // The $fields array is used to repopulate the form fields with the submitted data in case someone is sent back to the form.
        $fields['subject']    = 'Subject';
        $fields['comment']    = 'Comment';
        $this->validation->set_fields($fields);
        $this->validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->validation->run() == FALSE)
        {
            $this->load->view('comment_form', array('error' => ' ' ) );
        }
        else
        {
            $this->load->model('handle_comments');
            $input = $this->handle_comments->add();
            if ($input === false)
            {
                $this->load->view('failure');
            }
            else
            {
                $display['subject'] = $this->input->post('subject');
                $display['comment'] = $this->input->post('comment');
                $this->load->view('success', $display);
            }
        }
    }
}


my model:

Code:
class Handle_comments extends Model {

    function Handle_comments()
    {
        parent::Model();
    // No need to load the database library -- it's automatically loaded via the autoload.php file.
    }

    function add()
    {

    // Find out how many comments are on file for this user in prep to increase the comment count by 1.
        $user_id = $this->input->post('user_id');
        $this->db->select('comments');
        $this->db->where('id', $user_id);
        $query = $this->db->get('end_users');
        if ($query->num_rows() == '1') // found the user? then proceed
        {
            foreach ($query->result() as $row)
            {

    //    Increase the number in the "comments" field in End User table by one.
    //    The number is used in site page displays to show how many comments the user has posted to date.
                $comments = $row->comments + 1;
                $data = array('comments' => $comments);
                $this->db->where('id', $user_id);
                $this->db->update('end_users', $data);

    // Save new comment data
                $data = array(
                                'user_id'        => $user_id,
                                'content_id'    => $this->input->post('content_id'),
                                'content_type'    => $this->input->post('content_type'),
                                'subject'        => $this->input->post('subject'),
                                'comment'        => $this->input->post('comment'),
                                'date_created'    => date('m-d-Y'),
                                'date_mktime'    => mktime(0,0,0,date('m'),date('d'),date('Y'))
                            );
                $add_data = $this->db->insert_string('comments', $data);
                $query = $this->db->query($add_data);
            }
        }
        else
        {
            return false; // didn't find the user, tell the controller to show a failure message.
        }
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB