用php搭建个人博客(0)

前几天准备开始搭建个人博客,

=_=,  嗯,刚学完php基础,而且还很不牢固。。。

就当做练习来做


由于代码功底太渣。。。所以无耻的采用ci框架

前端用的bootstrap

上图



嗯,不错。。。 很low,


首先数据模型的构造 沿用ci教程的基础上参考了 虫师大神写的一篇php搭建简易博客,虽然是简易博客。。。但比我的史上最low复杂了不知多少倍。。


好吧。。其实是直接 拿来用的 本来有slug字段的,后来看下用id 就行了作用就是来 查看博文具体内容的

效果就是这样。。。



哇,不忍直视。。。。


代码可以看虫师大神的。。。 https://github.com/defnngj/ci_blog 他的文章 http://blog.csdn.net/ncafei/article/details/53202768


我把大部分时间花在后台管理上

先是登录验证,提交login表单 Login——model 的 check 方法 链接数据库验证,再用session记住我是管理员 ,Logout 销毁session数据


看ci的文档 第一次接触mvc之类的东西。。model view controller 用起来很方便 (嘿嘿)  

虽然是使用框架,但也暴露出我的很多问题,,写个表单都要对照资料,半吊子都算不上的水平。。。


所以我决定 完成这个 极简易(超low)版的博客 之后 还是得继续学习 html CSS javascript 得吧前端基础学扎实才能写出东西来


下面附上我的版本主要代码 


Blogs_model.php

<?php
    class Blogs_model extends CI_Model 
    {
	
	public function __construct()
        {
	    $this->load->database();
	}


	public function get_blogs($id = FALSE) 
	{
	    if ($id === FALSE) {
		$query = $this->db->get('blogs');
		return $query->result_array();
	    }


	    //更新点击数
	    $this->db->query("update blogs set hits=hits+1 where id='$id';");


	    $query = $this->db->get_where('blogs', array('id' => $id));
	    return $query->row_array();
    	}


	public function set_blogs() 
	{
	    $this->load->helper('url');


	    $slug = url_title($this->input->post('title'), 'dash', TRUE);
	
	    $date = date("Y-m-d H:i:s");


	    $data = array(
			'title' => $this->input->post('title'), 
			'text' => $this->input->post('text'),
			'date' => $date
			);
	    
	    return $this->db->insert('blogs', $data);
	}


        public function del_blogs($id = FALSE)
	{
	    $this->load->helper('url');


	    if ($id === FALSE)
	    {
		$query = $this->db->get('blogs');
		return $query->result_array();
	    }


	    $array = array(
		'id' => $id
	    );


	    return $this->db->delete('blogs', $array);
	}


    }
?>


Login_model.php

<?php
    class Login_model extends CI_model 
    {
	function __construct() 
	{
	    parent::__construct();
	    $this->load->database();
	}

	function check()
	{
	    $username = $this->input->post('username');
	    $password = $this->input->post('password');

	    $result = $this->db->query("select * from admin where username='".$username."' 
					 and password = sha1('".$password."')");
	
	    if (!$result)
	    {
		return FALSE;
	    }

	    if ($result->num_rows() > 0)
	    {
		return TRUE;
	    }
	    else
	    {
		return FALSE;
	    }
	}
    }
?>


控制类 Blogs.php

<?php
    class Blogs extends CI_Controller {

        public function __construct() 
	{
	    parent::__construct();
	    $this->load->model('blogs_model');
	    $this->load->helper('url_helper');
	    $this->load->library('session');
	}
	
 	public function index() 
	{
	    $data['blogs'] = $this->blogs_model->get_blogs();

	    $this->load->view('templates/header', $data);
	    $this->load->view('blogs/home', $data);
	    $this->load->view('templates/footer');
	}

	public function view($id = NULL) 
	{
	    $data['blogs_item'] = $this->blogs_model->get_blogs($id);
	
	    if (empty($data['blogs_item']))
	    {
		show_404();
	    }
	
	    $data['title'] = $data['blogs_item']['title'];
	
	    $this->load->view('templates/header');
	    $this->load->view('blogs/view', $data);
	    $this->load->view('templates/footer');
	}

	public function create() 
	{
	    $this->load->helper('form');
	    $this->load->library('form_validation');
	
	    $data['title'] = 'Create a new blog';

	    $this->form_validation->set_rules('title', 'Title', 'required');
	    $this->form_validation->set_rules('text', 'Text', 'required');

	    if ($this->form_validation->run() === FALSE)
	    {
		$this->load->view('templates/header', $data);
		$this->load->view('blogs/create');
		$this->load->view('templates/footer');
	    } 
	    else
	    {
		$this->blogs_model->set_blogs();
	 	echo'
			<script language="javascript"> 
				alert("create success!"); 
				window.location.href="http://localhost/myblog/index.php/blogs/create"; 
			</script> ';
	    }
	}

	public function delete($id = NULL)
	{
	    $this->blogs_model->del_blogs($id);
	    
	    $this->session->unset_userdata('admin');

	    echo'
		<script language="javascript"> 
		    alert("delete success!"); 
		    window.location.href="http://localhost/myblog/index.php/"; 
		</script> ';		
	}
    }
?>


Pages.php

<?php
    class Pages extends CI_Controller {

	public function __construct()
	{
	    parent::__construct();
	    $this->load->model('login_model');
	    $this->load->model('blogs_model');
	    $this->load->helper('url_helper');
	    $this->load->library('session');
	}
	
	public function view($page = 'home') 
	{
	    if (!file_exists(APPPATH.'views/pages/'.$page.'.php')) 
	    {
		show_404();
	    }

	    $data['title'] = ucfirst($page); // Capitalize the first letter

	    $this->load->view('templates/header', $data);
	    $this->load->view('pages/'.$page, $data);
	    $this->load->view('templates/footer', $data);	
	}

	public function login()
	{
	    $this->load->helper('form');
	    $this->load->library('form_validation');
	    $this->session->set_userdata('admin', FALSE);
	
	    $data['title'] = 'Login';
	    $data['blogs'] = $this->blogs_model->get_blogs();

	    $this->form_validation->set_rules('username', 'Username', 'required');
	    $this->form_validation->set_rules('password', 'Password', 'required');

	    if ($this->form_validation->run() === FALSE || $this->login_model->check() === FALSE)
	    {
		$this->load->view('templates/header', $data);
		$this->load->view('pages/login');
		$this->load->view('templates/footer');
	    }
	    else 
	    {
		$this->session->set_userdata('admin', TRUE);
		$this->load->view('templates/header');
		$this->load->view('pages/management', $data);
		$this->load->view('templates/footer');
	    }
	}

	 public function logout()
        {
            $this->session->unset_userdata('admin');
            echo'
                        <script language="javascript"> 
                                window.location.href="http://localhost/myblog/index.php/"; 
                        </script> ';

        }

    }
?>

view部分

因为 header.php 和 footer.php 是一样的,所以我直接写body内容

home.php  

    <div class="container">
      <div class="blog-header">
	<h1 class="blog-title">Lucifer's blog</h1>
	<p class="lead blog-description">灰溜溜的程序猿。。。</p>
      </div>
      <div class="row"> 
	<div class="col-sm-8 blog-main">
          <div class="blog-post">
	    <?php foreach ($blogs as $blogs_item): ?>
	    <h2 class-"blog-post-title"><?php echo $blogs_item['title']; ?></h2>
	    <p class="blog-post-meta">
              <?php echo $blogs_item['date']; ?>
	       by <a href="#">lucifer</a> Reading:<?php echo $blogs_item['hits']; ?>
	    </p>
            <p>
	      <?php echo $blogs_item['text']; ?>
	    </p>		    
            <p><a href="<?php echo site_url('blogs/view/'.$blogs_item['id']); ?>">View artical</a>
	    </P>
	    <hr>
            <?php endforeach; ?>
	  </div><!-- /.blog-post -->
	  <nav>
	    <ul class="pager">
	      <li><a href="#">Previous</a></li>
	      <li><a href="#">Next</a></li>
	    </ul>
	  </nav>
        </div><!-- /.blog-main -->
      </div><!-- /.row -->
    </div><!-- /.container -->



参考链接http://git.oschina.net/shuaibai123/thinkbjy



  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值