This article will share with you the use of php to write simple article publishing programs, suitable for php Cainiao learning reference, hope to help you learn php. --
--Table structure 'Yi _ article'
--
CREATE TABLE IF NOT EXISTS `yi_article` ( `id` int(11) unsigned NOT NULL auto_increment, `title` varchar(256) NOT NULL, `content` mediumtext NOT NULL, `add_man` varchar(20) NOT NULL, `add_time` datetime NOT NULL, `views` int(11) NOT NULL, `tag` tinyint(4) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;
Article. php
<? Php class Article extends CI_Controller {public $ tips; function _ construct () {parent ::__ construct (); // load my own class library $ this-> load-> library ('myclass'); $ this-> load-> database (); $ this-> load-> helper ('URL'); $ this-> tips = array ('required' => '[% s] is required! ', 'Is _ unique' =>' this [% s] already exists. please enter another name! ', 'Min _ lenght' => '[% s] the minimum length is [% s]', 'max _ length' => '[% s] the maximum length is [% s]');} function index () {echo "here is the homepage of the article"; echo"
"; // Load the url helper function $ this-> load-> helper ('URL'); $ addr = site_url ('Article/article_list '); echo" view articles "; $ addr = site_url ('Article/article_page '); echo "view pagination";} function article_list () {echo "here is the article list "; // load the database model // $ this-> load-> model ('article _ Model'); // $ this-> article_model-> index (); // read all articles $ this-> load-> database (); echo"
"; $ Query = $ this-> db-> where (" id> ", 5)-> select ('Id, TITLE')-> from ('article ') -> order_by ('id', 'desc')-> limit (4)-> get (); $ info = $ query-> result_array (); // of course, you can use result () $ this-> myclass-> p ($ info); echo "the title of the first record :". $ info [0] ['title']; echo"
"; Echo" the title of the second record: ". $ info [1] ['id']; echo"
"; Echo" there are some records in the article table: ". $ this-> db-> count_all ('article'); echo"
"; Echo" a total of these records are queried this time :". $ query-> num_rows ();} function article_page ($ page = 1) {///$ config = array (); // The first step is to query the total number of records $ this-> load-> database (); $ config ['total _ rows '] = $ this-> db-> select (' * ')-> from ('article')-> count_all_results (); // number of records per page $ config ['per _ page'] = 5; // basic url $ this-> load-> helper ('URL '); $ config ['base _ url'] = site_url ('Article/article_page '); // number of displayed links $ config ['Num _ links'] = 100; // display the current page number in the address bar $ config ['use _ page_numbers '] = true; // define the homepage $ config ['first _ link'] = 'homepage '; // define the last page $ config ['last _ link'] = 'Last page'; // $ config ['next _ link'] = 'next page '; // Pack the pages. $ config ['full _ tag_open '] =''; $ Config ['full _ tag_close'] ='
'; // The second step loads the class library $ this-> load-> library ('pagination'); $ this-> pagination-> initialize ($ config ); echo $ this-> pagination-> create_links (); /// ///$ page = $ page? Intval ($ page): 1; $ start = ($ page-1) * $ config ['per _ page']; $ query = $ this-> db-> select ('*')-> from ('article')-> limit ($ config ['per _ page'], $ start); $ info = $ query-> get ()-> result_array (); $ this-> myclass-> p ($ info ); echo $ this-> pagination-> create_links (); // echo base_url ('ABC/def ');} protected function _ page ($ total_rows, $ per_page, $ base_url) {///$ config = array (); // The total number of records retrieved in step 1 // $ this-> load-> database (); // $ config ['total _ rows '] = $ total_rows; // number of records per page $ config ['per _ page'] = $ per_page; // basic url $ this-> load-> helper ('URL '); /// $ config ['base _ url'] = site_url ($ base_url); // number of displayed links $ config ['Num _ links '] = 100; // display the current page number in the address bar $ config ['use _ page_numbers '] = true; // define the homepage $ config ['first _ link'] = 'homepage '; // define the last page $ config ['last _ link'] = 'Last page'; // $ config ['next _ link'] = 'next page '; // Pack the pages. $ config ['full _ tag_open '] =''; $ Config ['full _ tag_close'] ='
'; // The second step loads the class library $ this-> load-> library ('pagination'); $ this-> pagination-> initialize ($ config ); return $ this-> pagination-> create_links (); /// //} function page ($ page = 1) {$ config ['per _ page'] = 5; $ page = $ page? Intval ($ page): 1; $ start = ($ page-1) * $ config ['per _ page']; $ query = $ this-> db-> select ('*')-> from ('article')-> limit ($ config ['per _ page'], $ start); $ info = $ query-> get ()-> result_array (); return $ info;} function article_add () {$ this-> load-> library ('form _ validation '); // start to set verification rules. // set_message can pass a one-dimensional array $ chinesetips = $ this-> tips; $ this-> form_validation-> set_message ($ chinesetips);/* $ this-> form_validation-> set_mes Sage ('required', '[% s] is required! '); $ This-> form_validation-> set_message ('is _ unique',' this [% s] already exists. please enter another name! '); $ This-> form_validation-> set_message ('min _ length',' [% s] minimum length is [% s] '); $ this-> form_validation-> set_message ('max _ length', '[% s] maximum length is [% s]'); */$ this-> form_validation-> set_rules ('title', 'title', 'trim | required | is_unique [article. title] | min_length [6] | max_length [12] '); $ this-> form_validation-> set_rules ('content', 'content', 'required '); $ this-> form_validation-> set_rules ('tag', 'status', 'required'); if ($ this-> M_validation-> run () = true) {echo "form verification successful! "; Print_r ($ this-> input-> post (); $ data = $ this-> input-> post (); unset ($ data ['submit ']); $ data ['add _ time'] = date ('Y-m-d H: I: s '); $ data ['View'] = '0'; $ st = $ this-> db-> insert ('article', $ data); if ($ st) {echo "data inserted successfully! "; Echo" New id: ". $ this-> db-> insert_id ();} // echo get_magic_quotes_gpc ();} else {echo" form verification failed! "; Echo"
"; Echo validation_errors () ;}} function article_add_viewer () {$ this-> load-> helper ('URL '); $ this-> load-> view ('article _ add');} function article_links () {$ addr = site_url ('Article/article_mod_viewer/19 '); echo "modify 19";} function article_mod_viewer ($ id) {if ($ id = "") {echo "no parameter passed"; exit ;} $ this-> load-> helper ('URL'); // query from the database $ query = $ this-> db-> select () -> from ('article')-> where ('id', $ id)-> get (); $ info = $ Query-> row_array (); print_r ($ info); $ this-> load-> view ('article _ MoD', $ info );} function abc ($ val) {$ this-> form_validation-> set_message ('ABC', ''); // p ($ val); return true ;} function article_mod () {$ this-> load-> library ('form _ validation '); // start setting verification rules // set_message can be used to pass a one-dimensional array $ chinesetips = $ this-> tips; $ this-> form_validation-> set_message ($ chinesetips ); $ this-> form_validation-> set_rules ('title', 'title','t Rim | required | min_length [6] | max_length [12] | callback_abc '); $ this-> form_validation-> set_rules ('content', 'content', 'required '); $ this-> form_validation-> set_rules ('tag', 'status', 'required'); if ($ this-> form_validation-> run () = true) {echo "form verification successful! "; Print_r ($ this-> input-> post (); $ data = $ this-> input-> post (); $ id = $ data ['id']; unset ($ data ['id']); unset ($ data ['submit ']); $ data ['add _ time'] = date ('Y-m-d H: I: s'); $ data ['View'] = '0 '; // p ($ data); $ st = $ this-> db-> where ('id', $ id)-> update ('article', $ data ); if ($ st) {echo "data modification succeeded";} else {echo "data modification failed";} else {echo "form verification failed! "; Echo"
"; Echo validation_errors () ;}} function article_del ($ id ='') {if ($ id = "") {// exit ('upload ID');} $ id = array (17,18, 19); $ this-> db-> where_in ('id', $ id) -> delete ('article'); $ st = $ this-> db-> affected_rows (); echo $ st; if ($ st) {echo "data deleted successfully! ";} Else {echo" an error occurred while deleting the data! ";}}?>
Article_add.php
No title 1
Article_mod.php
No title 1
The above is all the content of this article. I hope you will like it.