2016/05/05 smarty ① 登录 ②主页面 ③删除 ④让缩略信息显示完整 (补:增加 修改 )
共 八个页面
①login.php
<?php
include("init.inc.php"); $smarty->display("login.html");
?>
②login.html 显示模板 在templates模板文件夹中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h1>登录页面</h1>
<form action="chuli.php" method="post">
<div>用户名:<input type="text" name="uid"></div>
<div>密 码:<input type="text" name="pwd"></div>
<div><input type="submit" value="登录"></div>
</form>
</body>
</html>
③chuli.php
<?php
session_start();
$uid=$_POST["uid"];
$pwd=$_POST["pwd"];
include("DBDA.php");
$db=new DBDA(); $sql="select count(*) from users where username='{$uid}' and password='{$pwd}'"; $result=$db->StrQuery($sql,1,"mydb2");
if ($result==1) {
$_SESSION["uid"]=$uid;
header("location:main.php");
} else{
header("location:login.php");
} ?>
④main.php
<?php
session_start(); include("init.inc.php");
include("DBDA.php"); $db=new DBDA(); if (!empty($_SESSION["uid"])) {
;
$sqlf="select * from news";
$attr=$db->Query($sqlf,1,"mydb"); $smarty->assign("news",$attr); $smarty->assign("jsurl","./js/jquery-1.11.2.min.js");
$smarty->display("main.html");
} else{
header("location:login.php");
}
?>
⑤main.html 显示模板 在templates模板文件夹中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="<{$jsurl}>"></script>
<style type="text/css">
.fu
{
width:360px; background-color: yellow;
position: absolute;
} </style>
</head>
<body>
<h1>主页面</h1>
<table style="position:absolute" width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>编号</td>
<td>标题</td>
<td>内容</td>
<td>时间</td>
<td>类型</td>
<td>操作</td>
</tr>
<{foreach $news as $guo}> <tr> <td><{$guo[0]}></td>
<td class='dq' bs='<{$guo[1]}>'><{$guo[1]|truncate:20}></td>
<td><{$guo[2]|truncate:40}></td>
<td><{$guo[3]}></td>
<td><{$guo[4]}></td>
<td><a href="shanchu.php?code=<{$guo[0]}>">删除</a></td>
</tr>
<{/foreach}>
</table>
</body>
<script type="text/javascript">
$(document).ready(function(e){ $(".dq").mouseover(function(e){
var top=e.clientY; //获取鼠标位置
var name=$(this).attr("bs");
var div="<div class='fu' style='top:"+top+"px;left:100px'>"+name+"</div>";
$(".fu").remove();
$("body").append(div);
})
$(".dq").mouseleave(function(e){
$(".fu").remove();
})
}); </script>
</html>
⑥shanchu.php
<?php
$code=$_GET["code"];
include("DBDA.php");
$db=new DBDA(); $sqls="delete from news where ids='{$code}'"; $db->Query($sqls,0,"mydb"); header("location:main.php"); ?>
⑦init.inc.php (smarty.class.php 核心的配置文件/在libs文件夹下)需要应用smarty模板的php 页面引入
<?php
define("ROOT",str_replace("\\","/",dirname(__FILE__)).'/'); //常量ROOT中指定项目根目录
//echo str_replace("\\","/",dirname(__FILE__))."/";
require ROOT.'libs/Smarty.class.php'; //加载Smarty类文件
$smarty = new Smarty(); //实例化Smarty对象<br>
//$smarty -> auto_literal = false; //就可以让定界符号使用空格
$smarty->setTemplateDir(ROOT.'templates/'); //设置所有模板文件存放位置
//$smarty->addTemplateDir(ROOT.'templates2/'); //添加一个模板文件夹
$smarty->setCompileDir(ROOT.'templates_c/'); //设置编译过的模板存放的目录
$smarty->addPluginsDir(ROOT.'plugins/'); //设置为模板扩充插件存放目录
$smarty->setCacheDir(ROOT.'cache/'); //设置缓存文件存放目录
$smarty->setConfigDir(ROOT.'configs/'); //设置模板配置文件存放目录
$smarty->caching = false; //设置Smarty缓存开关功能
$smarty->cache_lifetime = 60*60*24; //设置缓存模板有效时间一天
$smarty->left_delimiter = '<{'; //设置模板语言中的左结束符
$smarty->right_delimiter = '}>'; //设置模板语言中的右结束符
?>
⑧DBDA.php (query方法查询输出数组,StrQuery方法查询输出字符串,显示时需要拆)
<?php class DBDA
{
public $host = "localhost"; //服务器地址
public $uid = "root"; //数据库的用户名
public $pwd = "123"; //数据库的密码 //执行SQL语句,返回相应结果的函数
//$sql是要执行的SQL语句
//$type是SQL语句的类型,0代表增删改,1代表查询
//$db代表要操作的数据库
public function Query($sql,$type,$db)
{
//造连接对象
$conn = new MySQLi($this->host,$this->uid,$this->pwd,$db); //判断连接是否成功
!mysqli_connect_error() or die("连接失败!"); //执行SQL语句
$result = $conn->query($sql); //判断SQL语句类型
if($type==1)
{
//如果是查询语句返回结果集的二维数组
return $result->fetch_all();
}
else
{
//如果是其他语句,返回true或false
return $result;
}
} //Ajax调用返回JSON
public function JsonQuery($sql,$type=1,$db="test2")
{
//定义数据源
$dsn = "mysql:dbname={$db};host={$this->host}";
//造pdo对象
$pdo = new PDO($dsn,"{$this->uid}","{$this->pwd}"); //准备执行SQL语句
$st = $pdo->prepare($sql); //执行预处理语句
if($st->execute())
{
if($type==1)
{
$attr = $st->fetchAll(PDO::FETCH_ASSOC);
return json_encode($attr);
}
else
{
if($st)
{
return "OK";
}
else
{
return "NO";
}
} }
else
{
echo "执行失败!";
} }
//Ajax调用返回字符串
public function StrQuery($sql,$type,$db)
{
//造连接对象
$conn = new MySQLi($this->host,$this->uid,$this->pwd,$db); //判断连接是否成功
!mysqli_connect_error() or die("连接失败!"); //执行SQL语句
$result = $conn->query($sql); //判断SQL语句类型
if($type==1)
{
$attr = $result->fetch_all();
$str = "";
//如果是查询语句返回字符串
for($i=0;$i<count($attr);$i++)
{
for($j=0;$j<count($attr[$i]);$j++)
{
$str = $str.$attr[$i][$j];
$str = $str."^";
}
$str = substr($str,0,strlen($str)-1);
$str = $str."|";
}
$str = substr($str,0,strlen($str)-1); return $str;
}
else
{
//如果是其他语句,返回true或false
if($result)
{
return "OK";
}
else
{
return "NO";
}
}
} }
显示效果:
登录:

经过 login.html chuli.php 到达 main.php 通过main.html 模板显示出来

位于编号为1 的新闻 被 点击右侧 删除掉
main.html页面 传值
<td><a href="shanchu.php?code=<{$guo[0]}>">删除</a></td>
通过 shanchu.php 页面链接数据库 做删除动作
让缩略信息显示完整 通过js完成
<script type="text/javascript">
$(document).ready(function(e){ $(".dq").mouseover(function(e){
var top=e.clientY; //获取鼠标位置
var name=$(this).attr("bs");
var div="<div class='fu' style='top:"+top+"px;left:100px'>"+name+"</div>";
$(".fu").remove();
$("body").append(div);
})
$(".dq").mouseleave(function(e){
$(".fu").remove();
})
}); </script>

2016/05/05 smarty ① 登录 ②主页面 ③删除 ④让缩略信息显示完整 (补:增加 修改 )的更多相关文章
- http://tedhacker.top/2016/08/05/Spring%E7%BA%BF%E7%A8%8B%E6%B1%A0%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95/
http://tedhacker.top/2016/08/05/Spring%E7%BA%BF%E7%A8%8B%E6%B1%A0%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%9 ...
- Murano Weekly Meeting 2016.07.05
Meeting time: 2016.July.05 1:00~2:00 Chairperson: Kirill Zaitsev, from Mirantis Meeting summary: 1. ...
- Oracle 增加修改删除字段
Oracle 增加修改删除字段 添加字段的语法:alter table tablename add (column datatype [default value][null/not null],…. ...
- 2016/05/05 smarty ①分页 ② 查询后分页 ③缓存
samrty 分页 查询后分页 0505fch.php <?php include("init.inc.php"); include("DBDA.php&qu ...
- “耐撕”团队 2016.04.05 站立会议
1. 时间: 20:10--20:25 共计15分钟. 2. 成员: Z 郑蕊 * 组长 (博客:http://www.cnblogs.com/zhengrui0452/), P 濮成林(博客:ht ...
- extern “C”原理,用法以及使用场景-2016.01.05
1 问题提出 在编程过程中,经常发现如下用法: #ifndef _FILE_NAME_H_ #define _FILE_NAME_H_ #ifdef __cplusplus extern " ...
- iOS常识名词解释 2016/04/05
Bundle : http://www.cnblogs.com/BigPolarBear/archive/2012/03/28/2421802.html http://blog.sina.com.cn ...
- 2016/07/05 zend optimizer
Zend Optimizer是由PHP核心引擎“Zend” http://www.zend.com 创建者Zend技术公司所开的免费PHP优化软件.据Zend公司透露使用这个软件某些情况下至少可以提高 ...
- 2016.01.05 DOM笔记(一) 查找元素
DOM节点的种类 元素和标签是一个意思,例如<body>标签或者称为<body>元素 节点DOM的节点分为三类 元素节点,文本节点,属性节点 例如 <div id=‘b ...
随机推荐
- c++_加法变乘法
加法变乘法 我们都知道:1+2+3+ ... + 49 = 1225现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015 比如:1+2+3+...+10*11+12+...+27*28+29+ ...
- Android开发——获取应用数据/缓存大小并清理缓存
1. 获取应用数据/缓存大小 其中pm为实例化的PackageManager,因为需要遍历所有的已安装的应用.因此需要开启子线程进行处理. 还有需要注意的是,在Android4.2之前getPacka ...
- 了解DOM
DOM是为了方便处理层次型文档(如XML.HTML)的一种技术.DOM还提供了一套API,使开发人员可以用面向对象的方式来处理这些文档.对于XML文档来说,有专门的处理XML文档是XML DOM,一 ...
- selenium之文件上传
文件上传是所有UI自动化测试都要面对的一个头疼问题,今天博主在这里给大家分享下自己处理文件上传的经验,希望能够帮助到广大被文件上传坑住的seleniumer. 首先,我们要区分出上传按钮的种类,大体上 ...
- flume介绍及应用
版权声明:本文为yunshuxueyuan原创文章.如需转载请标明出处: http://www.cnblogs.com/sxt-zkys/QQ技术交流群:299142667 flume的概念 1. ...
- 【Ajax 2】封装Ajax的核心对象:XMLHttpRequest对象
导读:AJAX利用一个构建到所有现代浏览器内部的对象-XMLHttpRequest-来实现发送和接收HTTP请求与响应信息.那么,XMLHttpRequest对象是怎么创建和封装的呢? 一.简介 1. ...
- 道路游戏(洛谷 P1070)
题目描述 小新正在玩一个简单的电脑游戏. 游戏中有一条环形马路,马路上有 n 个机器人工厂,两个相邻机器人工厂之间由一小段马路连接.小新以某个机器人工厂为起点,按顺时针顺序依次将这 n 个机器人工厂编 ...
- 转载:用vector保存对象时保存指针的优点, 以及reserve的使用
#include <vector> #include <stdio.h> class A { public: A() { printf("A()/n"); ...
- msp430项目编程16
msp430中项目---电子秒表 1.定时器工作原理 2.电路原理说明 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习
- ThinkPHP __construct和_initialize的使用
ThinkPHP框架中的__construct和_initialize的使用 父类(PlatformController.class.php): class PlatformController ex ...