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