Welcome Guest, Not a member yet? Register   Sign In
Codeingniter 2 tutorial code for the blog
#1

[eluser]fernandoch[/eluser]
Hello, here is the code for the blog tutorial. This version is without models. Look some posts below for the version with models.

application\controllers\blog.php

Code:
<?php
class Blog extends CI_Controller {

function Blog()
{
  parent::__construct();
  $this->load->helper('url');
  $this->load->helper('form');
}

function index()
{  
  $data['title']= "My Blog Title";
  $data['heading']= "My Blog Heading";
  $data['query'] = $this->db->get('entries');

  $this->load->view('blog_view',$data);
}

function comments()
{
  $data['title']= "My Comment Title";
  $data['heading']= "My Comment Heading";
  $this->db->where('entry_id', $this->uri->segment(3));
  $data['query'] = $this->db->get('comments');

  $this->load->view('comment_view',$data);
}

function comment_insert()
{
  $this->db->insert('comments',$_POST);
  redirect('blog/comments/'.$_POST['entry_id']);  
}  
}  
?>

application\views\blog_view.php

Code:
<html>
<head>

<title><?php echo $title?></title>

</head>

<body>

<h1>&lt;?php echo $heading?&gt;</h1>


&lt;?php   foreach($query->result() as $row):?&gt;

<h3>&lt;? echo $row->title; ?&gt;</h3>
<p>&lt;? echo $row->body; ?&gt;</p>

<p>&lt;?=anchor('blog/comments/'.$row->id, 'Comments');?&gt;</p>

<hr>

&lt;?php endforeach;?&gt;  

&lt;/body&gt;
&lt;/html&gt;

application\views\comment_view.php

Code:
&lt;html&gt;

&lt;head&gt;
&lt;title&gt;&lt;?php echo $title?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

<h1>&lt;?php echo $heading?&gt;</h1>

&lt;?php if($query->num_rows() > 0): ?&gt;

&lt;?php   foreach($query->result() as $row):?&gt;

  <p>&lt;? echo $row->body; ?&gt;</p>
  <p>&lt;? echo $row->author; ?&gt;</p>

  <hr>

&lt;?php endforeach;?&gt;

&lt;?php endif;?&gt;

<p>&lt;?=anchor('blog', 'Back to Blog');?&gt;</p>

&lt;?=form_open('blog/comment_insert');?&gt;
&lt;?=form_hidden('entry_id', $this->uri->segment(3));?&gt;

<p>&lt;textarea name="body" rows="10"&gt;&lt;/textarea></p>
<p>&lt;input type="text" name="author"/&gt;&lt;/p>
<p>&lt;input type="submit" value="Submit Comment"/&gt;&lt;/p>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;

Look at the next post for database and configuration files.




Theme © iAndrew 2016 - Forum software by © MyBB