相關鏈接:
關于CodeIgniter的入門請參照這篇文章:[PHP]框架教程:CodeIgniter框架的簡易使用
使用的平臺是SAE:[SAE]免費服務器:新浪云服務器SAE的注冊與使用
BAE中的MySQL使用:[PHP]如何在百度(BAE)和新浪(SAE)的云平臺使用PHP連接MySQL并返回結果數(shù)據(jù)1.首先是控制器部分,Blog.php作為Controller即控制器:
<?php header('Content-Type:text/html;charset=utf-8');class Blog extends CI_Controller { function __construct() { //繼承父類的構造方法,不寫報錯 parent::__construct(); //加載框架中的相關helper $this->load->helper('url'); $this->load->helper('form'); } function index(){ //為即將跳轉的頁面設置相關數(shù)據(jù) $data['title']="My Blog Title"; $data['heading']="My Blog Heading"; $data['todo']=array('eat','sleep','call'); //連接數(shù)據(jù)庫并返回查詢結果 $sql = "SELECT * FROM `Entries` LIMIT 0, 30 "; //初始化MySQL數(shù)據(jù)庫 $mysql= new SaeMysql(); $sqlData = $mysql->getData($sql); //將數(shù)據(jù)庫的結果傳入data中 $data['query']=$sqlData; //使用變量$data向目標網(wǎng)頁傳入數(shù)據(jù) $this->load->view('blog_view',$data); } function comments(){ //為即將跳轉的頁面設置相關數(shù)據(jù) $data['title']="My Comment Title"; $data['heading']="My Comment Heading"; //連接數(shù)據(jù)庫并返回查詢結果 $sql = "SELECT * FROM `Comments` where `entry_id`=".$this->uri->segment(3); //初始化MySQL數(shù)據(jù)庫 $mysql= new SaeMysql(); $sqlData = $mysql->getData($sql); //將數(shù)據(jù)庫的結果傳入data中 $data['query']=$sqlData; //使用變量$data向目標網(wǎng)頁傳入數(shù)據(jù) $this->load->view('comment_view',$data); } function comment_insert(){ //插入POST提交的評論數(shù)據(jù)到MySQL中 $sql = "INSERT INTO `Comments` (`entry_id`, `body`, `author`) VALUES ('".$_POST['entry_id']."', '".$_POST['body']."', '".$_POST['author']."');"; //初始化MySQL數(shù)據(jù)庫 $mysql= new SaeMysql(); $mysql->runSql($sql); redirect('blog/comments/'.$_POST['entry_id']); }}?>
<html><head><title><?php echo $title?></title></head><body><h1><?php echo $heading?></h1><?php //輸出從數(shù)據(jù)庫中讀取到的文章列表foreach($query as $key=>$value): ?><h3><?=$value['title']?></h3></br><p><?=$value['body']?></p></br><p><?=anchor('blog/comments/'.$value['id'],'Comments')/*插入評論的超鏈接*/?></p><hr><?php endforeach; ?></body></html>
<html><head><title><?php echo $title?></title></head><body><h1><?php echo $heading?></h1><?php if(count($query)>0): /*確保有數(shù)據(jù)返回*/?> <?php //輸出從數(shù)據(jù)庫中讀取到的文章列表 foreach($query as $key=>$value): ?> <p><?=$value['body']?></p></br> <p><?=$value['author']?></p></br> <hr> <?php endforeach; ?><?php endif; ?><p><?=anchor('blog','Back to Blog')/*返回博客頁面*/?></p><?/*提交表單,跳轉到blog的comment_insert方法*/?><?=form_open('blog/comment_insert');?><?=form_hidden('entry_id',$this->uri->segment(3));?><p><textarea name="body" rows ="10"></textarea></p><p><input type="text" name="author"/></p><p><input type="Submit" value="Submit"/></p></form></body></html>
下一篇 php隨機生成字符串一些方法總結