|
| 1 | +################# |
| 2 | +Create news items |
| 3 | +################# |
| 4 | + |
| 5 | +You now know how you can read data from a database using CodeIgnite, but |
| 6 | +you haven't written any information to the database yet. In this section |
| 7 | +you'll expand your news controller and model created earlier to include |
| 8 | +this functionality. |
| 9 | + |
| 10 | +Create a form |
| 11 | +------------- |
| 12 | + |
| 13 | +To input data into the database you need to create a form where you can |
| 14 | +input the information to be stored. This means you'll be needing a form |
| 15 | +with two fields, one for the title and one for the text. You'll derive |
| 16 | +the slug from our title in the model. Create the new view at |
| 17 | +application/views/news/create.php. |
| 18 | + |
| 19 | +:: |
| 20 | + |
| 21 | + <h2>Create a news item</h2> |
| 22 | + |
| 23 | + <?php echo validation_errors(); ?> |
| 24 | + |
| 25 | + <?php echo form_open('news/create') ?> |
| 26 | + |
| 27 | + <label for="title">Title</label> |
| 28 | + <input type="input" name="title" /><br /> |
| 29 | + |
| 30 | + <label for="text">Text</label> |
| 31 | + <textarea name="text"></textarea><br /> |
| 32 | + |
| 33 | + <input type="submit" name="submit" value="Create news item" /> |
| 34 | + |
| 35 | + </form> |
| 36 | + |
| 37 | +There are only two things here that probably look unfamiliar to you: the |
| 38 | +form_open() function and the validation_errors() function. |
| 39 | + |
| 40 | +The first function is provided by the `form |
| 41 | +helper <../helpers/form_helper.html>`_ and renders the form element and |
| 42 | +adds extra functionality, like adding a hidden `CSFR prevention |
| 43 | +field <../libraries/security.html>`_. The latter is used to report |
| 44 | +errors related to form validation. |
| 45 | + |
| 46 | +Go back to your news controller. You're going to do two things here, |
| 47 | +check whether the form was submitted and whether the submitted data |
| 48 | +passed the validation rules. You'll use the `form |
| 49 | +validation <../libraries/form_validation.html>`_ library to do this. |
| 50 | + |
| 51 | +:: |
| 52 | + |
| 53 | + public function create() |
| 54 | + { |
| 55 | + $this->load->helper('form'); |
| 56 | + $this->load->library('form_validation'); |
| 57 | + |
| 58 | + $data['title'] = 'Create a news item'; |
| 59 | + |
| 60 | + $this->form_validation->set_rules('title', 'Title', 'required'); |
| 61 | + $this->form_validation->set_rules('text', 'text', 'required'); |
| 62 | + |
| 63 | + if ($this->form_validation->run() === FALSE) |
| 64 | + { |
| 65 | + $this->load->view('templates/header', $data); |
| 66 | + $this->load->view('news/create'); |
| 67 | + $this->load->view('templates/footer'); |
| 68 | + |
| 69 | + } |
| 70 | + else |
| 71 | + { |
| 72 | + $this->news_model->set_news(); |
| 73 | + $this->load->view('news/success'); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | +The code above adds a lot of functionality. The first few lines load the |
| 78 | +form helper and the form validation library. After that, rules for the |
| 79 | +form validation are set. The set\_rules() method takes three arguments; |
| 80 | +the name of the input field, the name to be used in error messages, and |
| 81 | +the rule. In this case the title and text fields are required. |
| 82 | + |
| 83 | +CodeIgniter has a powerful form validation library as demonstrated |
| 84 | +above. You can read `more about this library |
| 85 | +here <../libraries/form_validation.html>`_. |
| 86 | + |
| 87 | +Continuing down, you can see a condition that checks whether the form |
| 88 | +validation ran successfully. If it did not, the form is displayed, if it |
| 89 | +was submitted **and** passed all the rules, the model is called. After |
| 90 | +this, a view is loaded to display a success message. Create a view at |
| 91 | +application/view/news/success.php and write a success message. |
| 92 | + |
| 93 | +Model |
| 94 | +----- |
| 95 | + |
| 96 | +The only thing that remains is writing a method that writes the data to |
| 97 | +the database. You'll use the Active Record class to insert the |
| 98 | +information and use the input library to get the posted data. Open up |
| 99 | +the model created earlier and add the following: |
| 100 | + |
| 101 | +:: |
| 102 | + |
| 103 | + public function set_news() |
| 104 | + { |
| 105 | + $this->load->helper('url'); |
| 106 | + |
| 107 | + $slug = url_title($this->input->post('title'), 'dash', TRUE); |
| 108 | + |
| 109 | + $data = array( |
| 110 | + 'title' => $this->input->post('title'), |
| 111 | + 'slug' => $slug, |
| 112 | + 'text' => $this->input->post('text') |
| 113 | + ); |
| 114 | + |
| 115 | + return $this->db->insert('news', $data); |
| 116 | + } |
| 117 | + |
| 118 | +This new method takes care of inserting the news item into the database. |
| 119 | +The third line contains a new function, url\_title(). This function - |
| 120 | +provided by the `URL helper <../helpers/url_helper.html>`_ - strips down |
| 121 | +the string you pass it, replacing all spaces by dashes (-) and makes |
| 122 | +sure everything is in lowercase characters. This leaves you with a nice |
| 123 | +slug, perfect for creating URIs. |
| 124 | + |
| 125 | +Let's continue with preparing the record that is going to be inserted |
| 126 | +later, inside the $data array. Each element corresponds with a column in |
| 127 | +the database table created earlier. You might notice a new method here, |
| 128 | +namely the post() method from the `input |
| 129 | +library <../libraries/input.html>`_. This method makes sure the data is |
| 130 | +sanitized, protecting you from nasty attacks from others. The input |
| 131 | +library is loaded by default. At last, you insert our $data array into |
| 132 | +our database. |
| 133 | + |
| 134 | +Routing |
| 135 | +------- |
| 136 | + |
| 137 | +Before you can start adding news items into your CodeIgniter application |
| 138 | +you have to add an extra rule to config/routes.php file. Make sure your |
| 139 | +file contains the following. This makes sure CodeIgniter sees 'create' |
| 140 | +as a method instead of a news item's slug. |
| 141 | + |
| 142 | +:: |
| 143 | + |
| 144 | + $route['news/create'] = 'news/create'; |
| 145 | + $route['news/(:any)'] = 'news/view/$1'; |
| 146 | + $route['news'] = 'news'; |
| 147 | + $route['(:any)'] = 'pages/view/$1'; |
| 148 | + $route['default_controller'] = 'pages/view'; |
| 149 | + |
| 150 | +Now point your browser to your local development environment where you |
| 151 | +installed CodeIgniter and add index.php/news/create to the URL. |
| 152 | +Congratulations, you just created your first CodeIgniter application! |
| 153 | +Add some news and check out the different pages you made. |
0 commit comments