Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This is my delete function that I will be using for most of my other controllers that need one.

The purpose of this is to delete the value that passed in to delete the item from the database table and return an array for the user to see a message of results. This delete function will be used for jQuery Ajax as well as regular non-Ajax functionality with a link that you get sent to the delete method just in case the user does not have JavaScript enabled.

Should I have two separate delete functions, or is one okay? I know of the function $this->input->is_ajax_request(), but I'm not sure of the best practice for this situation.

Can someone shed some light on the subject as well as any critiquing of my code?

/**
 * Content_pages::delete()
 * 
 * Deletes a content page from the list of content pages.
 * 
 * @param string $content_page_id The id of the content page being deleted. Zero is set as default.
 * @return json array 
 */
public function delete($content_page_id = 0)
{
    $status = 'unprocessed';
    $title = 'Action Unprocessed';
    $message = 'The last action was rendered unprocessed. Please try again.';
    if (isset($content_page_id) && is_numeric($content_page_id))
    {
        $content_page_data = $this->content_page->get($content_page_id);
        if (!empty($content_page_data))
        {
            $this->content_page->delete($content_page_id);
            if ($this->db->affected_rows() > 0)
            {
                $status = 'success';
                $message = 'The content page has been successfully deleted.';
                $title = 'Content Page Deleted';   
            }
            else
            {
                $status = 'error';
                $message = 'The content page was not deleted successfully.';
                $title = 'Content Page Not Deleted';   
            }
        }
    }
    $output = array('status' => $status, 'message' => $message, 'title' => $title);
    echo json_encode($output);
}
share|improve this question
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.