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


Posted in PHP onJune 18, 2015

--

-- 表的结构 `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();
      //加载我自己的类库
      $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['prev_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?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['prev_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?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 "数据删除失败!";
      }
    }
  }
?>

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="<?php echo site_url('article/article_add')?>" 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="<?php echo site_url('article/article_mod')?>" method="post">
标题:<input name="title" type="text" value="<?php echo $title;?>" /><br />
内容:<input name="content" type="text" value="<?php echo $content?>" /><br />
添加人:<input name="add_man" type="text" value="<?php echo $add_man;?>" /><br />
添加时间:系统自动记录<br />
状态:<input name="tag" type="radio" value="1" <?php if($tag==1)echo 'checked';?> />显示 <input name="tag" type="radio" value="0" <?php if($tag==0)echo 'checked';?> />隐藏<br />
<input type="submit" name="Submit" value="提交" />
<input type="hidden" value="<?php echo $id;?>" name="id" />
</form>
</body>
</html>

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

PHP 相关文章推荐
php批量删除数据
Jan 18 PHP
php md5下16位和32位的实现代码
Apr 09 PHP
一个php导出oracle库的php代码
Apr 20 PHP
php程序之die调试法 快速解决错误
Sep 17 PHP
解析PHP多种序列化与反序列化的方法
Jun 06 PHP
ThinkPHP整合百度Ueditor图文教程
Oct 21 PHP
PHP嵌套输出缓冲代码实例
May 12 PHP
WordPress中邮件的一些修改和自定义技巧
Dec 15 PHP
CI配置多数据库访问的方法
Mar 28 PHP
PHP实现文件下载【实例分享】
Apr 28 PHP
thinkPHP分页功能实例详解
May 05 PHP
用Laravel轻松处理千万级数据的方法实现
Dec 25 PHP
试用php中oci8扩展
Jun 18 #PHP
浅谈PDO的rowCount函数
Jun 18 #PHP
基于PHP实现的事件机制实例分析
Jun 18 #PHP
php使用MySQL保存session会话的方法
Jun 18 #PHP
php判断表是否存在的方法
Jun 18 #PHP
PHP实现HTTP断点续传的方法
Jun 17 #PHP
PHP实现在线阅读PDF文件的方法
Jun 17 #PHP
You might like
分享一个Laravel好用的Cache宏
2015/03/02 PHP
PHP中常见的缓存技术实例分析
2015/09/23 PHP
Yii2 GridView实现列表页直接修改数据的方法
2016/05/16 PHP
phpmailer绑定邮箱的实现方法
2016/12/01 PHP
PHP使用OB缓存实现静态化功能示例
2019/03/23 PHP
PHP中的Iterator迭代对象属性详解
2019/04/12 PHP
laravel实现一个上传图片的接口,并建立软链接,访问图片的方法
2019/10/12 PHP
JavaScript对象模型-执行模型
2008/04/28 Javascript
浅谈JavaScript编程语言的编码规范
2011/10/21 Javascript
js 数组去重的四种实用方法
2014/09/09 Javascript
js编写的treeview使用方法
2016/11/11 Javascript
基于node.js的fs核心模块读写文件操作(实例讲解)
2017/09/10 Javascript
微信小程序使用二次贝塞尔曲线画波浪
2018/12/25 Javascript
Nuxt.js 数据双向绑定的实现
2019/02/17 Javascript
解决Vue项目中tff报错的问题
2020/10/21 Javascript
[02:48]DOTA2英雄基础教程 暗夜魔王
2013/12/12 DOTA
[00:32]2018DOTA2亚洲邀请赛Secret出场
2018/04/03 DOTA
python 字典(dict)遍历的四种方法性能测试报告
2014/06/25 Python
Python实现把utf-8格式的文件转换成gbk格式的文件
2015/01/22 Python
在Python中使用Mako模版库的简单教程
2015/04/08 Python
Python处理Excel文件实例代码
2017/06/20 Python
Python对接 xray 和微信实现自动告警
2019/09/17 Python
pytorch+lstm实现的pos示例
2020/01/14 Python
python3.8与pyinstaller冲突问题的快速解决方法
2020/01/16 Python
如何使用python写截屏小工具
2020/09/29 Python
美国第二大团购网站:LivingSocial
2016/07/24 全球购物
Prototype如何实现页面局部定时刷新
2013/08/06 面试题
生产内勤岗位职责
2013/12/07 职场文书
大学优秀班主任事迹材料
2014/05/02 职场文书
青年志愿者活动方案
2014/08/17 职场文书
《中国梦我的梦》大学生演讲稿
2014/08/20 职场文书
省委召开党的群众路线教育实践活动总结大会报告
2014/10/21 职场文书
初中优秀学生评语
2014/12/29 职场文书
公司晚宴祝酒词
2015/08/11 职场文书
python tqdm用法及实例详解
2021/06/16 Python
SQL实现LeetCode(176.第二高薪水)
2021/08/04 MySQL