search
HomeBackend DevelopmentPHP Tutorialphp编写简单的文章发布程序_PHP

--

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

<&#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;>

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>

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>

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

Statement
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
How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

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.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

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 vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

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.

Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Apr 17, 2025 am 12:22 AM

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.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Apr 17, 2025 am 12:06 AM

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: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

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 and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

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.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

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.

See all articles

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment