ThinkPHP实例化模型

来源:互联网 发布:惠普暗影精灵3怎么优化 编辑:程序博客网 时间:2024/06/05 01:02

一 代码

1、定义入口文件index.php
<?phpdefine('THINK_PATH', '../ThinkPHP');//定义ThinkPHP框架路径(相对于入口文件)define('APP_NAME', 'App');//定义项目名称define('APP_PATH', './App');//定义项目路径require(THINK_PATH."/ThinkPHP.php");//加载框架入口文件 App::run();//实例化一个网站应用实例?>
 
2、编辑配置文件
<?php return array('APP_DEBUG' => false, // 关闭调试模式'DB_TYPE'=> 'mysql',        // 数据库类型'DB_HOST'=> 'localhost', // 数据库服务器地址'DB_NAME'=>'db_database30', // 数据库名称'DB_USER'=>'root', // 数据库用户名'DB_PWD'=>'root', // 数据库密码'DB_PORT'=>'3306', // 数据库端口'DB_PREFIX'=>'think_', // 数据表前缀);?>
 
3、编辑控制文件
<?phpheader("Content-Type:text/html; charset=utf-8");//设置页面编码格式class IndexAction extends Action{public function index(){$db = M('User');// 实例化模型类,参数数据表名称,不包含前缀$select = $db->select(); // 查询数据$this->assign('select',$select); // 模板变量赋值$this->display(); // 指定模板页}public function type(){$dba = M('Type');// 实例化模型类,参数数据表名称,不包含前缀$select = $dba->select(); // 查询数据$this->assign('select',$select); // 模板变量赋值$this->display('type'); // 指定模板页}}?>
 
4、编辑模板文件
<!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=utf-8" /><title>用户信息输出</title><link href="__ROOT__/Public/Css/style.css" rel="stylesheet" type="text/css" /></head><body><table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF">  <tr>    <td colspan="3" bgcolor="#FFFFFF" class="title" align="center">用户信息</td>  </tr>  <tr class="title">    <td bgcolor="#FFFFFF" width="44">ID</td>    <td bgcolor="#FFFFFF" width="120">名称</td>    <td bgcolor="#FFFFFF" width="223">地址</td>  </tr>  <volist name='select' id='user' >  <tr class="content">    <td bgcolor="#FFFFFF">&nbsp;{$user.id}</td>    <td bgcolor="#FFFFFF">&nbsp;{$user.user}</td>    <td bgcolor="#FFFFFF">&nbsp;{$user.address}</td>  </tr>  </volist></table></body></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=utf-8" /><title>类别输出</title><link href="__ROOT__/Public/Css/style.css" rel="stylesheet" type="text/css" /></head><body><table width="405" border="1" cellpadding="1" cellspacing="1" bgcolor="#99CC33" bordercolor="#FFFFFF">  <tr>    <td colspan="3" bgcolor="#FFFFFF" class="title" align="center">类别输出</td>  </tr>  <tr class="title">    <td bgcolor="#FFFFFF" width="44">ID</td>    <td bgcolor="#FFFFFF" width="120">类别名称</td>    <td bgcolor="#FFFFFF" width="223">添加时间</td>  </tr>  <volist name='select' id='type' >  <tr class="content">    <td bgcolor="#FFFFFF">&nbsp;{$type.id}</td>    <td bgcolor="#FFFFFF">&nbsp;{$type.typename}</td>    <td bgcolor="#FFFFFF">&nbsp;{$type.dates}</td>  </tr>  </volist></table></body></html>
 
二 运行结果




 
  • 大小: 4.2 KB
  • 大小: 2.3 KB
  • 查看图片附件
原创粉丝点击