


[CI]CodeIgniter rapid development guide, cicodeigniter_PHP tutorial
[CI]CodeIgniter Rapid Development Guide, cicodeigniter
-------------------------- -------------------------------------------------- --------------------------
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 following shows the collaboration between the model, controller, and view.
<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 /><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

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function