[CI]CodeIgniter Rapid Development Guide
------------------------------------------------- -------------------------------------------------- ------
The strongest feeling since using CI is its thorough MVC design. For example: In the application/modesl directory, write our model operations and uniformly inherit CI_Model.
Only logic is written in the controller, and the database cannot be directly operated. Data is required to directly call the model, and finally the template is called.
The collaboration between models, controllers, and views are shown below.
<span>/*</span><span>* * 用户模型, 完整CURD示例 * @Chenwei </span><span>*/</span> <span>class</span> User_model <span>extends</span><span> CI_model<br>{ </span><span>public</span> <span>function</span><span> __construct() { parent</span>::<span>__constrcut(); } </span><span>/*</span><span>* * 查询用户信息, 这里不建议使用单一id参数作为条件, 为了便于控制器自己组装条件复用此模型方法 * @param array 格式如: $where = array('id'=>1); * @return array </span><span>*/</span> <span>public</span> <span>function</span> userInfo(<span>$where = array()</span><span>) { </span><span>if</span>(<span>$where</span> && <span>is_array</span>(<span>$where</span><span>)) { </span><span>$res</span> = <span>$this</span>->db->select('id, username, age')->where(<span>$where</span>)->get('users'<span>); </span><span>return</span> <span>$res</span>->result_array(); <span>//</span><span>以二维数组形式返回结果</span> <span> } </span><span>else</span><span> {<br> $res = $this->db->select('id, username, age')->get('users');<br> </span><span>return</span> <span>$res->result_array()</span><span>; } } </span><span>/*</span><span>* * 添加用户 * @param array 格式如: $data = array('username'=>'Chenwei', 'age'=>'18'); * @reteurn bool </span><span>*/</span> <span>public</span> <span>function</span> userAdd(<span>$data</span><span>) { </span><span>if</span>(<span>$data</span> && <span>is_array</span>(<span>$data</span><span>)) { </span><span>$bool</span> = <span>$this</span>->db->insert('users', <span>$data</span><span>); </span><span>return</span> <span>$bool</span><span>; } </span><span>else</span><span> { </span><span>return</span> <span>false</span><span>; } } </span><span>/*</span><span>* * 删除用户 * @param int $id * @reteurn bool </span><span>*/</span> <span>public</span> <span>function</span> userDel(<span>$id</span><span>) { </span><span>if</span>(<span>$id</span><span>) { </span><span>$where</span> = <span>array</span>('id'=><span>$id</span><span>); </span><span>$bool</span> = <span>$this</span>->db->where(<span>$where</span>)->delete('users'<span>); </span><span>return</span> <span>$bool</span><span>; } </span><span>else</span><span> { </span><span>return</span> <span>false</span><span>; } } </span><span>/*</span><span>* * 修改用户 * @param array $where 条件 * @param array $data 新数据 * @reteurn bool </span><span>*/</span> <span>public</span> <span>function</span> userEdit(<span>$where</span>, <span>$data</span><span>) { </span><span>if</span>(<span>$where</span> && <span>is_array</span>(<span>$where</span><span>)) { </span><span>$bool</span> = <span>$this</span>->db->where(<span>$where</span>)->update('users', <span>$data</span><span>); </span><span>return</span> <span>$bool</span><span>; } </span><span>else</span><span> { </span><span>return</span> <span>false</span><span>; } } } </span><span>/*</span><span>* * 几点注意: * 1. 模型类名字User_model首字母大写, 其余字母小写, 继承基础模型类CI_Model * 2. 类文件名 application/models/user_model.php * 3. 控制器中如何载入此模型 : <br> $this->load->model('User_model', 'user'); 这是以user为对象名引入;<br> $this->load->model('User_model'); 这是默认以User_model为对象名引入. 模型文件支持子目录;<br> 如果类文件在application/models/blog/user_model.php中, 可以这样引入: $this->load->model('blog/User_model'); * 4. 如果有需要, 你可以设置自动加载, 在 application/config/autoload.php文件中.<br> * 5. 如果没有设置自动连接数据库, 加在模型的时候可以设置连接, 像这样 $this->load->model('User_model', '', TRUE); </span><span>*/<br>Ps:<br>这里是一个联合查询的例子, 有需要可以尝试:<br>$res = $this->db->select('p.id, p.uid, p.order_no, p.amount, p.pay_way, p.pay_type, p.pay_bank, p.pay_time, p.goods_type, p.contact_tel, p.detail_desc, p.add_time, u.username')->from('payment as p')->join('users as u', 'p.uid = u.id')->order_by('p.id', 'desc')->get();</span>
<span>/*</span><span>* * 用户控制器, CURD示例 * @Chenwei </span><span>*/</span> <span>class</span> Users <span>extends</span><span> CI_Controller { </span><span>public</span> <span>function</span><span> __construct() { parent</span>::<span>__construct(); </span><span>$this</span>->load->model('User_model', 'user'<span>); } </span><span>/*</span><span>* * 用户列表 </span><span>*/</span> <span>public</span> <span>function</span><span> index() { </span><span>$data</span>['user_list'] = <span>$this</span>->user-><span>userInfo(); </span><span>$this</span>->load->view('user_list', <span>$data</span><span>); //调用模板, 并将数据输出到前台 } </span><span>/*</span><span>* * 添加用户 </span><span>*/</span> <span>public</span> <span>function</span><span> user_add() { </span><span>$data</span> = <span>array</span><span>( </span>'username'=><span>$this</span>->input->post('name'<span>); </span>'age'=><span>intval</span>(<span>$this</span>->input->post('age'<span>)); ); </span><span>$bool</span> = <span>$this</span>->user->userAdd(<span>$data</span><span>); </span><span>if</span>(<span>$bool</span><span>) { </span><span>$this</span>->show_tips('操作成功 !'<span>); } </span><span>else</span><span> { </span><span>$this</span>->show_tips('操作失败 !'<span>); } } </span><span>/*</span><span>* * 修改用户 </span><span>*/</span> <span>public</span> <span>function</span><span> user_edit() { </span><span>$id</span> = <span>$this</span>->input->post('id'<span>); </span><span>$data</span> = <span>array</span><span>( </span>'username'=><span>$this</span>->input->post('name'<span>); </span>'age'=><span>intval</span>(<span>$this</span>->input->post('age'<span>)); ); </span><span>if</span>(<span>$id</span><span>) {<br> $where = array('id'=>$id);<br> </span><span>$bool</span> = <span>$this</span>->user->userEdit(<span>$where</span>, <span>$data</span><span>); </span><span>if</span>(<span>$bool</span><span>) { </span><span>$this</span>->show_tips('操作成功 !'<span>); } </span><span>else</span><span> { </span><span>$this</span>->show_tips('操作失败 !'<span>); } } </span><span>else</span><span> { </span><span>$this</span>->show_tips('非法操作 !'<span>); } } </span><span>/*</span><span>* * 删除用户 </span><span>*/</span> <span>public</span> <span>function</span><span> user_del() { </span><span>$id</span> = <span>$this</span>->input->post('id'<span>); </span><span>$bool</span> = <span>$this</span>->user->userDel(<span>$id</span><span>); </span><span>if</span>(<span>$bool</span><span>) { </span><span>$this</span>->show_tips('操作成功 !'<span>); } </span><span>else</span><span> { </span><span>$this</span>->show_tips('操作失败 !'<span>); } } } </span><span>/*</span><span>*<br> * 几点注意: * 1. 控制器文件在 application/controller/users.php , 支持子目录 * 2. 控制器名首字母必须大写, 且必须继承CI_Controller * 3. 前后台权限控制都在application/core/MY_Controller.php文件中,<br> 定义两个控制器, 分别用于前台和后台, 继承CI_Controller , 其余都只需继承这两个自定义的控制器即可. * 4. 定义默认控制器, 在 application/config/route.php </span><span>*/</span>
<span>/*</span><span>* * 视图层 示例 * @Chenwei </span><span>*/</span> <?<span>php </span><span>$this</span>->load->view('header'<span>); </span>?> <!-- 简单的输出 --> <div> <table> <?php <span>if</span>(<span>$user_list</span>):?> <?php <span>foreach</span>(<span>$user_list</span> <span>as</span> <span>$v</span>):?> <tr><td><?=<span>$v</span>['username'];?></td></tr> <?php <span>endforeach</span>;?> <?php <span>endif</span>;?> </table> </div> <?<span>php </span><span>$this</span>->load->view('header'<span>); </span>?> <span>/*</span><span>* * 几点注意: * 1. 模板中可以直接使用控制器中分配的变量, 使用CI系统的所有函数和方法. * 2. 开启CI短标签支持后, 即使php未开启支持, CI也会帮我们自动解析, 可以放心使用. </span><span>*/</span>
There may be manual errors, please do not copy and use the above code directly; for more practical usage of CI, you can consult the CI manual at any time.
Link: http://www.cnblogs.com/farwish/p/3991419.html
@黑eyedpoet
The above introduces the [CI]CodeIgniter Rapid Development Guide, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

After problems occurred in many centralized exchanges, more and more cryptocurrency investors began to transfer assets to cold wallets to reduce the risks posed by centralized exchanges. This article will introduce Trezor, the world's earliest cold wallet provider. Since the first cold wallet was launched in 2014, it has been sold in many countries around the world. Trezor's products include Model One launched in 2014 and the advanced version Model T launched in 2018. The following will continue to introduce the differences between these two products and other cold wallets. What is Trezor cold wallet? In 2014, Trezor launched the first cold wallet ModelOne. In addition to common BTC, ETH, USDT and other currencies, the wallet also supports more than 1,000 other currencies.

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

Practical Guide to Where Method in Laravel Collections During the development of the Laravel framework, collections are a very useful data structure that provide rich methods to manipulate data. Among them, the Where method is a commonly used filtering method that can filter elements in a collection based on specified conditions. This article will introduce the use of the Where method in Laravel collections and demonstrate its usage through specific code examples. 1. Basic usage of Where method

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

In Docker, the permission problem of the mounting directory can usually be solved by the following method: adding permission-related options when using the -v parameter to specify the mounting directory. You can specify the permissions of the mounted directory by adding: ro or :rw after the mounted directory, indicating read-only and read-write permissions respectively. For example: dockerrun-v/host/path:/container/path:roimage_name Define the USER directive in the Dockerfile to specify the user running in the container to ensure that operations inside the container comply with permission requirements. For example: FROMimage_name#CreateanewuserRUNuseradd-ms/bin/

How to use the Where method in Laravel collection Laravel is a popular PHP framework that provides a wealth of functions and tools to facilitate developers to quickly build applications. Among them, Collection is a very practical and powerful data structure in Laravel. Developers can use collections to perform various operations on data, such as filtering, mapping, sorting, etc. In collections, the Where method is a commonly used method for filtering the collection based on specified conditions.
