- This topic has 0 replies, 1 voice, and was last updated 8 years, 10 months ago by Layer7web.
Viewing 0 reply threads
-
AuthorPosts
-
-
January 1, 2016 at 10:44 pm #2207
Layer7webKeymaster*** All of the code snippets taken from http://www.getfuelcms.com/. 🙂
application/config/MY_fuel_modules.phpPHP1234$config['modules']['guestbook'] = array('model_name' => 'guestbookcomments_model','model_location' => 'guestbook');Make sure you have you database tables set up. Here is an example:
MySQL123456789CREATE TABLE IF NOT EXISTS guestbook_comments (id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,author VARCHAR(25) NULL NULL,comment VARCHAR(100) NOT NULL,'date' DATETIME NOT NULL,visible ENUM('yes', 'no') NOT NULL DEFAULT 'yes',PRIMARY KEY (id));Create the Model Class:
PHP123456789101112131415<?phpif (!defined('BASEPATH')) exit('No direct script access allowed');require_once(FUEL_PATH . 'models/base_module_model.php');class GuestbookComments_model extends Base_module_model{public function __construct(){parent::__construct('guestbook_comments');}}class GuestbookComment_model extends Base_module_record{}Create the Controller:
PHP1234567891011121314<?phpclass Guestbook extends CI_Controller{function view(){echo '<h1>Guestbook</h1>';$this->load->module_model('guestbook', 'GuestbookComments_model');$comments = $this->GuestbookComments_model->find_all();foreach ($comments as $comment) {echo '<h2>' . $comment->author . '</h2>';echo $comment->comment;}}}Add Method to the Controller:
PHP12345678910111213141516171819202122232425262728293031<?phppublic function add(){$this->load->library('form_builder',array('id'=>'addComment','form_attrs' => array('method' => 'post','action' => 'newcomment'),'submit_value' => 'Add new comment','textarea_rows' => '5','textarea_cols' => '28'));$fields = array('name' => array('label' => 'Full Name','required' => true),'comment' => array('type' => 'text','label' => 'Comment','required' => true));$this->form_builder->set_fields($fields);echo $this->form_builder->render();}Create method in the Controller to handle the form submission:
PHP123456789101112<?phppublic function newcomment(){$this->load->module_model('guestbook', 'GuestbookComments_model');$comment = $this->GuestbookComments_model->create();$comment->author = $this->input->post('name');$comment->date = date('Y-m-d G:i:s');$comment->comment = $this->input->post('comment');$comment->save();echo 'comment successfully added';}
-
-
AuthorPosts
Viewing 0 reply threads
You must be logged in to reply to this topic.
Comments are closed.