Home Backend Development PHP Tutorial php编写简单的文章发布程序_PHP

php编写简单的文章发布程序_PHP

May 30, 2016 am 08:44 AM
php Article release

--

-- 表的结构 `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 ;

Copy after login

article.php

<&#63;php
  class Article extends CI_Controller{
    public $tips;
    function __construct(){
      parent::__construct();
      //加载我自己的类库
      $this->load->library('myclass');
      $this->load->database();
      $this->load->helper('url');
      $this->tips=array(
        'required'=>' [%s] 是必须填写的!',
        'is_unique'=>'此 [%s] 已经存在,请输入其它名称!',
        'min_lenght'=>' [%s] 最小长度为 [%s]',
        'max_length'=>'[%s] 最大长度为 [%s]'
      );
    }
    function index(){
      echo "这里是文章的首页";
      echo "<br />";
      //加载url辅助函数
      $this->load->helper('url');
      $addr=site_url('article/article_list');
      echo "<a href='$addr'>查看文章</a>";
      $addr=site_url('article/article_page');
      echo "<a href='$addr'>查看分页</a>";
    }
    function article_list(){
      echo "这里是文章列表";
      //加载数据库模型
      //$this->load->model('article_model');
      //$this->article_model->index();
      //读取所有的文章
      $this->load->database();
      echo "<br />";
      $query=$this->db->where("id >",5)->select('id,title')->from('article')->order_by('id','desc')->limit(4)->get();
      $info=$query->result_array();//当然你可以用result()
      $this->myclass->p($info);
      echo "第一条记录的标题:".$info[0]['title'];
      echo "<br />";
      echo "第二条记录的标题:".$info[1]['id'];
      echo "<br />";
      echo "表article中共有这么些记录:".$this->db->count_all('article');
      echo "<br />";
      echo "本次共查询出这么些条记录:".$query->num_rows();
    }
    function article_page($page=1){
      ///////////////////////////////////
      $config=array();
      //第一步查询出总记录数
      $this->load->database();
      $config['total_rows']=$this->db->select('*')->from('article')->count_all_results();
      //每页记录数
      $config['per_page']=5;
      //基础url
      $this->load->helper('url');
      $config['base_url']=site_url('article/article_page');
      //显示的链接数
      $config['num_links']=100;
      //在地址栏显示当前页码
      $config['use_page_numbers']=true;
      //定义首页
      $config['first_link']='首页';
      //定义末页
      $config['last_link']='尾页';
      //
      $config['next_link']='下一页';
      //把分页包起来
      $config['full_tag_open']='<p>';
      $config['full_tag_close']='</p>';
      //第二步加载类库
      $this->load->library('pagination');
      $this->pagination->initialize($config);
      echo $this->pagination->create_links();
      /////////////////////////////////////
      $page=$page&#63;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();
      //第一步查询出总记录数
      //$this->load->database();////
      $config['total_rows']=$total_rows;
      //每页记录数
      $config['per_page']=$per_page;
      //基础url
      $this->load->helper('url');////
      $config['base_url']=site_url($base_url);
      //显示的链接数
      $config['num_links']=100;
      //在地址栏显示当前页码
      $config['use_page_numbers']=true;
      //定义首页
      $config['first_link']='首页';
      //定义末页
      $config['last_link']='尾页';
      //
      $config['next_link']='下一页';
      //把分页包起来
      $config['full_tag_open']='<p>';
      $config['full_tag_close']='</p>';
      //第二步加载类库
      $this->load->library('pagination');
      $this->pagination->initialize($config);
      return $this->pagination->create_links();
      /////////////////////////////////////
    }
    function page($page=1){
      $config['per_page']=5;
      $page=$page&#63;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');
      //开始设置验证规则
      //set_message可以传一个一维数组
      $chinesetips=$this->tips;
      $this->form_validation->set_message($chinesetips);
      /*
      $this->form_validation->set_message('required', ' [%s] 是必须填写的!');
      $this->form_validation->set_message('is_unique', '此 [%s] 已经存在,请输入其它名称!');
      $this->form_validation->set_message('min_length', ' [%s] 最小长度为 [%s]');
      $this->form_validation->set_message('max_length', ' [%s] 最大长度为 [%s]');
      */
      $this->form_validation->set_rules('title','标题','trim|required|is_unique[article.title]|min_length[6]|max_length[12]');  
      $this->form_validation->set_rules('content','内容','required');
      $this->form_validation->set_rules('tag','状态','required');
      if($this->form_validation->run()==true){
        echo "表单验证成功!";
        print_r($this->input->post());
        $data=$this->input->post();
        unset($data['Submit']);
        $data['add_time']=date('Y-m-d H:i:s');
        $data['views']='0';
        $st=$this->db->insert('article',$data);
        if($st){
          echo "数据插入成功!";
          echo "新的id为:".$this->db->insert_id();
        }
        //echo get_magic_quotes_gpc();
      }else{
        echo "表单验证失败!";
        echo "<br />";
        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 "<a href='$addr'>修改19</a>";
    }
    function article_mod_viewer($id){
      if($id==""){
        echo "没有传递参数";
        exit;
      }
      $this->load->helper('url');
      //从数据库中查出来
      $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');
      //开始设置验证规则
      //set_message可以传一个一维数组
      $chinesetips=$this->tips;
      $this->form_validation->set_message($chinesetips);
      $this->form_validation->set_rules('title','标题','trim|required|min_length[6]|max_length[12]|callback_abc');  
      $this->form_validation->set_rules('content','内容','required');
      $this->form_validation->set_rules('tag','状态','required');
      if($this->form_validation->run()==true){
        echo "表单验证成功!";
        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['views']='0';
        //p($data);
        $st=$this->db->where('id',$id)->update('article',$data);
        if($st){
          echo "数据修改成功";
        }else{
          echo "数据修改失败";
        }
      }else{
        echo "表单验证失败!";
        echo "<br />";
        echo validation_errors();
      }
    }
    function article_del($id=''){
      if($id==""){
        //exit('请传id');
      }
      $id=array(17,18,19);
      $this->db->where_in('id',$id)->delete('article');
      $st=$this->db->affected_rows();
      echo $st;
      if($st){
        echo "数据删除成功!";
      }else{
        echo "数据删除失败!";
      }
    }
  }
&#63;>
Copy after login

article_add.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="Www.XiaZaiBa.Com" />
<title>无标题 1</title>
</head>
<body>
<form name="form1" action="<&#63;php echo site_url('article/article_add')&#63;>" method="post">
标题:<input name="title" type="text" value="" /><br />
内容:<input name="content" type="text" value="" /><br />
添加人:<input name="add_man" type="text" value="" /><br />
添加时间:系统自动记录<br />
状态:<input name="tag" type="radio" value="1" />显示 <input name="tag" type="radio" value="0" />隐藏<br />
<input type="submit" name="Submit" value="提交" />
</form>
</body>
</html>

Copy after login

article_mod.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="Www.XiaZaiBa.Com" />
<title>无标题 1</title>
</head>
<body>
<form name="form1" action="<&#63;php echo site_url('article/article_mod')&#63;>" method="post">
标题:<input name="title" type="text" value="<&#63;php echo $title;&#63;>" /><br />
内容:<input name="content" type="text" value="<&#63;php echo $content&#63;>" /><br />
添加人:<input name="add_man" type="text" value="<&#63;php echo $add_man;&#63;>" /><br />
添加时间:系统自动记录<br />
状态:<input name="tag" type="radio" value="1" <&#63;php if($tag==1)echo 'checked';&#63;> />显示 <input name="tag" type="radio" value="0" <&#63;php if($tag==0)echo 'checked';&#63;> />隐藏<br />
<input type="submit" name="Submit" value="提交" />
<input type="hidden" value="<&#63;php echo $id;&#63;>" name="id" />
</form>
</body>
</html>
Copy after login

以上所述就是本文的全部内容了希望大家能够喜欢。

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles