Table of Contents
ci framework (2), ci framework
php CI 框架怎从数据库中取值(一个二维数组)以表格的形式显示到view层
php CI框架问题?小弟是初学者
Home Backend Development PHP Tutorial ci framework (2), ci framework_PHP tutorial

ci framework (2), ci framework_PHP tutorial

Jul 13, 2016 am 10:23 AM
Two-dimensional array

ci framework (2), ci framework

Custom SQL statement                                                                                                                                   When the provided API cannot meet our requirements for SQL statements, we usually write SQL statements ourselves. CI also provides a relatively powerful general SQL API that can meet our needs.

<span>$res</span>=<span>$this</span>->db->select('id,name'<span>)
    </span>->from('表名'<span>)
    </span>->whrer('id >=',5)<span>//</span><span>注意id后面要有个空格</span>
    ->limit(3,2<span>)<span>//这里与sql的limit是顺序是反的</span>
    </span>->order_by('id desc '<span>)
    </span>->get();<span>//</span><span>翻译成sql语句</span>
<span>var_dump</span>(<span>$res</span>-><span>result());
</span><span>echo</span> <span>$this</span>->db->last_query();<span>//</span><span>先是最近一条SQL</span>
Copy after login
Custom expansion controller                                                                                     

Create a new MY_Controller.php in application/core At the same time, you need to configure it in application/config/config.php:

<span>class</span> MY_Controller <span>extends</span><span> CI_Controller
{
    </span><span>public</span> <span>function</span><span> __construct()
    {
        parent</span>::<span>__construct();
        </span><span>//</span><span>一定呀先调用父类的构造方法
        //登录验证、权限验证、其他操作。。。</span>
<span>    }
}</span>
Copy after login
Custom extension model                                                                             

Create user_model.php in application/models

<span>$config</span>['subclass_prefix'] = 'MY_';
Copy after login

Call custom model in controller application/controllers:

When loading the model, you can give the model a name:
<span>class</span> User_model <span>extends</span><span> CI_Model
{
    </span><span>public</span> <span>function</span><span> getAll()
    {
        </span><span>$res</span> = <span>$this</span>->db->get('表名'<span>);
        </span><span>return</span> <span>$res</span>-><span>result();
    }
}</span>
Copy after login

Url related function                                                                                   
<span>class</span> User <span>extends</span><span> MY_Controller
{
    </span><span>public</span> <span>function</span><span> index()
    {
        </span><span>$this</span>->load->model('User_model');<span>//</span><span>调用以类名为主,而不是文件名</span>
Copy after login
During form verification, data needs to be passed to the controller. How to write actions accurately and scalably? Call the API:
<span>$list</span> = <span>$this</span>->User_model->getAll();<span>//</span><span>调用模型获取数据</span>
Copy after login
<span>$this</span>->load->view('user/index',<span>array</span>('list'=><span>$list</span>));<span>//</span><span>加载视图</span><span>  
    }   
}</span>
Copy after login

In the user/add.php view:

<span>$this</span>->load->model('User_model','user');<span>//</span><span>调用以类名为主,而不是文件名        </span>
<span>$list</span> = <span>$this</span>->user->getAll();<span>//</span><span>调用模型获取数据</span>
Copy after login

If it is the index.php directory, use: This API.

At the same time, loading the URL every time is very troublesome. You can also set it to load automatically and modify it in config/config.php:

<span>public</span> <span>function</span><span> addView()
{
    </span><span>$this</span>->load->helper('url');<span>//</span><span>为了不把表单传递的地址写死,用url函数</span>
    <span>$this</span>->load->view('user/add'<span>);
}</span>
Copy after login
This automatic loading may not be available in later versions.

<span><</span><span>form </span><span>action</span><span>="<?php echo site_url('user/insert'); ?>"</span><span> method</span><span>="post"</span><span>></span>
<span><!--</span><span> ........</span><span>--></span>
<span></</span><span>form</span><span>></span>
Copy after login

Routing                                                🎜>

base_url();
Copy after login

$route

['rouxx/showxx/([d]+).html'] = 'rou/show/$1';

//

<span>$config</span>['helper'] = <span>array</span>('url');
Copy after login
Insert this sentence

<页> Pagling

Some parameters you must know

ci framework (2), ci framework_PHP tutorial How many records are there in total How many records should be on one page How many pages in total How many pagination links should be displayed before and after the current page

  • 设置一些CI分页类基本参数
<span>//</span><span>总条数</span>
<span>$config</span>['total_rows'<span>]
</span><span>//</span><span>一页显示几条</span>
<span>$config</span>['per_page'<span>]
</span><span>//</span><span>定义当前页的前后各有几个数字链接</span>
<span>$config</span>['num_links'<span>]
</span><span>//</span><span>定义没有分页参数,主URL</span>
<span>$config</span>['base_url']
Copy after login
  • 调用CI的分页类
<span>$this</span>->load->library('pagination');
Copy after login
  • 执行分页方法
<span>$this</span>->pagination->initialize(<span>$config</span>);
Copy after login
  • 输出分页链接
<span>echo</span> <span>$this</span>->pagination->create_links();
Copy after login
  • 查询部分数据(limit)
<span>echo</span> <span>$this</span>->db->limit(<span>$num</span>,<span>$start</span>);  <span>//</span><span>从$start查$num条</span>
Copy after login
<?<span>php 
</span><span>if</span> ( ! <span>defined</span>('BASEPATH')) <span>exit</span>('No direct script access allowed'<span>);

</span><span>class</span> Page <span>extends</span><span> CI_Controller {

    </span><span>public</span> <span>function</span><span> user_add(){
        </span><span>$this</span>->load->model('test_m'<span>);

        </span><span>for</span> (<span>$i</span> = 1;<span>$i</span> <= 100;<span>$i</span>++<span>){
            </span><span>$name</span> = 'u'.<span>$i</span><span>;
            </span><span>$arr</span> = <span>array</span>("usid"=><span>$i</span>,"uname"=><span>$name</span>,"upass"=>123456<span>);
            </span><span>$this</span>->test_m->user_insert(<span>$arr</span><span>);

        }
    }

    </span><span>public</span> <span>function</span><span> pagelist(){
        </span><span>$this</span>->load->model('test_m'<span>);
        </span><span>$user</span> = <span>$this</span>->test_m-><span>user_select_all();
        </span><span>$allnum</span> = <span>count</span>(<span>$user</span><span>);
        </span><span>$pagenum</span> = 20<span>;

        </span><span>$config</span>['total_rows'] = <span>$allnum</span><span>;
        </span><span>$config</span>['per_page'] = <span>$pagenum</span><span>;
        </span><span>$config</span>['num_links'] = 3<span>;
        </span><span>$config</span>['base_url'] = "/CI/index.php/page/pagelist"<span>;
        </span><span>$config</span>['use_page_numbers'] = <span>true</span><span>;

        </span><span>$this</span>->load->library('pagination'<span>);
        </span><span>$this</span>->pagination->initialize(<span>$config</span><span>);

        </span><span>var_dump</span>(<span>$this</span>->pagination-><span>create_links());
        </span><span>echo</span> <span>$this</span>->pagination-><span>create_links();

        </span><span>echo</span> "<br />"<span>;
        </span><span>$id</span> = <span>$this</span>->uri->segment(3);  <span>//</span><span>获得url第三段字符</span>
        <span>$id</span> =<span>$id</span> ? <span>$id</span>:1<span>;
        </span><span>$start</span> = (<span>$id</span> - 1) * <span>$pagenum</span><span>;
        </span><span>$list</span> = <span>$this</span>->test_m->user_select_limit(<span>$start</span>,<span>$pagenum</span><span>);
        </span><span>var_dump</span>(<span>$list</span><span>);
    }
}</span>
Copy after login

上传文件

视图 /views/up.php:

<span><</span><span>html</span><span>></span>
    <span><</span><span>form </span><span>action</span><span>="ci/CodeIgniter_2.2.0/index.php/upload/up"</span><span> method</span><span>="post"</span><span> enctype</span><span>="multipart/form-data"</span><span>></span>
        <span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="upfile"</span> <span>/></span>
        <span><</span><span>input </span><span>type</span><span>="submit"</span><span> name</span><span>="sub"</span><span> value</span><span>="提交"</span> <span>/></span>
    <span></</span><span>form</span><span>></span>
<span></</span><span>html</span><span>></span>
Copy after login

控制器:

  • 定义一个数组,设置一些与上传相关的参数
<span>$config</span>['upload_path'] = './uploads/'<span>;
</span><span>//</span><span>设置允许上传的类型</span>
<span>$config</span>['allowed_types'] = 'gif|jpg|png'<span>;
</span><span>$config</span>['max_size'] = '100'<span>;
</span><span>//</span><span>如果是图片还可以设置最大高度和宽度</span>
<span>$config</span>['max_height'] = 768<span>;
</span><span>$config</span>['max_width'] = 1024;
Copy after login

还可以设置其他的一些额外参数,详细看用户手册。

  • 调用CI的上传通用类,并执行上传
<span>//</span><span>upload为调用的类名,全小写</span>
<span>$this</span>->load->library('upload',<span>$config</span><span>);
</span><span>//</span><span>如果上传框的name写的是userfile,那就不用传参数了,如果不是,把name的值传进去</span>
<span>$this</span>->upload->do_upload('上传框的name');
Copy after login
  • 接收出错信息或成功信息
<span>//</span><span>出错信息</span>
<span>$error</span> = <span>array</span>('error' => <span>$this</span>->upload-><span>display_error());
</span><span>//</span><span>成功信息</span>
<span>$data</span> = <span>array</span>('upload_data' => <span>$this</span>->upload->data());
Copy after login
<?<span>php 
</span><span>if</span> ( ! <span>defined</span>('BASEPATH')) <span>exit</span>('No direct script access allowed'<span>);

</span><span>class</span> Upload <span>extends</span><span> CI_Controller {
    </span><span>//</span><span>显示带表单的视图</span>
    <span>public</span> <span>function</span><span> index(){
        </span><span>$this</span>->load->view('up'<span>);
    }
    </span><span>//</span><span>显示上传信息</span>
    <span>public</span> <span>function</span><span> up(){
        </span><span>$config</span>['upload_path'] = './uploads/'<span>;
        </span><span>$config</span>['allowed_types'] = 'gif|jpg|png'<span>;
        </span><span>$config</span>['max_size'] = "2000"<span>;
        </span><span>$this</span>->load->library('upload',<span>$config</span><span>);
        </span><span>//</span><span>打印成功或错误的信息</span>
        <span>if</span>(<span>$this</span>->upload->do_upload('upfile'<span>))
        {
            </span><span>$data</span> = <span>array</span>("upload_data" => <span>$this</span>->upload-><span>data());
            </span><span>var_dump</span>(<span>$data</span><span>);
        }
        </span><span>else</span><span>
        {
            </span><span>$error</span> = <span>array</span>("error" => <span>$this</span>->upload-><span>display_errors());
            </span><span>var_dump</span>(<span>$error</span><span>);
        }
    }
}</span>
Copy after login

Session

利用CI类实现session登录

  • 修改配置文件(config.php)
<span>//</span><span>生成一个随机不重复的字符串走位加密的key保存到config.php的encryption_key中</span>
<span>$config</span>['encryption_key']='adb8bf6d0ac4e17b42a80941582497a4';
Copy after login
  • 加载SESSION类
<span>$this</span>->load->library('session');
Copy after login
  • 创建SESSION
<span>$array</span> = <span>array</span>('id'=>3,'name'=>'jack'<span>);
</span><span>$this</span>->session->set_userdata(<span>$array</span>);
Copy after login
  • 查看SESSION
<span>$this</span>->session->userdata(session名);
Copy after login
  • 删除SESSION
<span>$this</span>->session->unset_userdata('SESSION名');
Copy after login

ci framework (2), ci framework_PHP tutorial$config['sess_cookie_name'] = 'ci_session'; $config['sess_expiration'] = 7200; $config['sess_expire_on_close'] = FALSE; $config['sess_encrypt_cookie'] = TRUE $config['sess_use_database'] = FALSE; $config['sess_table_name'] = 'ci_sessions'; $config['sess_match_ip'] = FALSE; $config['sess_match_useragent'] = TRUE; $config['sess_time_to_update'] = 300;

  • 一次性数据,只能读取一次
<span>//</span><span>设置</span>
<span>$this</span>->session->set_flashdata('test','aaaaa'<span>);
</span><span>//</span><span>读取</span>
<span>$test</span> = <span>$this</span>->session->flashdata('test');
Copy after login

php CI 框架怎从数据库中取值(一个二维数组)以表格的形式显示到view层

控制层
function test_func(){
//获取model页面需要的两个参数
$competition_id = $_GET["competition_id"];
$report_class = $_GET["report_class"];
$this->load->model("Action"); //引入model
$data["head"] = $this->Action->get_report_item($competition_id, $report_class); //引用model的函数
$this->load->view("test_result",$data); //将结果显示在test_result.php页面中
}
view层:


添加结果显示</h3>
//此处选择了循环输出从控制层传输的结果



字段名称(含义)</td> //该td中显示的是你从数据库、即model层中获取到的数据的含义,想显示多少,显示哪个,在这里确认
</tr>

test; ?></td>
</tr>
</table>
echo “123”;
}?>
</div>
 

php CI框架问题?小弟是初学者

You can use $title directly in the view file
It is CI. According to each item in $data, you can declare a variable and call it directly in the view
This is how the CI architecture works. I don’t know how to declare it. Take a closer look

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/838913.htmlTechArticleci framework (2), ci framework customizes SQL statements when the provided API cannot meet our requirements for SQL statements At that time, we usually wrote the SQL statements ourselves, and CI also provided a more powerful one...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to convert a two-dimensional php array into a one-dimensional array How to convert a two-dimensional php array into a one-dimensional array Aug 03, 2023 am 11:14 AM

How to convert a php array from two dimensions to a one-dimensional array: 1. Use loop traversal to traverse the two-dimensional array and add each element to the one-dimensional array; 2. Use the "array_merge" function to merge multiple arrays into An array. Pass the two-dimensional array as a parameter to the "array_merge" function to convert it into a one-dimensional array; 3. Using the "array_reduce" function, you can process all the values ​​in the array through a callback function and finally return a result.

How to use the array_sum function in PHP to calculate the sum of elements in a column of a two-dimensional array How to use the array_sum function in PHP to calculate the sum of elements in a column of a two-dimensional array Jun 26, 2023 pm 12:45 PM

In PHP programming, the array_sum function is a very practical function that can calculate the sum of all elements in an array. However, when we need to calculate the sum of a column of elements in a two-dimensional array, we may encounter some trouble. This article will introduce how to use the array_sum function in PHP to calculate the sum of elements in a column of a two-dimensional array. First, we need to understand the concept of two-dimensional arrays. A two-dimensional array is an array containing multiple arrays, which can be regarded as a table. Each array represents a table

How to reverse a two-dimensional array in php How to reverse a two-dimensional array in php Dec 26, 2022 am 09:38 AM

How to reverse a two-dimensional array in php: 1. Create a php sample file; 2. Define a two-dimensional array; 3. Reverse the array through the "array_reverse($a,true);" function; 4. Use "print_r" to print Just reverse the two-dimensional array.

How to get the value of a specified column in a two-dimensional array using the array_column function in PHP How to get the value of a specified column in a two-dimensional array using the array_column function in PHP Jun 26, 2023 pm 01:32 PM

In PHP programming, we often need to operate on arrays, including obtaining the value of a specified column. PHP provides a very convenient function - array_column, which can help us quickly obtain the value of a specified column in a two-dimensional array. This article will introduce how to use the array_column function. Basic usage of array_column function: array_column(array$array,mixed$column_key[

How to convert 2D array to 1D array in PHP How to convert 2D array to 1D array in PHP Jul 07, 2023 pm 06:42 PM

How to convert a two-dimensional array to a one-dimensional array in PHP In PHP development, you often encounter scenarios where you need to convert a two-dimensional array into a one-dimensional array. This article will introduce several common methods to help you complete this task easily. Method 1: Use loop traversal The simplest and most straightforward method is to use a loop to traverse a two-dimensional array and add each element to a new one-dimensional array. Here is a code example using this method: functionflattenArray($array){$result

Detailed explanation of PHP 5.5 functions: How to use the array_column function to extract a certain column in a two-dimensional array Detailed explanation of PHP 5.5 functions: How to use the array_column function to extract a certain column in a two-dimensional array Jul 30, 2023 am 08:45 AM

Detailed explanation of PHP5.5 functions: How to use the array_column function to extract a certain column in a two-dimensional array. In the PHP5.5 version, the array_column function was introduced. It is a very practical function that can extract a specified column of data from a two-dimensional array. This comes in handy when working with large amounts of data, allowing us to quickly get the data we need. The basic syntax of the array_column function is as follows: arrayarray_column(array$

Does php have two-dimensional arrays? Does php have two-dimensional arrays? Aug 03, 2023 pm 02:45 PM

PHP has a two-dimensional array, which is a special type of array that can store other arrays as elements. The declaration and access of a two-dimensional array are very simple. You can use the "array" function to create a two-dimensional array and use indexing or association. Arrays, as their elements, are very useful in practical programming and can be used to process various complex data structures.

In C programming, working with 2D arrays at runtime In C programming, working with 2D arrays at runtime Sep 13, 2023 pm 11:29 PM

Question Write a C program that uses runtime compilation to calculate the sum and product of all elements in a two-dimensional array. Solution runtime compilation or initialization is also known as dynamic allocation. Allocating memory at execution time (runtime) is called dynamic memory allocation. The functions calloc() and malloc() support dynamic memory allocation. The functions calloc() and malloc() support dynamic memory allocation. p>In this program, we will calculate the sum of all elements of a 2D array and the product of all elements at runtime. Logic is used to calculate the sum of all elements in a 2D array - printf("Sumarrayis:");for(i=0;i<2;i++){&amp

See all articles