[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....
}
}
}