下载CodeIgniter.chm 里面有些基本的概念,后续也是必备的手册。
平台:windows7(64bit)。
依赖:(java/tomcat/php/...)
1. http://download.oracle.com/otn-pub/java/jdk/7u15-b03/jdk-7u15-windows-x64.exe 【安装】
2. http://jaist.dl.sourceforge.net/project/xampp/XAMPP%20Windows/1.8.3/xampp-win32-1.8.3-2-VC11-installer.exe 【安装后启动各服务】
3. http://www.codeigniter.com/
4. 按教程一步步来
2.x版本的构造函数有所区别:
public function __construct() { parent::__construct(); ........ }
5. mysql-phpmyadmin
mysql装好后是不需要密码的,我之前机器上独立的装过mysql,所以冲突,折腾了好一阵子。
参考:http://blog.sina.com.cn/s/blog_6e1333a70100v9c7.html
通过前端修改密码
6. 配置db
./config/database.php 如:
$db['ci_before']['hostname'] = '10.129.xxx.xxx'; $db['ci_before']['username'] = 'test'; $db['ci_before']['password'] = '123456'; $db['ci_before']['database'] = 'before_test'; $db['ci_before']['dbdriver'] = 'mysql'; $db['ci_before']['dbprefix'] = ''; $db['ci_before']['pconnect'] = FALSE; $db['ci_before']['db_debug'] = TRUE; $db['ci_before']['cache_on'] = FALSE; $db['ci_before']['cachedir'] = ''; $db['ci_before']['char_set'] = 'GBK'; $db['ci_before']['dbcollat'] = 'GBK_chinese_ci'; $db['ci_before']['swap_pre'] = ''; $db['ci_before']['autoinit'] = TRUE; $db['ci_before']['stricton'] = FALSE; $db['ci_plat']['hostname'] = '10.128.xxx.xxx'; $db['ci_plat']['username'] = 'ci_test'; $db['ci_plat']['password'] = 'ci_test'; $db['ci_plat']['database'] = 'ci_plat'; $db['ci_plat']['dbdriver'] = 'mysql'; $db['ci_plat']['dbprefix'] = ''; $db['ci_plat']['pconnect'] = FALSE; $db['ci_plat']['db_debug'] = TRUE; $db['ci_plat']['cache_on'] = FALSE; $db['ci_plat']['cachedir'] = ''; $db['ci_plat']['char_set'] = 'GBK'; $db['ci_plat']['dbcollat'] = 'GBK_chinese_ci'; $db['ci_plat']['swap_pre'] = ''; $db['ci_plat']['autoinit'] = TRUE; $db['ci_plat']['stricton'] = FALSE; $db['ci_plat']['port'] = 3306;
7. 从controller读db数据数据到views,如controllers/citest.php
<?php class CITest extends MY_Controller { public function test() { $this->load->database("ci_before"); #select * from t_system_info LIMIT 20, 10 $query = $this->db->get('t_system_info',10,20); $cur_data = array(); $idx = 0; echo "<br>"; foreach ($query->result() as $row) { echo $row->SystemId . "|"; $cur_data[$idx] = $row->SystemId; $idx += 1; } echo "</br>"; $param_data = array("ary_data" => $cur_data); $this->load->view("test.php", $param_data); } } ?>
则views/test.php为:
<?php echo "</br>-------------</br>"; echo $ary_data[0]."|".$ary_data[1]; echo "</br>"; echo "Hello World~~"; echo "</br>-------------</br>"; ?>
效果为:
【 控制器和视图通过post/get请求交互】
-------------------更新 2014.03.08-----------------
条件:这里可能用到了jquery,jquery是javascript的第三方库,只要下载并引用即可
views/xxx.php
<script src="/assets/js/jquery-2.1.0.js" type="text/javascript"></script>
controllers/xxx.php
#该函数属于控制器的成员方法,接收来自视图的post请求,计算一个简单的加减乘除结果,并post返回 public function calc() { $num1 = iconv("UTF-8","GBK", $_POST['num1']); $num2 = iconv("UTF-8","GBK", $_POST['num2']); $oper = iconv("UTF-8","GBK", $_POST['oper']); if ($oper == "plus") {$result=$num1+$num2;} if ($oper == "sub") {$result=$num1-$num2;} if ($oper == "muti") {$result=$num1*$num2;} if ($oper == "divi") {$result=$num1/$num2;} $aryRet = array("ret"=>$result); $DB1 = $this->load->database("mytest", True); mysql_query("set NAMES GKB"); $r_result = $DB1->query("show databases;"); $aryRet["dbdata"] = $r_result->result(); $DB1->close(); $this->output_data($aryRet); } public function output_data($out_data = array(), $type = 'json') { if (!isset($out_data['code'])) { $out_data['code'] = 0; } switch ($type) { // json格式 case 'json': echo json_encode($out_data); break; // jsonp格式 case 'jsonp': $str = (isset($_GET['callback'])) ? $_GET['callback'] . "(". json_encode($out_data).")" : json_encode($out_data); echo $str; break; // str格式 default: echo $out_data; } exit(); }
views/xxx.php 视图端的js主动发起post请求
function bt_calc_by_php() { var num1 = document.getElementById("num1").value; //获取到空间的值 input text var num2 = document.getElementById("num2").value; var oper = document.getElementById("operator").value; //获取元算符号,select //发送post请求并处理请求结果 $.ajax({ type:"POST", url:"/index.php/welcome/calc", dataType:'json', data:"num1="+num1+"&num2="+num2+"&oper="+oper, success:function(data){//处理返回结果 document.getElementById("result").value=data.ret;//设置页面的计算结果 } }); }
【调试点滴】
-------------------------update: 2014.03.08 -----------------------
一般使用chrome进行调试,右键->审查元素
.调试页面元素的值
调试交互过程