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 ...
随机推荐
- 每日命令:(14)tune2fs
tune2fs简介 tune2fs是调整和查看ext2/ext3文件系统的文件系统参数,Windows下面如果出现意外断电死机情况,下次开机一般都会出现系统自检.Linux系统下面也有文件系统自检,而 ...
- Python旅途——数据类型
1.概要 今天跟大家分享的是数据类型,数据类型在Python中的重要程度就好比我们汉语中的拼音一样重要. 2.数据类型 1.整形 整形就像是数学中的1.2.3一样,用int来表示. a = 1 # ...
- LeetCode(34)Search for a Range
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
- 牛客网暑期ACM多校训练营(第四场) J 贪心
链接: https://www.nowcoder.com/acm/contest/143/J #include<bits/stdc++.h> using namespace std; lo ...
- Android记录2013年10月20日
1. ailed to fectch URl https://dl-ssl.google.com/android/repository/addons_list.xml, reason: Connect ...
- pycharm运行没问题,但是在命令行执行就报错
因为python并不知道你那个叫demo的package在哪里.你需要手动把project的完整路径添加到PYTHONPATH这个环境变量中.pycharm执行项目中的文件时会自动帮你做这件事,所以你 ...
- mysql查最大字符串
select MAX(comp_code+0) from t_base_company 字符串 +0 把字符串转成数字
- [Go]通道(channel)的基本操作
通道类型是Go语言自带的.唯一一个可以满足并发安全性的类型,在声明并初始化一个通道时,需要用到内建函数make,传给make函数的第一个参数应该代表通道的具体类型的类型字面量. 如类型字面量 chan ...
- 【LibreOJ10121】与众不同(RMQ,二分)
题意: 思路: C++ #include<map> #include<set> #include<cmath> #include<cstdio> #in ...
- Educational Codeforces Round 50 (Rated for Div. 2) E. Covered Points
注释上都有解析了,就不写了吧,去重的问题就用set解决,并且呢第i个线段最多和其他线段产生i-1个交点,n^2logn. #include <cmath> #include <cst ...