]> jfr.im git - z_archive/KronOS.git/blob - video/user_guide/tutorial/create_news_items.html
Adding CodeIgniter version
[z_archive/KronOS.git] / video / user_guide / tutorial / create_news_items.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3 <head>
4
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 <title>CodeIgniter Features : CodeIgniter User Guide</title>
7
8 <style type='text/css' media='all'>@import url('../userguide.css');</style>
9 <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
10
11 <script type="text/javascript" src="../nav/nav.js"></script>
12 <script type="text/javascript" src="../nav/prototype.lite.js"></script>
13 <script type="text/javascript" src="../nav/moo.fx.js"></script>
14 <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
15
16 <meta http-equiv='expires' content='-1' />
17 <meta http-equiv= 'pragma' content='no-cache' />
18 <meta name='robots' content='all' />
19 <meta name='author' content='ExpressionEngine Dev Team' />
20 <meta name='description' content='CodeIgniter User Guide' />
21
22 </head>
23 <body>
24
25 <!-- START NAVIGATION -->
26 <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
27 <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
28 <div id="masthead">
29 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30 <tr>
31 <td><h1>CodeIgniter User Guide Version 2.1.3</h1></td>
32 <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
33 </tr>
34 </table>
35 </div>
36 <!-- END NAVIGATION -->
37
38
39 <!-- START BREADCRUMB -->
40 <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41 <tr>
42 <td id="breadcrumb">
43 <a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
44 <a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45 <a href="index.html">Tutorial</a> &nbsp;&#8250;&nbsp;
46 Create news items
47 </td>
48 <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
49 </tr>
50 </table>
51 <!-- END BREADCRUMB -->
52
53 <br clear="all" />
54
55
56 <!-- START CONTENT -->
57 <div id="content">
58
59
60 <h1>Tutorial - Create news items</h1>
61
62 <p>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.</p>
63
64 <h2>Create a form</h2>
65
66 <p>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 <dfn>application/views/news/create.php</dfn>.</p>
67
68 <textarea class="textarea" style="width:100%" cols="50" rows="16">
69 <h2>Create a news item</h2>
70
71 &lt;?php echo validation_errors(); ?>
72
73 &lt;?php echo form_open('news/create') ?>
74
75 <label for="title">Title</label>
76 <input type="input" name="title" /><br />
77
78 <label for="text">Text</label>
79 <textarea name="text">&lt;/textarea><br />
80
81 <input type="submit" name="submit" value="Create news item" />
82
83 &lt;/form>
84 </textarea>
85
86 <p>There are only two things here that probably look unfamiliar to you: the <dfn>form_open()</dfn> function and the <dfn>validation_errors()</dfn> function.</p>
87
88 <p>The first function is provided by the <a href="../helpers/form_helper.html">form helper</a> and renders the form element and adds extra functionality, like adding a hidden <a href="../libraries/security.html">CSFR prevention field</a>. The latter is used to report errors related to form validation.</p>
89
90 <p>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 <a href="../libraries/form_validation.html">form validation</a> library to do this.</p>
91
92 <pre>
93 public function create()
94 {
95 $this->load->helper('form');
96 $this->load->library('form_validation');
97
98 $data['title'] = 'Create a news item';
99
100 $this->form_validation->set_rules('title', 'Title', 'required');
101 $this->form_validation->set_rules('text', 'text', 'required');
102
103 if ($this->form_validation->run() === FALSE)
104 {
105 $this->load->view('templates/header', $data);
106 $this->load->view('news/create');
107 $this->load->view('templates/footer');
108
109 }
110 else
111 {
112 $this->news_model->set_news();
113 $this->load->view('news/success');
114 }
115 }
116 </pre>
117
118 <p>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 <var>set_rules()</var> 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.</p>
119
120 <p>CodeIgniter has a powerful form validation library as demonstrated above. You can read <a href="../libraries/form_validation.html">more about this library here</a>.</p>
121
122 <p>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 <strong>and</strong> passed all the rules, the model is called. After this, a view is loaded to display a success message. Create a view at <dfn>application/view/news/success.php</dfn> and write a success message.</p>
123
124 <h2>Model</h2>
125
126 <p>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:</p>
127
128 <pre>
129 public function set_news()
130 {
131 $this->load->helper('url');
132
133 $slug = url_title($this->input->post('title'), 'dash', TRUE);
134
135 $data = array(
136 'title' => $this->input->post('title'),
137 'slug' => $slug,
138 'text' => $this->input->post('text')
139 );
140
141 return $this->db->insert('news', $data);
142 }
143 </pre>
144
145 <p>This new method takes care of inserting the news item into the database. The third line contains a new function, <dfn>url_title()</dfn>. This function - provided by the <a href="../helpers/url_helper.html">URL helper</a> - 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.</p>
146
147 <p>Let's continue with preparing the record that is going to be inserted later, inside the <var>$data</var> array. Each element corresponds with a column in the database table created earlier. You might notice a new method here, namely the <dfn>post()</dfn> method from the <a href="../libraries/input.html">input library</a>. 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 <var>$data</var> array into our database.</p>
148
149 <h2>Routing</h2>
150
151 <p>Before you can start adding news items into your CodeIgniter application you have to add an extra rule to <dfn>config/routes.php</dfn> file. Make sure your file contains the following. This makes sure CodeIgniter sees 'create' as a method instead of a news item's slug.</p>
152
153 <pre>
154 $route['news/create'] = 'news/create';
155 $route['news/(:any)'] = 'news/view/$1';
156 $route['news'] = 'news';
157 $route['(:any)'] = 'pages/view/$1';
158 $route['default_controller'] = 'pages/view';
159 </pre>
160
161 <p>Now point your browser to your local development environment where you installed CodeIgniter and add <dfn>index.php/news/create</dfn> to the URL. Congratulations, you just created your first CodeIgniter application! Add some news and check out the different pages you made.</p>
162
163 </div>
164 <!-- END CONTENT -->
165
166
167 <div id="footer">
168 <p>
169 Previous Topic:&nbsp;&nbsp;<a href="news_section.html">News section</a>
170 &nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
171 <a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
172 <a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
173 Next Topic:&nbsp;&nbsp;<a href="conclusion.html">Conclusion</a>
174 </p>
175 <p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 - 2012 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">EllisLab, Inc.</a></p>
176 </div>
177
178 </body>
179 </html>