Codeigniter learning 2

Source: Internet
Author: User
1 Cache

/System/cache directory cleared, set the permission to 666,

$ This-> output-> cache (5 );

5 is how long you want your cache to last in minutes, that is, how long it will take to use static html files before the page is regenerated.

2 file auxiliary functions

This-> load-> helper ('file ');

R is read, W is write (Write File, overwrite existing data), and a is append (Write File, add after existing data ). Situation

Add a "+", for example, "A +", to open the file for read/write operations. "A" and "W" instead of "R" or "R +". If not

, Always create files

Write_file ('e:/filetest.txt ', 'Hello world', 'a + ');

Delete_files ('C:/mydirectory/'); Delete all files in the directory

3. File Upload

In System/application/config/upload. php

<? PHP if (! Defined ('basepath') Exit ('no direct Script Access allowed ');

$ Config ['upload _ path'] = 'uploads ';

?>

$ This-> load-> Library ('upload', $ config );

Types of files that can be uploaded by users. Set as follows:

$ Config ['allowed _ types'] = 'gif | JPG | PNG ';

Upload example :,

$ Config ['upload _ path'] = 'uploads ';

$ Config ['allowed _ types'] = 'gif | JPG | PNG ';

$ Config ['max _ size'] = '000000 ';

$ This-> load-> Library ('upload', $ config );

If (! $ This-> upload-> do_upload ('file '))

{

Echo $ this-> upload-> display_errors ();

}

4 crud routines:



Add and edit:

Function savenews ($ do)

{

If ($ DO = "add ")

{

$ DATA = array (

'Title' => $ this-> input-> post ('title '),

'Cid' => $ this-> input-> post ('cid '),

'Content' => $ this-> input-> post ('content '),

'Time' => (string) Date ('Y-m-J '),

);

$ This-> DB-> insert ('News', $ data );

If ($ this-> DB-> affected_rows ())

{

Return true;

}

Else

{

Return false;

}

}

Else if ($ DO = "edit ")

{

$ DATA = array (

'Title' => $ this-> input-> post ('title '),

'Cid' => $ this-> input-> post ('cid '),

'Content' => $ this-> input-> post ('content '),

);

$ This-> DB-> where ('id', $ this-> input-> post ('id '));

$ This-> DB-> Update ('News', $ data );

If ($ this-> DB-> affected_rows ())

{

Return true;

}

Else

{

Return false;

}

}

Else

{

Return false;

}

Delete:

// Delete a single News Record

If ($ ID! = 0)

{

// $ This-> DB-> where ('id', (INT) $ id );

// $ This-> DB-> Delete ('News ');

$ This-> DB-> Delete ('News', array ('id' => $ id ));

Return true;

}

// Batch Delete news

Else

{

$ This-> DB-> where_in ('id', $ this-> input-> post ('id '));

$ This-> DB-> Delete ('News ');

Return true;

}

5 session usage

Settings:

$ Query = $ this-> DB-> get ("admin ");

If ($ query-> num_rows ()> 0)

{

$ Session_data = array ('admin' => $ this-> input-> post ("username "));

$ This-> session-> set_userdata ($ session_data );

}

Determine whether a session exists:

If ($ this-> session-> userdata ("admin "))

{

Redirect ("admin/Index ");

}

Destroy session:

$ This-> session-> sess_destroy ();

6 across the script XSS-CLEAN:

$ DATA = $ this-> input-> xss_clean ($ data );

To enable automatic processing:

You can set it in the application/config. php file:

$ Config ['Global _ xss_filtering '] = true;

$ This-> input-> post ('some _ data', true); the second parameter is to set XSS filtering.

7 pages

$ This-> load-> Library ('pagination ');

$ Config ['base _ url'] = site_url ('admin/listnews ');

$ Config ['total _ rows '] = $ this-> DB-> count_all ('News ');

$ Config ['per _ page'] = '1 ';

$ Config ['num _ link'] = '6 ';

$ Config ['uri _ segment '] = 3;

$ Config ['full _ tag_open '] =' <Div class = "pagination"> ';

$ Config ['full _ tag_close '] =' </div> ';

$ Config ['first _ link'] = 'first ';

$ Config ['last _ link'] = 'last ';

$ This-> pagination-> initialize ($ config );

$ This-> DB-> select ('Id, title, Time ');

$ This-> DB-> order_by ('id', 'desc ');

$ Query = $ this-> DB-> get ('News', $ config ['per _ page'], $ this-> URI-> segment (3 ));

Return $ query;

$ This-> URI-> segment (3) indicates that the deviation is obtained from the 3rd segment address of the URL: Offset, which indicates the row from which the record is returned. First Parameter

(Limit) values from the $ config ['per _ page'] variable, while the second parameter uses the URL helper function to get the value from the third segment of the URL: $ this

-> URI-> segment (3), for example, www.your-site.com/index.php/class/function/id.

Id. The following url is allowed:

Http: // localhost /~ Yannick/pagination/index. php/books/index/10

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.