找回密码
 立即注册
搜索
查看: 1625|回复: 0

⑤ThinkPHP系统函数

[复制链接]

581

主题

110

回帖

4066

积分

管理员

积分
4066

众神之神

发表于 2015-3-1 16:38:07 | 显示全部楼层 |阅读模式
占位
I方法:
U方法:
M方法:
C方法:

C方法,读取配置器
如在config.php里
  1. <?php
  2. return array(
  3.         //'配置项'=>'配置值'
  4.         'USERNAME' => 'zhaichu',
  5. );
  6. ?>
复制代码
可以在IndexAction.class.php控制器里
  1. <meta charset="utf-8">
  2. <?php
  3. class IndexAction extends Action {
  4.     public function index(){
  5.          echo C('username');
  6.     }
  7. }
复制代码
则会输出:zhaichu
数据库链接配置,config.php
  1. <?php
  2. return array(
  3.         //数据库链接
  4.         'DB_HOST' => '127.0.0.1',
  5.         'DB_USER' => 'root',
  6.         'DB_PWD' => '',
  7.         'DB_NAME' => 'think',
  8.         'DB_PREFIX' => 'hd_'
  9. );
  10. ?>
复制代码
M方法:实例化数据库模型
可以在IndexAction.class.php控制器里
  1. <meta charset="utf-8">
  2. <?php
  3. class IndexAction extends Action {
  4.     public function index(){
  5.           $db = M('wish')->select();
  6.           dump($db);
  7.     }
  8. }
复制代码
可以打印出查找wish表里的所有字段的数据

扩展函数库的定义:
在Commom/下创建common.php
自定义P,打印函数
  1. <?php
  2. function p ($array) {
  3.         dump($array, 1, '<pre>', 0);
  4. }
  5. ?>
复制代码
可以在IndexAction.class.php控制器里:
<meta charset="utf-8">
<?php
class IndexAction extends Action {
    public function index(){
          $db = M('wish')->select();
          p($db);
    }
}
展示的形式就是为print_r的形式来打印


在config.php
  1. <?php
  2. return array(

  3.         //改变函数库的名字
  4.         'LOAD_EXT_FILE' => 'function',
  5. );
  6. ?>
复制代码
那在common文件夹下就可以新建function.php,就可以在里面定义函数了。临时性的加载(只能在当前方法内有用)
  1. load('@.function');
复制代码

输出模板:在控制器中如IndexAction.clas.php中的index要显示一个模板
  1. <meta charset="utf-8">
  2. <?php
  3. class IndexAction extends Action {
  4.     public function index(){
  5.             $this->display();
  6.     }
  7. }
复制代码
修改样式的映射地址,在Conf/config.php中
  1. '__PUBLIC__' => __ROOT__ . '/' . APP_NAME . '/Tpl/Public',
复制代码

Url的U方法,在IndexAction.class.php中
  1. echo U('Index/index');
复制代码
默认url的展现形式为为/index.php/Index/index.html也可以添加参数
  1. echo U('Index/index',array('aid' => 12, 'uid' => 11));
复制代码
url展现形式为:/Index/index/aid/12/uid/11

可以在conf/config.php中
  1. //URL的展现形式
  2.     'URL_MODEL' => 1,
复制代码
当参数为0时,展现形式为:/index.php?m=Index&a=index
当参数为1时,展现形式为:/index.php/Index/index.html

当参数为2时,展现形式为:/Index/index.html
当参数为3时,展现形式为:/index.php?s=/Index/index.html

伪静态后缀的设置,在Conf/config.php中
  1. //URL后缀伪静态形式
  2.     'URL_HTML_SUFFIX' => '',
复制代码
当为‘’的时候,没有后缀,如:/Index/index
当为‘htm’的时候,没有后缀,如:/Index/index.htm
当为‘php’的时候,没有后缀,如:/Index/index.php
在模版里面用函数的格式{:函数名(‘参数’)}
如:
  1. <form action="{:U('handle')}" name='wish' method="post">
复制代码
Think里面提取post或者get提交的数据I('form里面的name值');
如:
在模版里
  1. <input type="text" name='username' id='username'/>
复制代码
在控制器里面提取
  1.   echo I('content');
复制代码
判断是否post提交,错误的话抛出错误,错误的话就跳转
  1. Public function handle () {
  2.        if (!IS_POST) _404('页面不存在哦哦哦', U('index'));
  3.        echo 11111;
  4.     }
复制代码

向数据库表中插入的实例:
  1. <?php
  2. // 本类由系统自动生成,仅供测试用途
  3. class IndexAction extends Action {
  4.     public function index(){
  5.         $this->display();
  6.     }
  7.     Public function handle () {
  8.        if (!IS_POST) _404('页面不存在哦哦哦', U('index'));

  9.         $data = array(
  10.             'username' => I('username'),
  11.             'content' => I('content'),
  12.             'time' => time(),
  13.         );
  14.         /*插入之前必须先创建数据对象M('wish')->data('数组'),最后返回最后插入的id值*/
  15.         if (M('wish')->data($data)->add()) {
  16.             /*操作成功之后的方法 $this->success('提示信息', '成功之后的跳转地址')*/
  17.             $this->success('发布成功', 'index')
  18.         } else {
  19.             $this->error('发布失败,请重试....');
  20.         }

  21.     }
  22. }
复制代码

删除方法,必须要传递一个where条件
  1. /*删除方法*/
  2.         $result = M('wish')->where('id > 0')->delete();
  3.         var_dump($result);
复制代码

数据库查询方法:
  1. $wish = M('wish')->select();
  2.         var_dump($wish);
复制代码

TP将变量分配到模版上的方法在控制器里:$this->assign('名字', 值);
  1. $this->assign('a', d111);
复制代码
  1. /*也可以这样,通过对象来分配*/
  2.         $this->a = d111;
复制代码


在模版里输出:
  1. <span class='username'>{$a}</span>
复制代码

TP里面模版的循环方式
  1. <foreach name='wish' item='v'>
  2.             循环体
  3.         </foreach>
复制代码

在模版里面时间输出的格式
  1. <span class='time'>{$v.time|date='y-m-d H:i', ###}</span>
复制代码
在config.php中开启默认点语法解析
  1. //默认点语法
  2.     'TMPL_VAR_IDENTIFT' => 'array',
复制代码
这样在模版中使用数组就可以:{$v.content} 以这样的形式了
TP里面的随机数:
  1. <dl class='paper a{:mt_rand(1,5)}'>
复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|十三博客 ( 鲁ICP备2023000528号 )

GMT+8, 2026-6-1 18:26 , Processed in 0.055510 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表