CodeIgniterのチュートリアルをやってみた。
mavenちゃんの気分が優れないから、気分転換に久しぶりにPHPを組んでみた。
PHPのフレームワークの一つである「CodeIgniter」を触ってみたよ。
うん、軽い。いろんな意味で。
公式のサイトのチュートリアル( やっつけのブログです )に基づいて作成したソースをだらっとはっつけます。
僕もチュートリアルに負けじとやっつけなエントリーを書きます。
チュートリアル概要
DBを用いて簡易なブログを作成して、
ブログ毎にコメントを表示するアプリケーション
テーブルの作成 ( MySQL )
「ブログのエントリー用のテーブル」
create table entry(
id int not null auto_increment,
title varchar(128),
body text,
primary key(id)
);
「エントリーにひもづくコメントテーブル」
create table comments(
id int not null auto_increment,
entry_id int,
body text,
author varchar(100),
primary key(id)
);
テストデータを挿入
mysql> insert into entry values(1,'test title1','test body1');
mysql> insert into entry values(2,'test title2','test body2');
ここまで、DBの準備。
こっから、CodeIgniterの設定
データベース設定
/system/application/config/database.php
db['default']['hostname'] = "localhost";
$db['default']['username'] = "*****";
$db['default']['password'] = "+++++";
$db['default']['database'] = "-----";
$db['default']['dbdriver'] = "mysql";
コントローラ
/system/application/controllers/blog.php
<?php class Blog extends Controller { function Blog(){ parent::Controller(); //ヘルパの追加 $this->load->helper('url'); $this->load->helper('form'); } function index() { $data['title'] = "My Real Title"; $data['heading'] = "My Real Heading"; $data['query'] = $this->db->get('entry'); //select * from entry; $this->load->view('blog_view', $data); } function comments() { $data['title'] = "comment Title"; $data['heading'] = "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']); } } ?>
ブログ一覧ページ
system/application/views/blog_view.php
<html> <head> <title><?php echo $title;?></title> </head> <body> <h1><?php echo $heading;?></h1> <table border=1> <tr> <th>タイトル</th> <th>内容</th> <th>コメント</th> </tr> <?php foreach($query->result() as $row): ?> <tr> <td><?=$row->title ?></td> <td><?=$row->body ?></td> <td><?=anchor('blog/comments/'.$row->id,'Comments'); ?></td> </tr> <?php endforeach; ?> </body> </html>
コメント表示ページ
system/application/views/comment_view.php
<html> <head> <title><?php echo $title;?></title> </head> <body> <h1><?php echo $heading;?></h1> <?php if ($query->num_rows() > 0 ): ?> <table border=1> <tr> <th>コメント人</th> <th>コメント</th> </tr> <?php foreach($query->result() as $row): ?> <tr> <td><?=$row->author ?></td> <td><?=$row->body ?></td> </tr> <?php endforeach; ?> <?php endif; ?> <p><?=anchor('blog','back to blog'); ?></p> <table border=1> <?=form_open('blog/comment_insert'); ?> <?=form_hidden('entry_id',$this->uri->segment(3)); ?> <tr> <td>コメント人</td><td><input type="text" name="author"></td> </tr> <tr> <td>コメントの内容</td> <td><textarea name="body" rows="10"></textarea></td> </tr> <tr> <td colspan="2"><input type="submit" value="こめんと"></td> </tr> </table> </body> </html>
さて、ちょっと休憩してmavenちゃんやります。
参考URL
http://codeigniter.com/tutorials/
$this->db->where('entry_id',$this->uri->segment(3));
http://codeigniter.jp/user_guide_ja/libraries/uri.html
$this->db->get('entry');
http://codeigniter.jp/user_guide_ja/database/active_record.html