From 3bf17fb1423283d79a81b6ec3ca6134566b28475 Mon Sep 17 00:00:00 2001 From: Joël Cox Date: Sun, 9 Oct 2011 19:20:12 +0200 Subject: Initial conversion of the HTML to RST using pandoc. --- .../source/tutorial/create_news_items.rst | 142 +++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 user_guide_src/source/tutorial/create_news_items.rst (limited to 'user_guide_src/source/tutorial/create_news_items.rst') diff --git a/user_guide_src/source/tutorial/create_news_items.rst b/user_guide_src/source/tutorial/create_news_items.rst new file mode 100644 index 000000000..4a0056ada --- /dev/null +++ b/user_guide_src/source/tutorial/create_news_items.rst @@ -0,0 +1,142 @@ +Tutorial - Create news items +============================ + +You now know how you can read data from a database using CodeIgnite, but +you haven't written any information to the database yet. In this section +you'll expand your news controller and model created earlier to include +this functionality. + +Create a form +------------- + +To input data into the database you need to create a form where you can +input the information to be stored. This means you'll be needing a form +with two fields, one for the title and one for the text. You'll derive +the slug from our title in the model. Create the new view at +application/views/news/create.php. + +Create a news item +------------------ + + Title + Text + + +There are only two things here that probably look unfamiliar to you: the +form\_open() function and the validation\_errors() function. + +The first function is provided by the `form +helper <../helpers/form_helper.html>`_ and renders the form element and +adds extra functionality, like adding a hidden `CSFR prevention +field <../libraries/security.html>`_. The latter is used to report +errors related to form validation. + +Go back to your news controller. You're going to do two things here, +check whether the form was submitted and whether the submitted data +passed the validation rules. You'll use the `form +validation <../libraries/form_validation.html>`_ library to do this. + +:: + + public function create() + { + $this->load->helper('form'); + $this->load->library('form_validation'); + + $data['title'] = 'Create a news item'; + + $this->form_validation->set_rules('title', 'Title', 'required'); + $this->form_validation->set_rules('text', 'text', 'required'); + + if ($this->form_validation->run() === FALSE) + { + $this->load->view('templates/header', $data); + $this->load->view('news/create'); + $this->load->view('templates/footer'); + + } + else + { + $this->news_model->set_news(); + $this->load->view('news/success'); + } + } + +The code above adds a lot of functionality. The first few lines load the +form helper and the form validation library. After that, rules for the +form validation are set. The set\_rules() method takes three arguments; +the name of the input field, the name to be used in error messages, and +the rule. In this case the title and text fields are required. + +CodeIgniter has a powerful form validation library as demonstrated +above. You can read `more about this library +here <../libraries/form_validation.html>`_. + +Continuing down, you can see a condition that checks whether the form +validation ran successfully. If it did not, the form is displayed, if it +was submitted **and** passed all the rules, the model is called. After +this, a view is loaded to display a success message. Create a view at +application/view/news/success.php and write a success message. + +Model +----- + +The only thing that remains is writing a method that writes the data to +the database. You'll use the Active Record class to insert the +information and use the input library to get the posted data. Open up +the model created earlier and add the following: + +:: + + public function set_news() + { + $this->load->helper('url'); + + $slug = url_title($this->input->post('title'), 'dash', TRUE); + + $data = array( + 'title' => $this->input->post('title'), + 'slug' => $slug, + 'text' => $this->input->post('text') + ); + + return $this->db->insert('news', $data); + } + +This new method takes care of inserting the news item into the database. +The third line contains a new function, url\_title(). This function - +provided by the `URL helper <../helpers/url_helper.html>`_ - strips down +the string you pass it, replacing all spaces by dashes (-) and makes +sure everything is in lowercase characters. This leaves you with a nice +slug, perfect for creating URIs. + +Let's continue with preparing the record that is going to be inserted +later, inside the $data array. Each element corresponds with a column in +the database table created earlier. You might notice a new method here, +namely the post() method from the `input +library <../libraries/input.html>`_. This method makes sure the data is +sanitized, protecting you from nasty attacks from others. The input +library is loaded by default. At last, you insert our $data array into +our database. + +Routing +------- + +Before you can start adding news items into your CodeIgniter application +you have to add an extra rule to config/routes.php file. Make sure your +file contains the following. This makes sure CodeIgniter sees 'create' +as a method instead of a news item's slug. + +:: + + $route['news/create'] = 'news/create'; + $route['news/(:any)'] = 'news/view/$1'; + $route['news'] = 'news'; + $route['(:any)'] = 'pages/view/$1'; + $route['default_controller'] = 'pages/view'; + +Now point your browser to your local development environment where you +installed CodeIgniter and add index.php/news/create to the URL. +Congratulations, you just created your first CodeIgniter application! +Add some news and check out the different pages you made. -- cgit v1.2.3-24-g4f1b From c2b48812b1e1c3457ddaffb2fbbfcccb45554694 Mon Sep 17 00:00:00 2001 From: Joël Cox Date: Mon, 10 Oct 2011 20:24:46 +0200 Subject: Fixed markup glitches from HTML to RST parser. --- .../source/tutorial/create_news_items.rst | 29 +++++++++++++++------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'user_guide_src/source/tutorial/create_news_items.rst') diff --git a/user_guide_src/source/tutorial/create_news_items.rst b/user_guide_src/source/tutorial/create_news_items.rst index 4a0056ada..003b94bd8 100644 --- a/user_guide_src/source/tutorial/create_news_items.rst +++ b/user_guide_src/source/tutorial/create_news_items.rst @@ -1,5 +1,6 @@ -Tutorial - Create news items -============================ +################# +Create news items +################# You now know how you can read data from a database using CodeIgnite, but you haven't written any information to the database yet. In this section @@ -15,16 +16,26 @@ with two fields, one for the title and one for the text. You'll derive the slug from our title in the model. Create the new view at application/views/news/create.php. -Create a news item ------------------- +:: + +

Create a news item

+ + + + + + +
+ + +
+ + - Title - Text - + There are only two things here that probably look unfamiliar to you: the -form\_open() function and the validation\_errors() function. +form_open() function and the validation_errors() function. The first function is provided by the `form helper <../helpers/form_helper.html>`_ and renders the form element and -- cgit v1.2.3-24-g4f1b