原理: 引入模板引擎类,根据其语法规范,将网页制作成模板文件。

规范:
1. 一套模板风格以一个目录名放在View目录下;
2. 模板名称组成: 控制器名_功能名.html 例如user_closet.html

注意事项:
1.模板的开发规范根据不用模板引擎而不同。
2.KFC会将一些全局变量自动赋值到模板文件中以供使用,这些全局变量为:网站基本信息 $GLOBALS['gSiteInfo']和 语言包$GLOBALS['gLang']

范例:

使用phpTemplate引擎的模板:

header.html文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?=$webcharset?>" />
<title><?=$site_name?></title>
<link rel="stylesheet" href="<?=$image_site_url?>/css/personal.css" type="text/css" />
</head>


其他文件

<? include "header.html";?>

    <div class="menu">
      <ul>
        <li><a href="?action=defaults&view=defaults" id="menu1" class="menuvisited">显示资料</a></li>
        <li><a href="?action=defaults&view=editinfo" id="menu2">修改资料</a></li>
        <li><a href="?action=defaults&view=changepwd" id="menu3">修改密码</a></li>
      </ul>
    </div>
    
  </div>
  <div id="main">
    <? include "left.html";?>
    <div id="profileinfo">
      <ul>
        <li>用 户 名:<?=$loginuser['user_name']?></li>
        <li>昵  称:<?=$loginuser['user_nickname']?></li>
        <li>性  别:<? if($loginuser['user_gender']==0) echo "男生";if($loginuser['user_gender']==1) echo "女生";?></li>
        <li>地  区:<?=$loginuser['user_city']?></li>
        <li>保密邮箱:<?=$loginuser['user_email']?></li>
      </ul>
    </div>
  </div>
</div>
</div>
<!--end content -->
<? include "footer.html";?>



原理: 分析业务逻辑,抽象出接口,开发底层的业务类,提供给控制层使用。

规范:
1. 模型文件命名为 foo.class.php;
2. 类名跟文件名"."号的最前头一致, class foo{};

API:
resource parent::dbConnect(array $options);

注意事项:
由于数据库操作类有多种选择adodb,peardb,pdo,database,所以开发人员要熟悉这些类的使用方法。

类书写范例

<?php
/*
* 需要数据库操作的要 继承 Model
*  
*/
class UserWealthManage extends Model {
  // php5 的写法
  private  $mydb="";
  
  /**
  * 选择php5 构造函数
  *
  */
  function __construct(){
    $this->mydb = parent::dbConnect($GLOBALS['gDataBase1']);
  }
  /**
  * 或者php4 构造函数,这里只是为了说明KFC框架类库本身兼容php4,php5,
       * 但是应用项目最好采用php5的类规范来开发。
  *
  */
  function UserWealthManage(){
    $this->mydb = parent::dbConnect($GLOBALS['gDataBase1']);
  }
  /**
  * 获取用户
  *@params int $user_id
  *@return mixed
  */
  function GetUserWealth($user_id){
    $n=floor($user_id/100000);
    $table = "user_".($n+1)."0w";
    $query = "select * from $table where user_id=".$user_id;
    $user_wealth = $this->mydb->getAll($query);
    if($user_wealth!==false){
      return $user_wealth;
    }else{
      return false;
    }
    
  }
  /**
  * 解析字符串
  *@params string $str
  *@return array
  */
  function ParseItemStr($str){
    if(!empty($str)){
      if(substr($str,-1,1)==';') $str = substr($str,0,-1);
      $arrs = array();
      $array = array();
      $arrs = explode(";",$str);
      foreach ($arrs as $k=>$v){
        $array[$k] = explode(",",$v);      
      }
      unset($arrs);
      return $array;
    }
  }
  
       function __destruct(){
           unset($this->mydb);
      }
  
}



?>



轻量级数据库类database.class.php API

//初始化参数
class database($database_type,$host,$database,$user,$password,$port=false,$dsn=false);

//连接数据库
boolen connect(void);

//返回false 或者 连接资源
mixed query(string $sql_statement, array $array ='');

// 执行结果
boolen execute(string $sql_statement, array $array ='')

//返回根据从结果集取得的行生成的数组,如果没有更多行则返回 FALSE。
mixed fetchRow(resource $result)

//只返回一行
mixed getRow(resource $result);

// 取得结果集中行的数目
mixed countRows(resource result);

// 取得所有数据
array getAll(string $sql_statement, array $array ='');


<?php
// 应用举例
$db = new database($options['type'],$options['host'],$options['dbname'],$options['user'],
    $options['passwd'],$options['port']);

// 获取结果
$result = $db->query("select * from user where user_id=?", array($user_id));
while($row = $db->fetchRow($result)){
  $array[] = $row;
}

// 取得行数
$num = $db->countRow($result);

// 执行操作
$r = $db->execute("update user set user_name='ddd' where user_id='{$user_id}'");
if($r){
  return true;
else
   return false;

// 取得一行
$row = $db->getRow("select * from user where user_id=?", array($user_id));

// 取得所有
$array = $db->getAll("select * from user where 1");



?>
原理:利用面向对象的思维,将功能点定义为类的方法;将应用功能模块定义为一个类;

规范:
1. 控制器文件命名为 foo.class.php;
2. 类名跟文件名"."号的最前头一致,class foo{};
3. 类的方法命名采用"view_"或者"op_" 小写开头; 剩下功能描述部分(功能名)只能为大小写字母和数字组成,如view_home1 , op_saveForm
 *不带view_和op_ 开头的类方法不会做为应用项目系统的权限点;

API 接口:
// 权限验证
void   parent::authenticate();

// 引入业务模型
object $this->includeModel(string $model_name);

// 引入外部组件
object $this->addComponent($com_name);

// 赋值到模板,跟smarty一样
void   $this->assign('var',$value);

// 显示模板
void   $this->display($tpl_name);

// 显示提示信息
void   show_message(string $message);

// 返回
void   goback();

// 跳转到其他页面
void   Redirect(string $url,int $type);
 
 类书写范例

 <?php
 class foo extends Controller{
 
 /**
  * _constructor KFC自带的仿构造函数,会在类实例化时自动执行
  * @access public
  * @return void
  */
 function _constructor(){
      // 权限验证
      parent::authenticate();
 }
 /**
  * URL访问:?action=foo&view=home&var=1111
  * @access public
  * @return void
  */
     
 function view_home(){
       
    // 初始化外部变量
    $var = isset($_GET['var'])?$_GET['var']:'';
     
      // 引入业务模型
        $userManage = $this->includeModel("UserManage");
        $userinfo = $userManage->getUserInfo(22);


     // 引入外部组件
     $phpmailer = $this->addComponent('phpmailer');
 
       
   // 赋值到模板,跟smarty一样
     $this->assign("var",$var);
     // 显示模板
     $this->display("foo_home.html");
 }
 /**
  * 处理POST提交的操作:在form表单需要插入
  * <input type="hidden" name="action" value="foo">
  * <input type="hidden" name="op" value="saveForm">
  * @access public
  * @return void
  */
 function op_saveForm(){
    // $KFC_POST 获取了$_POST的所有数据;
    global $KFC_POST;

    print_r($KFC_POST);

    //....
    // 实际业务编程

   

    // 完成后需要跳转 几个内置函数可以参考使用

    // 显示提示信息
    show_message("操作成功!");
    // 返回
    goback();
    // 相当于 show_message_goback("操作成功!");

    //或者跳转到其他页面
    //类型1 使用php的header;类型2 使用js的location.href;类型3 使用setTimeout跳转;
    Redirect("http://www.google.cn",1);

 }

 function otherFunction(){
   
      // 编程

    return ;
 }

 }
 ?>
javascript 技巧集 (ZT)
Posted in Javascript on 2007/08/09 / 评论(0) »
事件源对象
event.srcElement.tagName
event.srcElement.type
捕获释放
event.srcElement.setCapture();  
event.srcElement.releaseCapture();  
事件按键
event.keyCode
event.shiftKey
event.altKey
event.ctrlKey
事件返回值
event.returnValue
鼠标位置
event.x
event.y
窗体活动元素
document.activeElement
绑定事件
document.captureEvents(Event.KEYDOWN);
访问窗体元素
document.all("txt").focus();
document.all("txt").select();
窗体命令
document.execCommand
窗体COOKIE
document.cookie
菜单事件
document.oncontextmenu
创建元素
document.createElement("SPAN");  
根据鼠标获得元素:
document.elementFromPoint(event.x,event.y).tagName=="TD
document.elementFromPoint(event.x,event.y).appendChild(ms)  
窗体图片
document.images[索引]
窗体事件绑定
document.onmousedown=scrollwindow;
元素
document.窗体.elements[索引]
对象绑定事件
document.all.xxx.detachEvent('onclick',a);
插件数目
navigator.plugins
取变量类型
typeof($js_libpath) == "undefined"
下拉框
下拉框.options[索引]
下拉框.options.length
查找对象
document.getElementsByName("r1");
document.getElementById(id);
定时
timer=setInterval('scrollwindow()',delay);
clearInterval(timer);
UNCODE编码
escape() ,unescape
父对象
obj.parentElement(dhtml)
obj.parentNode(dom)
交换表的行
TableID.moveRow(2,1)
替换CSS
document.all.csss.href = "a.css";
并排显示
display:inline
隐藏焦点
hidefocus=true
根据宽度换行
style="word-break:break-all"
自动刷新

<meta HTTP-EQUIV="refresh" CONTENT="8;URL=http://www.kakapo.cn">

简单邮件
 
快速转到位置
obj.scrollIntoView(true)


anchors
网页传递参数
location.search();
可编辑
obj.contenteditable=true
执行菜单命令
obj.execCommand
双字节字符
/[^\x00-\xff]/
汉字
/[\u4e00-\u9fa5]/
让英文字符串超出表格宽度自动换行
word-wrap: break-word; word-break: break-all;
透明背景

<IFRAME src="1.htm" width=300 height=180 allowtransparency></iframe>

获得style内容
obj.style.cssText
HTML标签
document.documentElement.innerHTML
第一个style标签
document.styleSheets[0]
style标签里的第一个样式
document.styleSheets[0].rules[0]
防止点击空链接时,页面往往重置到页首端。
word
上一网页源
asp:
request.servervariables("HTTP_REFERER")
javascript:
document.referrer
释放内存
CollectGarbage();
禁止右键
document.oncontextmenu = function() { return false;}
禁止保存

禁止选取
地址栏图标

favicon.ico 名字最好不变16*16的16色,放虚拟目录根目录下
收藏栏图标

查看源码

关闭输入法

自动全选


title换行
obj.title = "123 sdfs "
获得时间所代表的微秒
var n1 = new Date("2004-10-10".replace(/-/g, "\/")).getTime()
窗口是否关闭
win.closed
checkbox扁平


获取选中内容
document.selection.createRange().duplicate().text
 
窗口最大化

<body onload="window.resizeTo(window.screen.width - 4,window.screen.height-50);window.moveTo(-4,-4)">

无关闭按钮IE
window.open("aa.htm", "meizz", "fullscreen=7");
统一编码/解码
alert(decodeURIComponent(encodeURIComponent("http://你好.com?as= hehe")))
encodeURIComponent对":"、"/"、";" 和 "?"也编码






参考资料

http://www.cnblogs.com/seagate_cn/archive/2005/04/21/142825.html

到PHP的安装目录下

[root@test1 ext]# cd /root/php/php5.2/ext

[root@test1 ext]# ./ext_skel --extname=cltest


修改 配置文件config.m4

[root@test1 ext]# vi cltest/config.m4  

删除 3 个 dnl

dnl PHP_ARG_WITH(my_module, for my_module support,

dnl Make sure that the comment is aligned:

dnl [ --with-my_module Include my_module support])


或者删除 下边这3个 dnl

dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,

dnl Make sure that the comment is aligned:

dnl [ --enable-my_module Enable my_module support])

修改完毕。


在my_module.c扩展模块的主程序文件中加PHP的函数定义

主程序中描述了php扩展模块的声明,模块中含有多少个函数,各个函数的作用,在phpinfo函数中显示什么内容,模块初始化做些什么,结束做些什么都会在这个文件里进行描述。我们在上面只是添加了一个函数say_hello,并且描述了say_hello函数的具体内容

[root@test1 ext]# Vi cltest.c


function_entry my_module_functions[] = {


PHP_FE(say_hello, NULL) /* ?添加这一行代码   注册函数say_hello() */

PHP_FE(confirm_my_module_compiled, NULL) /* For testing, remove later. */

{NULL, NULL, NULL} /* Must be the last line in my_module_functions[] */

};

在文件的最后添加下列代码 函数程序主体

PHP_FUNCTION(say_hello)
{
       RETURN_STRINGL("hello world",100,1);
}

修改完毕。



在my_module.h扩展模块的头文件中加PHP启动时要注册的函数

在对应的头文件中声明了say_hello这个函数,从而完成了我们预期的功能。

[root@test1 ext]# vi php_cltest.h

在文件中PHP_FUNCTION(confirm_my_module_compiled);一行前面添加下面的代码 函数定义

PHP_FUNCTION(say_hello);


保存文件退出,修改完毕。

pack 和 unpack 的使用
Posted in PHP on 2007/08/07 / 评论(2) »
      任何一款拥有socket操作能力的语言都有一个专门用于组包的函数,php也不例外!          

      用了很久php了却很少有机会用php进行一些二进制操作。 最近用php写一个socket客户端连接一个用C++语言开发的游戏服务端。 服务器端开发人员使用了二进制的形式来定义协议的格式。协议格式如下:

   包头(2bytes)+加密(1byte)+命令码(2bytes)+帧内容

1.包头的内容是记录帧内容的长度;
2. 加密:0表示不加密,1表示加密;
3. 命令码为服务端命令识别符号;

    一开始不了解php原来有pack可以来组装二进制包, 走了弯路,让服务端开发人员用C语言帮忙开发了的几个内存操作函数,按照协议规则返回二进制包,然后我将这几个方法编译成一组扩展函数供php使用。
   
    话归正题,本文是介绍如何使用pack和unpack这两个方法的。php官方手册举例太少,不能很容易理解,特别是那些格式化参数的使用。

转摘的参数中文说明:

pack/unpack 的摸板字符字符 含义
a 一个填充空的字节串
A 一个填充空格的字节串
b 一个位串,在每个字节里位的顺序都是升序
B 一个位串,在每个字节里位的顺序都是降序
c 一个有符号 char(8位整数)值
C 一个无符号 char(8位整数)值;关于 Unicode 参阅 U
d 本机格式的双精度浮点数
f 本机格式的单精度浮点数
h 一个十六进制串,低四位在前
H 一个十六进制串,高四位在前
i 一个有符号整数值,本机格式
I 一个无符号整数值,本机格式
l 一个有符号长整形,总是 32 位
L 一个无符号长整形,总是 32 位
n 一个 16位短整形,“网络”字节序(大头在前)
N 一个 32 位短整形,“网络”字节序(大头在前)
p 一个指向空结尾的字串的指针
P 一个指向定长字串的指针
q 一个有符号四倍(64位整数)值
Q 一个无符号四倍(64位整数)值
s 一个有符号短整数值,总是 16 位
S 一个无符号短整数值,总是 16 位,字节序跟机器芯片有关
u 一个无编码的字串
U 一个 Unicode 字符数字
v 一个“VAX”字节序(小头在前)的 16 位短整数
V 一个“VAX”字节序(小头在前)的 32 位短整数
w 一个 BER 压缩的整数
x 一个空字节(向前忽略一个字节)
X 备份一个字节
Z 一个空结束的(和空填充的)字节串
@ 用空字节填充绝对位置

分页: 9/26 第一页 上页 4 5 6 7 8 9 10 11 12 13 下页 最后页 [ 显示模式: 摘要 | 列表 ]