The DoYouHaoBaby PHP Framework中文开发文档手册
Copyright(c) DoYouHaoBaby.NET All Rights Reserved.
[ 编辑任务 ]

有时候,我们需要临时更改我们的任务,这个时候我们需要编辑任务。

编辑任务很简单,就是一个简单的更新操作。

编辑界面的代码,获取编辑的任务,位于IndexController.class.php的update_task:

  1. public function update_task(){   
  2.           
  3.        // 判断是否已经登录   
  4.        if(  $GLOBALS['___login___']===false  )   
  5.            $this->E( G::L( '你没有登录' ) );   
  6.           
  7.        $nTaskId=  intval(Safe::getGpc( 'id' ));   
  8.           
  9.        if( !empty($nTaskId) ){   
  10.              
  11.           $oTask = TaskModel::F( 'task_id=?',$nTaskId )   
  12.                    ->query();   
  13.   
  14.           if( empty($oTask['task_id']) ){   
  15.                 
  16.              $this->E( '任务不存在' );   
  17.           }   
  18.   
  19.           else{   
  20.               if( $GLOBALS['___login___']['user_id']!=$oTask['user_id'] ){   
  21.                      
  22.                   $this->E( '你不能编辑别人的任务' );   
  23.               }   
  24.   
  25.               else{   
  26.                     
  27.                   $this->assign( 'oTask',$oTask );   
  28.                   $this->display( );   
  29.               }   
  30.           }   
  31.        }   
  32.   
  33.        else  
  34.            $this->E( '你没有选择任务' );   
  35.     }  


我们在模板index_update_task.html中写下如下的代码:
  1. <include file="public_header">       
  2.       
  3.     <div id="content_wrapper">       
  4.       <div id="content">       
  5.          <form name="form_task" id="form_task" action="{:G::U('index/update_the_task')}"    
  6.   
  7. method="post">       
  8.         
  9.   <fieldset>       
  10.     <p>       
  11.       <label for="task_subject">任务主题</label> <br/>        
  12.       <input type="text" name="task_subject" id="task_subject" value="{$oTask['task_subject']}" />       
  13.     </p>       
  14.         
  15.     <p>       
  16.       <label for="task_description">任务内容</label><br/>       
  17.       <textarea name="task_description" id="task_description">{$oTask['task_description']}   
  18.   
  19. </textarea>   
  20.     </p>       
  21.         
  22.     <p>       
  23.       <input type="submit" name="Submit" value="更新任务" />      
  24.       <input type="hidden"  name="id" value="{$oTask['task_id']}"/>   
  25.     </p>       
  26.         
  27.   </fieldset>       
  28.         
  29. </form>     
  30. <p>你可以返回首页,<a href="{:G::U( 'index/index' )}">返回首页</a> </p>   
  31.       </div>       
  32.       <include file="public_sidebar">       
  33.       
  34. <include file="public_footer">    


访问首页的任务“编辑”,地址为http://localhost/wedoit/index.php/index/update_task/id/1。
你可以看到如下的界面:


修改了任务,然后我们需要保存,位于IndexController.class.php中的update_the_task:
  1. public function update_the_task(){   
  2.           
  3.         // 判断是否已经登录   
  4.         if(  $GLOBALS['___login___']===false  )   
  5.             $this->E( G::L( '你没有登录' ) );   
  6.   
  7.         $nId = intval( Safe::getGpc( 'id' ) );   
  8.         $oTask = TaskModel::F( 'task_id=?',$nId )   
  9.                        ->query();   
  10.   
  11.   
  12.         if( empty($oTask['task_id']) ){   
  13.                 
  14.             $this->E( '任务不存在' );   
  15.         }   
  16.   
  17.         else{   
  18.                
  19.             if( $GLOBALS['___login___']['user_id']!=$oTask['user_id'] ){   
  20.                      
  21.                 $this->E( '你不能编辑别人的任务' );   
  22.             }   
  23.   
  24.             else{   
  25.   
  26.                 $oTask->save( 0,'update' );   
  27.                 if( $oTask->isError() )   
  28.                     $this->E( $oTask->getErrorMessage() );   
  29.   
  30.                 $this->S( '任务更新成功' );   
  31.             }   
  32.         }   
  33.   
  34. }  


}

然后,你保存,访问首页的效果如下。
如图2:


To be continue!