用php搭建个人博客(0)

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

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

就当做练习来做


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

前端用的bootstrap

上图

用php搭建个人博客(0)_第1张图片


嗯,不错。。。 很low,


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

用php搭建个人博客(0)_第2张图片

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

效果就是这样。。。

用php搭建个人博客(0)_第3张图片


哇,不忍直视。。。。


代码可以看虫师大神的。。。 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

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

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

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'
			 ';
	    }
	}

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

	    echo'
		 ';		
	}
    }
?>


Pages.php

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'
                         ';

        }

    }
?>

view部分

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

home.php  

    

Lucifer's blog

灰溜溜的程序猿。。。




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



你可能感兴趣的:(我的博客)