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ã®è¨å®
DBã¨èªåæ¥ç¶
/system/application/config/autoload.php
$autoload['libraries'] = array('database');
ãã¼ã¿ãã¼ã¹è¨å®
/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