ACTION=cas logoutT什么意思?

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
I have an application that integrates with Facebook using Oauth 2.
I can authorize with FB and query their REST and Graph APIs perfectly well, but when I authorize an active browser session is created with FB. I can then log-out of my application just fine, but the session with FB persists, so if anyone else uses the browser they will see the previous users FB account (unless the previous user manually logs out of FB also).
The steps I take to authorize are:
Call [LINK: /oauth/authorize?client_id...]
This step opens a Facebook login/connect window if the user's browser doesn't already have an active FB session. Once they log-in to facebook they redirect to my site with a code I can exchange for an oauth token.
Call [LINK: /oauth/access_token?client_id..] with the code from (1)
Now I have an Oauth Token, and the user's browser is logged into my site, and into FB.
I call a bunch of APIs to do stuff: i.e. [LINK: /me?access_token=..]
Lets say my user wants to log out of my site. The FB terms and conditions demand that I perform Single Sign Off, so when the user logs out of my site, they also are logged out of Facebook. There are arguments that this is a bit daft, but I'm happy to comply if there is any way of actually achieving that.
I have seen suggestions that:
A. I use the Javascript API to logout: FB.Connect.logout(). Well I tried using that, but it didn't work, and I'm not sure exactly how it could, as I don't use the Javascript API in any way on my site. The session isn't maintained or created by the Javascript API so I'm not sure how it's supposed to expire it either.
B. Use [LINK: /logout.php]. This was suggested by an admin in the Facebook forums some time ago. The example given related to the old way of getting FB sessions (non-oauth) so I don't think I can apply it in my case.
C. Use the old REST api expireSession or revokeAuthorization. I tried both of these and while they do expire the Oauth token they don't invalidate the session that the browser is currently using so it has no effect, the user is not logged out of Facebook.
I'm really at a bit of a loose end, the Facebook documentation is patchy, ambiguous and pretty poor. The support on the forums is non-existant, at the moment I can't even log in to the facebook forum, and aside from that, their own FB Connect integration doesn't even work on the forum itself. Doesn't inspire much confidence.
Ta for any help you can offer.
ps. Had to change HTTPS to LINK, not enough karma to post links which is probably fair enough.
1,40211118
I am having the same problem. I also login using oauth (I am using RubyOnRails), but for logout, I do it with JavaScript using a link like this:
&a href="/logout" onclick="FB.logout();"&Logout&/a&
This first calls the onclick function and performs a logout on facebook, and then the normal /logout function of my site is called.
Though I would prefer a serverside solution as well, but at least it does what I want, it logs me out on both sites.
I am also quite new to the Facebook integration stuff and played around the first time with it, but my general feeling is that the documentation is pretty spread all over the place with lots of outdated stuff.
6,02421232
This solution no longer works with FaceBook's current API (seems it was unintended to begin with)
/logout.php?confirm=1&next=;
Try to give this link on you signout link or button where ""
is where u want to redirect back after signout may be ur home page.
It works..
This works as of now - and is documented on facebook's site @ .
Not sure how recently it was added to the documentation, pretty sure it wasn't there when I checked Feb-2012
You can programmatically log the user our of Facebook by redirecting
the user to
4,95322442
You can do this with the access_token:
$access_array = split("\|", $access_token);
$session_key = $access_array[1];
You can use that $session key in the PHP SDK to generate a functional logout URL.
$logoutUrl = $facebook-&getLogoutUrl(array('next' =& $logoutUrl, 'session_key' =& $session_key));
This ends the browser's facebook session.
I can programmatically log user out Facebook by redirecting user to
The URL supplied in the next parameter must be a URL with the same base domain as your application as defined in your app's settings.
More details:
16.8k99199
For Python developers that want to log user out straight from the backend
At the moment I'm writing this, the trick with
no longer works (at least for me) and user is redirected to the mobile FB login page which obviously is not good for UX.
Fortunately, FB PHP SDK has a
(in case the link doesn't lead to getLogoutUrl() function, just search look for it on that page). This is also mentioned in at least one other on StackOverflow: .
BTW I've just noticed that
in this question, but I'm adding my answer as a summary for Python developers.
9,95123054
the mobile solution suggested by Sumit works perfectly for AS3 Air:
html.location = "/logout.php?confirm=1&next="
A note for Christoph's answer:
The logout function requires a callback function to be specified and will fail without
it, at least on Firefox. Chrome works without the callback.
FB.logout(function(response) {});
@Christoph: just adding someting . i dont think so this is a correct way.to logout at both places at the same time.(&a href="/logout" onclick="FB.logout();"&Logout&/a&).
Just add id to the anchor tag . &a id='fbLogOut' href="/logout" onclick="FB.logout();"&Logout&/a&
$(document).ready(function(){
$('#fbLogOut').click(function(e){
e.preventDefault();
FB.logout(function(response) {
// user is now logged out
var url = $(this).attr('href');
window.location=
Update: This solution works and just a call to 'FB.logout()' doesn't work because browser wants a user interaction to actually call this function, so that it knows - it is a user not a script.
&a href="#" onclick="FB.logout();"&Logout&/a&
With PHP I'm doing:
&a href="?action=logout"&logout.&/a&
if(isset($_GET['action']) && $_GET['action'] === 'logout'){
$facebook-&destroySession();
header(WHERE YOU WANT TO REDIRECT TO);
Works and is nice and easy am just trying to find a logout button graphic now!
it's simple just type : $facebook->setSession(null); for logout
protected by ?
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10
on this site.
Would you like to answer one of these
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabledPHP仿博客园 个人博客(2) 数据库增添改删,PHP教程,PHP案例,PHP实例
本站中文域名:、 
        
     
 |  |  |  |  
     |     |     |   
您的位置: &&
&& PHP仿博客园 个人博客(2) 数据库增添改删
PHP仿博客园 个人博客(2) 数据库增添改删
  本文标签:PHP,博客园
废话不,上一篇有个核心概念就是 give action do something ! 这篇我就用代码来解释这个概念是啥意思,先看我的 post.class.php . 这个文件是我们的数据层处理类&。
简单介绍一下这个model 类,它继承了一个数据库基类来做crud 等常用操作, 每次初始化时就会初始化一个数据库对象 $db. 我们就用这个对象来操作我们的数据&。对于数据操作有2个重要方法 storePostFormValues( ) , storeDiaryFormValues( ),它们2个方法是数据流的开始&。还有2个方法很有意思,addChildNumber( ), reduceChildNumber( ),& 它们负责在插入或删除文档时的 一个暗箱操作&。因为我的文档可以用多个分类,所以在操作文档的时候,要考虑到一个问题,就是 category 表中有个字段 记录了该分类下的 文档数量&。所以要动态地改变这些数目的值&。下面配合 post.php 控制器,我们就可以开始我们数据的流程了(我的控制器还不是一个类,所以无法生成API文档&。因为这还不是真正地MVC架构&。)所以在MVC之前,这个也能更利于的理解MVC到底是神马东东,以及你自己如何去应用,写出自己的MVC&。
以下的情形都是假设:
$action = "天上掉下个女朋友给我吧!"; 让我们传入这个控制器看会发生神马事情&。
复制代码 代码如下:require_once( "config/config.php" );session_start( );$action = isset( $_GET[action] ) ? $_GET[action] : "";$username = isset( $_SESSION[username] ) ? $_SESSION[username] : "";
&if( !$username )&{&&& header("Location: index.php?action=login");&&&&}
这里我们有个重要流程控制语句 switch , 这个单词是 开关的意思; 所以当上面那个 $action = "天上掉下个女朋友给我吧!"; 传入 switch 时,只有2种可能,一种是开,一种是关&。这里有点双关地意思,有些同学可能看出来了&。嘿嘿!
言归正传:看看我们的 switch 是如何开关这些 $action , 很明显 天上不会掉个女朋友给我,因为控制器里没有这个开关,所以只能还是说说代码的事&。
复制代码 代码如下:switch( $action )&{&&& case "newPost" :&&&&&&& newPost( );&&&&&& case "delete" :&&&&&&& delete( ) ;&&&&&& case "updatePost":&&&&&&& updatePost( );&&&&&& case "IsDraft":&&&&&&& listDraft( );&&&&&& case "logout" :&&&&&&& logout( );&&&&&&&& case "isPost":&&&&&&& listPost( );&&&&&& case "diffentCategoryPost":&&&&&&& diffentCategoryPost( );&&&&&& case "unCategory":&&&&&&& unCategory( );&&&&&& default :&&&&&&& listPost( );&&&&}
每个switch都应该定义默认的 开关,这样当没有女朋友的时候,可以确保我们还有基友&。如何传入 action 呢?来看这样一个url,也就是我们的后台框架的导航, post.php?action=isPost& 这个是一个标准的action, 我们每个url 其实都是由这些action组成的,也可以加入其他的一些参数到我们的url 中, 这样我们可以在控制器定义的方法中 GET (得到这些变量的值),然后我们可以多些控制&。好了,当这个url 到达我们的控制器后,我们接收判断,然后打开一个 isPost 的开关,这样我们就可以调用后面的方法了,想想 开关灯,开关电脑,开关就是我们经常做的事&。这里我们只是换了一个地方&。ok &。 来看看这个开关的下面的方法&。
复制代码 代码如下:function listPost( ){&&& $results = array( );&&& $results[pageTitle] = "Post List" ;&&& $results[path] = "&a href=?action=isPost &随笔&/a&";&&& // set the message &&& if ( isset( $_GET[error] ) ) &&& {&&&&&&& if ( $_GET[error] == "InsertedFailed" ) $results[errorMessage] = "文档添加失败";&&&&&&& if ( $_GET[error] == "postDeleteFailed" ) $results[errorMessage] = "文档删除失败";&&& }&&& if ( isset( $_GET[status] ) ) &&& {&&&&&&& if ( $_GET[status] == "changesSaved" ) $results[statusMessage] = "文档保存了!";&&&&&&& if ( $_GET[status] == "Deleted" ) $results[statusMessage] = "文档删除了!";&&&&&&& if ( $_GET[status] == "Inserted" ) $results[statusMessage] = "你添加了新的文档!";&&&&&&& if ( $_GET[status] == "SaveToDraft" ) $results[statusMessage] = "文档保存到了草稿箱!";&&& }&&& // 文档的分类浏览 &&& $db = MySQL::getInstance( );&&& $pagination = new P&&& $cat = new C&&& $results[categories] =& $cat-&getCategoryList("post");&&& $pagination-&countSQL = "select * from post where type = post " ;&&& $db-&Query( $pagination-&countSQL );&&& $pagination-&totalRecords = $db-&RowCount( );&&& $records = $db-&HasRecords( $pagination-&rebuiltSQL( ) );&&& if( $records )&&& {&&&&&&& $results[posts] = $db-&QueryArray( $pagination-&rebuiltSQL( ) );&&&&&&& require_once(TEMPLATE_PATH . "/post/post_list.php");&&& } &&& else &&& {&&&&&&& require_once(TEMPLATE_PATH . "/post/post_list.php");&&& }&}
我们定义了一个数组,$results = array( ); 这个数组的作用明显,它将保存我们从 model 中获取的任何数据,也可以保存从url上 GET 的特殊参数&。然后将在我们下面require_once(*****) 包含的模版中显示出来, 路径定义在了 path 变量中&。
同时我们会接收2个提示参数,
error , 表示操作出现错误,任何人都在所难免,包括电脑,谁都会犯错,关键是去承认,电脑做的很好,他们勇于承认错误&。
表示状态,就是成功的操作&。
$pagination = new P 这个类是我们的分页类,我们传入一个 总的数量给它,然后它自己会算出总页数,每跳转一个页面,相当于刷新了一次,所以大家的做法就是,在构造器里 GET(获取)url上的page 的值,让我们知道是当前那一页了&。同时我们重新生成了查询的语句,后面加上一条限制的语句,类似 limit $start(起始的id), $offset(长度); 原理就是从这个id起,往后给我10 条记录;我的设定就是 10 条,你也可以更灵活&。$cat = new C这个类后面会详细说,也是非常重要的分类model&。这里我们就是简单获取 这个类型下的所有分类,显示在侧边栏,我已经完成了&。有图有真相!
这样 我们的 $results 数组中就储存了我们页面所需的所有数据&。 好的,来看看我们的模版,是怎么输出的&。
复制代码 代码如下:&&!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&&&html&&&&& &head&&&&&&&&& &title&&&&&&&&&&&&& 博客后台管理&/title&&&&&&&&&&&&& &meta http-equiv="Content-Type" content="text/ charset=utf-8"/&&&&&&&&&&&&& &link rel="stylesheet" type="text/css" href="assert/css/blog.css" /&&&&&&&&&&& &&&& &/head&&&& &&&& &body id="Posts"&&&&&&&&&&&&& &table id="BodyTable" border="0" cellpadding="0" cellspacing="0" width="100%"&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&& &td id="Header" colspan="2"&&div id="SiteNav"&&/div&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&& &div id="BlogTitle"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& Arists Blog&&&&&&&&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&&&&&&&&&& &div id="Site Title"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &b&&blockquote&Hinging there, everything will be fine.&/blockquote&&/b&&&&&&&&&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&& &td&&&&&&&&&&&&&&&&&&&&&&&&& &div id="LeftNavHeader"&操作&/div&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &td class="NavHeaderRow"&&&&&&&&&&&&&&&&&&&&&&&&& &ul id="TopNav"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="post.php?action=IsPost"&随笔&/a&&/li&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="article.php?action=IsArticle"&文章&/a&&/li&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="diary.php?action=IsDiary"&日记&/a&&/li&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="comment.php?action=IsComment"&评论&/a&&/li&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="photo.php?action=IsPhoto"&相片&/a&&/li&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&& &/ul&&&&&&&&&&&&&&&&&&&&&&&&& &div id="SubNav"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 当前位置:&&?php if( isset( $results[path] )) echo $results[path]; ?&&&&&&&&&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&& &td class="NavLeftCell"&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&& &div class="left_nav"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &ul id="LinksActions"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="article.php?action=newArticle"&& 添加新文章&/a&&/li&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &/ul&&&&&&&&&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&&&&&&&&&& &div id="CategoriesHeader" class="LeftNavHeader"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 分类&&&&&&&&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&&&&&&&&&& &div class="left_nav"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &ul id="LinksCategories"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="category.php?action=ListCat&type=article"&[编辑分类]&/a&&/li&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="article.php?action=IsArticle"&[所有分类]&/a&&/li&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="article.php?action=unCategory"&[未分类]&/a&&/li&&&?php&if( isset( $results[categories] )& && ! empty( $results[categories] ) ){&&&& foreach( $results[categories] as $category ){&&&& echo &&&EOB&&&&&&&&&&&&&&&&&&&& &li&&a href="article.php?action=diffentCategoryArticle&catID={$category[category_id]}"&{$category[name]}({$category[count_child_number]})&/a&&/li&&EOB;&&&& }&}&?&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &/ul&&&&&&&&&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &td id="Body"&&&&&&&&&&&&&&&&&&&&&&&&& &div id="Main"&&&&&&&&&&&&&&&&& &div id="Editor_Messages"&&&&&&&&&&&&&&&&& &!-- 显示提示信息 --&&&&&&&&&&&&&&&&& &?php &&&&&&&&&&&&&&&&&&&& if( isset( $results[statusMessage] )){echo& $results[statusMessage];}&&&&&&&&&&&&&&&&&&&& if( isset( $results[errorMessage] )){echo& $results[errorMessage];}&&&&&&&&&&&&&&&& ?&&&&&&&&&&&&&&&&& &/div&&&div id="Editor_Results"&&&&& &div id="Editor_Results_Header" class="CollapsibleHeader"&&&&&&&&& &span id="Editor_Results_headerTitle"&文章(主要用于转载,发布原创博文要通过“随笔”)&/span&&&&& &/div&&&&& &div id="Editor_Results_Contents"&&&&& &&?php& &&&& if( isset( $results[posts] )){&&&& echo &&&EOB&&&&&&&&&&&& &table id="Listing" class="Listing" cellspacing="0" cellpadding="0" border="0" style=width:98%;&&&&&&&&&&&&&&&&& &tr style="text-align: center"&&&&&&&&&&&&&&&&&&&&& &th valign="bottom"&&&&&&&&&&&&&&&&&&&&&&&&& 标题&&&&&&&&&&&&&&&&&&&& &/th&&&&&&&&&&&&&&&&&&&&& &th width="40" style="text-align: center"&&&&&&&&&&&&&&&&&&&&&&&&& 发布&br /&&&&&&&&&&&&&&&&&&&&&&&&& 状态&&&&&&&&&&&&&&&&&&&& &/th&&&&&&&&&&&&&&&&&&&&& &th valign="bottom" width="50" style="text-align: center"&&&&&&&&&&&&&&&&&&&&&&&&& 评论&&&&&&&&&&&&&&&&&&&& &/th&&&&&&&&&&&&&&&&&&&&& &th width="50" style="text-align: center"&&&&&&&&&&&&&&&&&&&&&&&&& 页面&br /&&&&&&&&&&&&&&&&&&&&&&&&& 浏览&&&&&&&&&&&&&&&&&&&& &/th&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&& &th valign="bottom" width="40" style="text-align: center"&&&&&&&&&&&&&&&&&&&&&&&&& 操作&&&&&&&&&&&&&&&&&&&& &/th&&&&&&&&&&&&&&&&&&&&& &th valign="bottom" width="40" style="text-align: center"&&&&&&&&&&&&&&&&&&&&&&&&& 操作&&&&&&&&&&&&&&&&&&&& &/th&&&&&&&&&&&&&&&&& &/tr&&EOB;&&&&&&&& foreach( $results[posts] as $post ){&&&&&&&&&&&& $time = date("Y-m-d H:i:s", $post[create_time]);&&&&&&&&&&&& if( $post[status] == "1" ){&&&&&&&&&&&&&&&& $post[status]& = "发布";&&&&&&&&&&&& }&&& else {&&&&&&&&&&&&&&&& $post[status]& = "&b&未发布&/b&";&&&&&&&&&&&& }&&&&&&&&&&&& echo &&&EOB&&&&&&&&&&&& &tr id="entry_{$post[post_id]}" class="Alt"&&&&&&&&&&&&&&&&& &td style="text-align: left"&{$post[title]} ({$time})&/td&&&&&&&&&&&&&&&&& &td style="text-align: center"&{$post[status]}&/td&&&&&&&&&&&&&&&&& &td style="text-align: center"&{$post[view_count]}&/td&&&&&&&&&&&&&&&&& &td style="text-align: center"&{$post[comment_count]}&/td&&&&&&&&&&&&&&&&& &td style="text-align: center"&&a href="article.php?action=editArticle&postID={$post[post_id]}"&编辑&/a&&/td&&&&&&&&&&&&&&&&& &td style="text-align: center"&&a href="JavaScript:if(confirm(从数据库中删除这篇文档?)==true){window.location=article.php?action=delete&postID={$post[post_id]};}"&删除&/a&&/td&&&& &&&&&&&&&&&& &/tr&&EOB;&&&&&&&& }&&&&&&&&&&&& echo "&/table&";&&&&&&&&&&&&&&& &&&&&&&&&&&& if( isset( $pagination) ){$pagination-&createLinks( ) ;}&&&& } else {&&&&&&&& echo "当前无内容!";&&&& }&?&&& &&&& &/div&&&/div& &&span id="currentPostId" style="display:"&&/span&&&&&&&&&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&& &/table&&&&&&&&&&&&& &div id="blog_top_nav_block"&&&&&&&&&&&&&&&&& &div id="site_nav"&&&&&&&&&&&&&&&&& &/div& &&&&&&&&&&&&&&&& &div id="login_area"&&&&&&&&&& &&&&&&&&&&&&&&&&& &span id="span_userinfo"&&b&&?php& echo $_SESSION[username]; ?& &/b&& &a href="?action=logout"&logout&/a&&/span&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&& &div class="clear"&&/div& &&&&&&&&&&&& &/div&&&&&&&&&&&&& &table id="Footer" border="0" cellpadding="0" cellspacing="0" width="100%"&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&& &td colspan="2"&&&&&&&&&&&&&&&&&&&&&&&&& &div&&&&&&&&&&&&&&&&&&&&&&&&&&&&& & &?php echo date("Y", time( ) ); ?& Arist&&&&&&&&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&& &/table&&&&& &/body&&&/html&
以上只是显示数据,人人都会啊&。
我们怎么操作这些数据呢?
操作,就像是一种控制能力&。 学生时代踢足球,我对球场有一种很强的控制能力,大学足球比赛拿了1次冠军,1次亚军,1次季军,大四没去,中学更是无数荣誉&。
我的位置是中卫,在足球场上,这个位置,你得有统观全局的能力,也得有很强的个人能力,还有指挥能力;扯的远了,现在天天坐在电脑前,这些东西也早就没了,
就剩下些经验之谈&。不过其中滋味,你也须也体验过&。
我这个博客有个缺点,每次你对数据库进行一次读写操作,你得刷新啊!我知道这对服务器的负载很大,但是我觉得如果一个新技术你没有完全吃透,盲目运用,只会适得其反&。
所以暂时我还是牺牲服务器的响应时间,内存消耗,来获得一种相对的稳定!
所以我对全局还不是很了解,还有很多未知地领域没有涉入,如深入ajax,深入php,c &。&。&。 不多说了&。
好了,看看怎么对数据进行CRUD 吧!
DELETE 删除先看这个指令 post.php?action=delete&postID=132
当我们确认要删除时,这里有个注意的地方,我们能先要对该文档所属的分类下的 count_child_number 这个字段进行 一个减 1 的操作&。
为什么? 因为我也开始犯了个逻辑错误,删除后我才调用这个方法,还记得嘛!reduceChildNumber( ) 有趣地地方就是这里,让我受益匪浅!也让我调试了N久!
所以:当你的语法都没错的时候,可能是你的逻辑错了!或是方法错了!这就是我的注释! 请看:
复制代码 代码如下:  $post = new P&&& $filter[post_id] = isset( $_GET[postID] ) ? ( int )$_GET[postID] : "";&&& //& !important 在数据删除之前 先将该分类下的文章数量减 1&&& // 否则你不知道删除那个分类下的文章数量&&& // 我犯了个逻辑错误 先删除了 文档, 然后查该文档的分类ID;永远也查不到,因为已经不存在了&。&&& $post-&reduceChildNumber( "category", ( int ) $_GET[postID] );&&& $result = $post-&delete("post", $filter );
这里我们只要初始化我们文章头顶的那个 model 就可以轻松调用 delete() 方法&。
CREATE 插入先看这个指令 post.php?action=newPost说实话,我很久没有插入了&。呵呵! 看控制方法:
复制代码 代码如下:function newPost( )&{&&& $results[action] = "newPost" ;&&& $results[pageTitle] = " Add New post" ;&&& $results[newPost] = "true";&&& $results[path] = "&a href=?action=isPost &随笔&/a&& &span&添加随笔&/span&" ;&&& $post = new P&&& $cat = new C&&& $results[categories] =& $cat-&getCategoryList( "post");&&& // 新建文档&&& if( isset( $_POST[saveChanged] ))&&& {&&& &&&&&&& $post-& storePostFormValues( $_POST );&&&&&&& $result = $post-&insertPost( );&&&&&&& if( $result )&&&&&&& {&&&&&&&&&&& $post-&addChildNumber( "category", $_POST[category] );&&&&&&&&&&& header("Location: post.php?action=isPost&status=Inserted"); &&&&&&& }&&&&&&& else &&&&&&& {&&&&&&&&&&& header("Location: post.php?action=isPost&error=InsertedFailed"); &&&&&&& }&&&&&&& // 保存到草稿箱&&& } else if( isset( $_POST[saveDraft]) )&&& {&&&&&&& $post = new P&&&&&&& $post-& storePostFormValues( $_POST );&&&&&&& $post-&saveDraft( );&&&&&&& header("Location: post.php?action=isPost&status=postSaveToDraft"); &&&&&&& // cancel&&& } else if( isset( $_POST[cancel] ))&&& {&&&&&&& header("Location: post.php?action=isPost");&&& } &&& else&&& {&&&&&&& require_once(TEMPLATE_PATH . "/post/post_edit.php");&&& }&}
我们使用一个模版来同时进行文档的插入和更新&。关键就是 isset( ),当我们调用控制器的 newPost 方法时,我们并没有往模版中传入文档&。所以在模版中,我们用 isset() 来做判断时,我们获得了空值,是个好东西;这样我们不会输出任何内容到模版中去&。这样,我们等待用户提交表单,在这里,我为了省事,暂时没有对表单进行过滤,不过我留了个后门以后来更新&。 好的,假设我们的表单被提交了,(也被你基本的过滤了)&。
我们调用 post model中 的 storePostFormValues( ) , storeDiaryFormValues( ); 记得嘛,这个方法把所有的表单内容放入一个数组,在做了基本的类型检查之后,到这里已经成功一半了&。下面就是 insert***()&。 这就是mysql 万能数据库操作类的好处,它能帮你处理各种表单,各种类型&。当然你如果要求更细,更多,你可以继承它,扩展它的方法,或新建方法&。 到这里离完成还有一步,addChildNumber( )&。 当你为你的文档选择分类时,同时也要在相应的分类表中的 count_child_number中加 1 &。如果用户选择将文档放入草稿箱的话,只需插入一个 type = PostDraft 的文档记录&。UPDATE 更新先看这个指令 post.php?action=updatePost&postID=132更新首先就要获得这个文档的数据,postID, 同样是 GET方法得到&。 这样我们就可以初始化表单中的 value 值了&。 isset( ) 在这里起了关键作用,不是嘛?
后面的部分大同小异, storePostFormValues( ) , storeDiaryFormValues( ); 然后你调用 post model update***( ) &。看代码:
复制代码 代码如下:function updatePost( )&{&&& $results[action] = "updatePost";&&& $results[pageTitle] = "Edit post";&&& $post = new P&&& $cat = new C&&& $results[categories] =& $cat-&getCategoryList("post");&&& if( isset( $_POST[saveChanged] ))&&& {&&&&&&& // do update&&&&&&& $post-&storePostFormValues( $_POST );&&&&&&& $post-&updatePost( );&&&&&&& header("Location: post.php?action=isPost&status=changesSaved") ;&&&&&&&&&&&&&&&&&&& &&& } else if( isset( $_POST[cancel] ) )&&& {&&&&&&& header("Location: post.php?action=isPost&status=Cancel");&&& }else &&& {&&&&&&& // get the post&&&& &&&&&&& $postID = isset( $_GET[postID] ) ?& $_GET[postID] : " ";&&&&&&& $results[post] = $post-&getPostByID( $postID ); &&&&&&& require_once(TEMPLATE_PATH . "/post/post_edit.php");&&& }&}
到这里就差不多了,我们实现了几乎所有的基本操作&。
几点说明,这些action 有的是导航,有的是生成的,大部分是固定的&。自己看着用吧&。 下篇说说 分类的事!还有就是这篇博客写完后会放在一个网站上
你如果想要源码学习的话,我会提供下载&。谢谢你花这么长时间听我唠叨, 看到这句的人,祝你们 昨天 6,1 快乐,嘿嘿&。
废话不,上一篇有个核心概念就是 give action do something ! 这篇我就用代码来解释这个概念是啥意思,先看我的 post.class.php . 这个文件是我们的数据层处理类&。
简单介绍一下这个model 类,它继承了一个数据库基类来做crud 等常用操作, 每次初始化时就会初始化一个数据库对象 $db. 我们就用这个对象来操作我们的数据&。对于数据操作有2个重要方法 storePostFormValues( ) , storeDiaryFormValues( ),它们2个方法是数据流的开始&。还有2个方法很有意思,addChildNumber( ), reduceChildNumber( ),& 它们负责在插入或删除文档时的 一个暗箱操作&。因为我的文档可以用多个分类,所以在操作文档的时候,要考虑到一个问题,就是 category 表中有个字段 记录了该分类下的 文档数量&。所以要动态地改变这些数目的值&。下面配合 post.php 控制器,我们就可以开始我们数据的流程了(我的控制器还不是一个类,所以无法生成API文档&。因为这还不是真正地MVC架构&。)所以在MVC之前,这个也能更利于的理解MVC到底是神马东东,以及你自己如何去应用,写出自己的MVC&。
以下的情形都是假设:
$action = "天上掉下个女朋友给我吧!"; 让我们传入这个控制器看会发生神马事情&。
复制代码 代码如下:require_once( "config/config.php" );session_start( );$action = isset( $_GET[action] ) ? $_GET[action] : "";$username = isset( $_SESSION[username] ) ? $_SESSION[username] : "";
&if( !$username )&{&&& header("Location: index.php?action=login");&&&&}
这里我们有个重要流程控制语句 switch , 这个单词是 开关的意思; 所以当上面那个 $action = "天上掉下个女朋友给我吧!"; 传入 switch 时,只有2种可能,一种是开,一种是关&。这里有点双关地意思,有些同学可能看出来了&。嘿嘿!
言归正传:看看我们的 switch 是如何开关这些 $action , 很明显 天上不会掉个女朋友给我,因为控制器里没有这个开关,所以只能还是说说代码的事&。
复制代码 代码如下:switch( $action )&{&&& case "newPost" :&&&&&&& newPost( );&&&&&& case "delete" :&&&&&&& delete( ) ;&&&&&& case "updatePost":&&&&&&& updatePost( );&&&&&& case "IsDraft":&&&&&&& listDraft( );&&&&&& case "logout" :&&&&&&& logout( );&&&&&&&& case "isPost":&&&&&&& listPost( );&&&&&& case "diffentCategoryPost":&&&&&&& diffentCategoryPost( );&&&&&& case "unCategory":&&&&&&& unCategory( );&&&&&& default :&&&&&&& listPost( );&&&&}
每个switch都应该定义默认的 开关,这样当没有女朋友的时候,可以确保我们还有基友&。如何传入 action 呢?来看这样一个url,也就是我们的后台框架的导航, post.php?action=isPost& 这个是一个标准的action, 我们每个url 其实都是由这些action组成的,也可以加入其他的一些参数到我们的url 中, 这样我们可以在控制器定义的方法中 GET (得到这些变量的值),然后我们可以多些控制&。好了,当这个url 到达我们的控制器后,我们接收判断,然后打开一个 isPost 的开关,这样我们就可以调用后面的方法了,想想 开关灯,开关电脑,开关就是我们经常做的事&。这里我们只是换了一个地方&。ok &。 来看看这个开关的下面的方法&。
复制代码 代码如下:function listPost( ){&&& $results = array( );&&& $results[pageTitle] = "Post List" ;&&& $results[path] = "&a href=?action=isPost &随笔&/a&";&&& // set the message &&& if ( isset( $_GET[error] ) ) &&& {&&&&&&& if ( $_GET[error] == "InsertedFailed" ) $results[errorMessage] = "文档添加失败";&&&&&&& if ( $_GET[error] == "postDeleteFailed" ) $results[errorMessage] = "文档删除失败";&&& }&&& if ( isset( $_GET[status] ) ) &&& {&&&&&&& if ( $_GET[status] == "changesSaved" ) $results[statusMessage] = "文档保存了!";&&&&&&& if ( $_GET[status] == "Deleted" ) $results[statusMessage] = "文档删除了!";&&&&&&& if ( $_GET[status] == "Inserted" ) $results[statusMessage] = "你添加了新的文档!";&&&&&&& if ( $_GET[status] == "SaveToDraft" ) $results[statusMessage] = "文档保存到了草稿箱!";&&& }&&& // 文档的分类浏览 &&& $db = MySQL::getInstance( );&&& $pagination = new P&&& $cat = new C&&& $results[categories] =& $cat-&getCategoryList("post");&&& $pagination-&countSQL = "select * from post where type = post " ;&&& $db-&Query( $pagination-&countSQL );&&& $pagination-&totalRecords = $db-&RowCount( );&&& $records = $db-&HasRecords( $pagination-&rebuiltSQL( ) );&&& if( $records )&&& {&&&&&&& $results[posts] = $db-&QueryArray( $pagination-&rebuiltSQL( ) );&&&&&&& require_once(TEMPLATE_PATH . "/post/post_list.php");&&& } &&& else &&& {&&&&&&& require_once(TEMPLATE_PATH . "/post/post_list.php");&&& }&}
我们定义了一个数组,$results = array( ); 这个数组的作用明显,它将保存我们从 model 中获取的任何数据,也可以保存从url上 GET 的特殊参数&。然后将在我们下面require_once(*****) 包含的模版中显示出来, 路径定义在了 path 变量中&。
同时我们会接收2个提示参数,
error , 表示操作出现错误,任何人都在所难免,包括电脑,谁都会犯错,关键是去承认,电脑做的很好,他们勇于承认错误&。
表示状态,就是成功的操作&。
$pagination = new P 这个类是我们的分页类,我们传入一个 总的数量给它,然后它自己会算出总页数,每跳转一个页面,相当于刷新了一次,所以大家的做法就是,在构造器里 GET(获取)url上的page 的值,让我们知道是当前那一页了&。同时我们重新生成了查询的语句,后面加上一条限制的语句,类似 limit $start(起始的id), $offset(长度); 原理就是从这个id起,往后给我10 条记录;我的设定就是 10 条,你也可以更灵活&。$cat = new C这个类后面会详细说,也是非常重要的分类model&。这里我们就是简单获取 这个类型下的所有分类,显示在侧边栏,我已经完成了&。有图有真相!
这样 我们的 $results 数组中就储存了我们页面所需的所有数据&。 好的,来看看我们的模版,是怎么输出的&。
复制代码 代码如下:&&!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&&&html&&&&& &head&&&&&&&&& &title&&&&&&&&&&&&& 博客后台管理&/title&&&&&&&&&&&&& &meta http-equiv="Content-Type" content="text/ charset=utf-8"/&&&&&&&&&&&&& &link rel="stylesheet" type="text/css" href="assert/css/blog.css" /&&&&&&&&&&& &&&& &/head&&&& &&&& &body id="Posts"&&&&&&&&&&&&& &table id="BodyTable" border="0" cellpadding="0" cellspacing="0" width="100%"&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&& &td id="Header" colspan="2"&&div id="SiteNav"&&/div&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&& &div id="BlogTitle"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& Arists Blog&&&&&&&&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&&&&&&&&&& &div id="Site Title"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &b&&blockquote&Hinging there, everything will be fine.&/blockquote&&/b&&&&&&&&&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&& &td&&&&&&&&&&&&&&&&&&&&&&&&& &div id="LeftNavHeader"&操作&/div&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &td class="NavHeaderRow"&&&&&&&&&&&&&&&&&&&&&&&&& &ul id="TopNav"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="post.php?action=IsPost"&随笔&/a&&/li&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="article.php?action=IsArticle"&文章&/a&&/li&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="diary.php?action=IsDiary"&日记&/a&&/li&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="comment.php?action=IsComment"&评论&/a&&/li&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="photo.php?action=IsPhoto"&相片&/a&&/li&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&& &/ul&&&&&&&&&&&&&&&&&&&&&&&&& &div id="SubNav"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 当前位置:&&?php if( isset( $results[path] )) echo $results[path]; ?&&&&&&&&&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&& &td class="NavLeftCell"&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&&&&&& &div class="left_nav"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &ul id="LinksActions"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="article.php?action=newArticle"&& 添加新文章&/a&&/li&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &/ul&&&&&&&&&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&&&&&&&&&& &div id="CategoriesHeader" class="LeftNavHeader"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 分类&&&&&&&&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&&&&&&&&&& &div class="left_nav"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &ul id="LinksCategories"&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="category.php?action=ListCat&type=article"&[编辑分类]&/a&&/li&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="article.php?action=IsArticle"&[所有分类]&/a&&/li&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &li&&a href="article.php?action=unCategory"&[未分类]&/a&&/li&&&?php&if( isset( $results[categories] )& && ! empty( $results[categories] ) ){&&&& foreach( $results[categories] as $category ){&&&& echo &&&EOB&&&&&&&&&&&&&&&&&&&& &li&&a href="article.php?action=diffentCategoryArticle&catID={$category[category_id]}"&{$category[name]}({$category[count_child_number]})&/a&&/li&&EOB;&&&& }&}&?&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &/ul&&&&&&&&&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&&&&&& &td id="Body"&&&&&&&&&&&&&&&&&&&&&&&&& &div id="Main"&&&&&&&&&&&&&&&&& &div id="Editor_Messages"&&&&&&&&&&&&&&&&& &!-- 显示提示信息 --&&&&&&&&&&&&&&&&& &?php &&&&&&&&&&&&&&&&&&&& if( isset( $results[statusMessage] )){echo& $results[statusMessage];}&&&&&&&&&&&&&&&&&&&& if( isset( $results[errorMessage] )){echo& $results[errorMessage];}&&&&&&&&&&&&&&&& ?&&&&&&&&&&&&&&&&& &/div&&&div id="Editor_Results"&&&&& &div id="Editor_Results_Header" class="CollapsibleHeader"&&&&&&&&& &span id="Editor_Results_headerTitle"&文章(主要用于转载,发布原创博文要通过“随笔”)&/span&&&&& &/div&&&&& &div id="Editor_Results_Contents"&&&&& &&?php& &&&& if( isset( $results[posts] )){&&&& echo &&&EOB&&&&&&&&&&&& &table id="Listing" class="Listing" cellspacing="0" cellpadding="0" border="0" style=width:98%;&&&&&&&&&&&&&&&&& &tr style="text-align: center"&&&&&&&&&&&&&&&&&&&&& &th valign="bottom"&&&&&&&&&&&&&&&&&&&&&&&&& 标题&&&&&&&&&&&&&&&&&&&& &/th&&&&&&&&&&&&&&&&&&&&& &th width="40" style="text-align: center"&&&&&&&&&&&&&&&&&&&&&&&&& 发布&br /&&&&&&&&&&&&&&&&&&&&&&&&& 状态&&&&&&&&&&&&&&&&&&&& &/th&&&&&&&&&&&&&&&&&&&&& &th valign="bottom" width="50" style="text-align: center"&&&&&&&&&&&&&&&&&&&&&&&&& 评论&&&&&&&&&&&&&&&&&&&& &/th&&&&&&&&&&&&&&&&&&&&& &th width="50" style="text-align: center"&&&&&&&&&&&&&&&&&&&&&&&&& 页面&br /&&&&&&&&&&&&&&&&&&&&&&&&& 浏览&&&&&&&&&&&&&&&&&&&& &/th&&&&&&&&&&&&&& &&&&&&&&&&&&&&&&&&&& &th valign="bottom" width="40" style="text-align: center"&&&&&&&&&&&&&&&&&&&&&&&&& 操作&&&&&&&&&&&&&&&&&&&& &/th&&&&&&&&&&&&&&&&&&&&& &th valign="bottom" width="40" style="text-align: center"&&&&&&&&&&&&&&&&&&&&&&&&& 操作&&&&&&&&&&&&&&&&&&&& &/th&&&&&&&&&&&&&&&&& &/tr&&EOB;&&&&&&&& foreach( $results[posts] as $post ){&&&&&&&&&&&& $time = date("Y-m-d H:i:s", $post[create_time]);&&&&&&&&&&&& if( $post[status] == "1" ){&&&&&&&&&&&&&&&& $post[status]& = "发布";&&&&&&&&&&&& }&&& else {&&&&&&&&&&&&&&&& $post[status]& = "&b&未发布&/b&";&&&&&&&&&&&& }&&&&&&&&&&&& echo &&&EOB&&&&&&&&&&&& &tr id="entry_{$post[post_id]}" class="Alt"&&&&&&&&&&&&&&&&& &td style="text-align: left"&{$post[title]} ({$time})&/td&&&&&&&&&&&&&&&&& &td style="text-align: center"&{$post[status]}&/td&&&&&&&&&&&&&&&&& &td style="text-align: center"&{$post[view_count]}&/td&&&&&&&&&&&&&&&&& &td style="text-align: center"&{$post[comment_count]}&/td&&&&&&&&&&&&&&&&& &td style="text-align: center"&&a href="article.php?action=editArticle&postID={$post[post_id]}"&编辑&/a&&/td&&&&&&&&&&&&&&&&& &td style="text-align: center"&&a href="JavaScript:if(confirm(从数据库中删除这篇文档?)==true){window.location=article.php?action=delete&postID={$post[post_id]};}"&删除&/a&&/td&&&& &&&&&&&&&&&& &/tr&&EOB;&&&&&&&& }&&&&&&&&&&&& echo "&/table&";&&&&&&&&&&&&&&& &&&&&&&&&&&& if( isset( $pagination) ){$pagination-&createLinks( ) ;}&&&& } else {&&&&&&&& echo "当前无内容!";&&&& }&?&&& &&&& &/div&&&/div& &&span id="currentPostId" style="display:"&&/span&&&&&&&&&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&& &/table&&&&&&&&&&&&& &div id="blog_top_nav_block"&&&&&&&&&&&&&&&&& &div id="site_nav"&&&&&&&&&&&&&&&&& &/div& &&&&&&&&&&&&&&&& &div id="login_area"&&&&&&&&&& &&&&&&&&&&&&&&&&& &span id="span_userinfo"&&b&&?php& echo $_SESSION[username]; ?& &/b&& &a href="?action=logout"&logout&/a&&/span&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&& &div class="clear"&&/div& &&&&&&&&&&&& &/div&&&&&&&&&&&&& &table id="Footer" border="0" cellpadding="0" cellspacing="0" width="100%"&&&&&&&&&&&&&&&&& &tr&&&&&&&&&&&&&&&&&&&&& &td colspan="2"&&&&&&&&&&&&&&&&&&&&&&&&& &div&&&&&&&&&&&&&&&&&&&&&&&&&&&&& & &?php echo date("Y", time( ) ); ?& Arist&&&&&&&&&&&&&&&&&&&&&&&& &/div&&&&&&&&&&&&&&&&&&&&& &/td&&&&&&&&&&&&&&&&& &/tr&&&&&&&&&&&&& &/table&&&&& &/body&&&/html&
以上只是显示数据,人人都会啊&。
我们怎么操作这些数据呢?
操作,就像是一种控制能力&。 学生时代踢足球,我对球场有一种很强的控制能力,大学足球比赛拿了1次冠军,1次亚军,1次季军,大四没去,中学更是无数荣誉&。
我的位置是中卫,在足球场上,这个位置,你得有统观全局的能力,也得有很强的个人能力,还有指挥能力;扯的远了,现在天天坐在电脑前,这些东西也早就没了,
就剩下些经验之谈&。不过其中滋味,你也须也体验过&。
我这个博客有个缺点,每次你对数据库进行一次读写操作,你得刷新啊!我知道这对服务器的负载很大,但是我觉得如果一个新技术你没有完全吃透,盲目运用,只会适得其反&。
所以暂时我还是牺牲服务器的响应时间,内存消耗,来获得一种相对的稳定!
所以我对全局还不是很了解,还有很多未知地领域没有涉入,如深入ajax,深入php,c &。&。&。 不多说了&。
好了,看看怎么对数据进行CRUD 吧!
DELETE 删除先看这个指令 post.php?action=delete&postID=132
当我们确认要删除时,这里有个注意的地方,我们能先要对该文档所属的分类下的 count_child_number 这个字段进行 一个减 1 的操作&。
为什么? 因为我也开始犯了个逻辑错误,删除后我才调用这个方法,还记得嘛!reduceChildNumber( ) 有趣地地方就是这里,让我受益匪浅!也让我调试了N久!
所以:当你的语法都没错的时候,可能是你的逻辑错了!或是方法错了!这就是我的注释! 请看:
复制代码 代码如下:  $post = new P&&& $filter[post_id] = isset( $_GET[postID] ) ? ( int )$_GET[postID] : "";&&& //& !important 在数据删除之前 先将该分类下的文章数量减 1&&& // 否则你不知道删除那个分类下的文章数量&&& // 我犯了个逻辑错误 先删除了 文档, 然后查该文档的分类ID;永远也查不到,因为已经不存在了&。&&& $post-&reduceChildNumber( "category", ( int ) $_GET[postID] );&&& $result = $post-&delete("post", $filter );
这里我们只要初始化我们文章头顶的那个 model 就可以轻松调用 delete() 方法&。
CREATE 插入先看这个指令 post.php?action=newPost说实话,我很久没有插入了&。呵呵! 看控制方法:
复制代码 代码如下:function newPost( )&{&&& $results[action] = "newPost" ;&&& $results[pageTitle] = " Add New post" ;&&& $results[newPost] = "true";&&& $results[path] = "&a href=?action=isPost &随笔&/a&& &span&添加随笔&/span&" ;&&& $post = new P&&& $cat = new C&&& $results[categories] =& $cat-&getCategoryList( "post");&&& // 新建文档&&& if( isset( $_POST[saveChanged] ))&&& {&&& &&&&&&& $post-& storePostFormValues( $_POST );&&&&&&& $result = $post-&insertPost( );&&&&&&& if( $result )&&&&&&& {&&&&&&&&&&& $post-&addChildNumber( "category", $_POST[category] );&&&&&&&&&&& header("Location: post.php?action=isPost&status=Inserted"); &&&&&&& }&&&&&&& else &&&&&&& {&&&&&&&&&&& header("Location: post.php?action=isPost&error=InsertedFailed"); &&&&&&& }&&&&&&& // 保存到草稿箱&&& } else if( isset( $_POST[saveDraft]) )&&& {&&&&&&& $post = new P&&&&&&& $post-& storePostFormValues( $_POST );&&&&&&& $post-&saveDraft( );&&&&&&& header("Location: post.php?action=isPost&status=postSaveToDraft"); &&&&&&& // cancel&&& } else if( isset( $_POST[cancel] ))&&& {&&&&&&& header("Location: post.php?action=isPost");&&& } &&& else&&& {&&&&&&& require_once(TEMPLATE_PATH . "/post/post_edit.php");&&& }&}
我们使用一个模版来同时进行文档的插入和更新&。关键就是 isset( ),当我们调用控制器的 newPost 方法时,我们并没有往模版中传入文档&。所以在模版中,我们用 isset() 来做判断时,我们获得了空值,是个好东西;这样我们不会输出任何内容到模版中去&。这样,我们等待用户提交表单,在这里,我为了省事,暂时没有对表单进行过滤,不过我留了个后门以后来更新&。 好的,假设我们的表单被提交了,(也被你基本的过滤了)&。
我们调用 post model中 的 storePostFormValues( ) , storeDiaryFormValues( ); 记得嘛,这个方法把所有的表单内容放入一个数组,在做了基本的类型检查之后,到这里已经成功一半了&。下面就是 insert***()&。 这就是mysql 万能数据库操作类的好处,它能帮你处理各种表单,各种类型&。当然你如果要求更细,更多,你可以继承它,扩展它的方法,或新建方法&。 到这里离完成还有一步,addChildNumber( )&。 当你为你的文档选择分类时,同时也要在相应的分类表中的 count_child_number中加 1 &。如果用户选择将文档放入草稿箱的话,只需插入一个 type = PostDraft 的文档记录&。UPDATE 更新先看这个指令 post.php?action=updatePost&postID=132更新首先就要获得这个文档的数据,postID, 同样是 GET方法得到&。 这样我们就可以初始化表单中的 value 值了&。 isset( ) 在这里起了关键作用,不是嘛?
后面的部分大同小异, storePostFormValues( ) , storeDiaryFormValues( ); 然后你调用 post model update***( ) &。看代码:
复制代码 代码如下:function updatePost( )&{&&& $results[action] = "updatePost";&&& $results[pageTitle] = "Edit post";&&& $post = new P&&& $cat = new C&&& $results[categories] =& $cat-&getCategoryList("post");&&& if( isset( $_POST[saveChanged] ))&&& {&&&&&&& // do update&&&&&&& $post-&storePostFormValues( $_POST );&&&&&&& $post-&updatePost( );&&&&&&& header("Location: post.php?action=isPost&status=changesSaved") ;&&&&&&&&&&&&&&&&&&& &&& } else if( isset( $_POST[cancel] ) )&&& {&&&&&&& header("Location: post.php?action=isPost&status=Cancel");&&& }else &&& {&&&&&&& // get the post&&&& &&&&&&& $postID = isset( $_GET[postID] ) ?& $_GET[postID] : " ";&&&&&&& $results[post] = $post-&getPostByID( $postID ); &&&&&&& require_once(TEMPLATE_PATH . "/post/post_edit.php");&&& }&}
到这里就差不多了,我们实现了几乎所有的基本操作&。
几点说明,这些action 有的是导航,有的是生成的,大部分是固定的&。自己看着用吧&。 下篇说说 分类的事!还有就是这篇博客写完后会放在一个网站上
你如果想要源码学习的话,我会提供下载&。谢谢你花这么长时间听我唠叨, 看到这句的人,祝你们 昨天 6,1 快乐,嘿嘿&。
Google搜索中
搜狗搜索中
在线教程导航
数据库开发
产品库推荐
| 站长工具:
All Rights Reserved.
珠江路在线版权所有
苏ICP备号 中文域名:
 |  | }

我要回帖

更多关于 cas logout 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信