在SAVE和SAVE& PUBLISH之间创建条件

时间:2013-04-16 04:23:05

标签: publish conditional-statements

过去几天,我的项目似乎让人头晕目眩。我正在尝试构建一个自定义cms,我正在为网站页面创建帖子。

我有两(2)个按钮 - > '保存'和'保存& PUBLISH 'SAVE按钮会将我的表单字段提交到数据库,但将数字1放在发布字段中,这样当SAVE& PUBLISH执行时,它将不会被处理为已发布同样但是会将数字1加到发布字段中,因此它将被处理为已发布。

如何使用此方法构建条件而不会弄乱我的代码?我知道这有一点调整。请查看我当前的代码。

     // CHECK IF THE FORM WAS SUBMITTED
       if( isset( $_POST['submit'] ) ){

          $title = $_POST['title'];
          $editor_data = $_POST[ 'editor1' ];
          $format = $_POST[ 'format' ];
          $page = $_POST['page'];

        //IF FORM WAS SUBMITTED, CHECK IF THERE ARE ERRORS
        if(empty($title) === true){
           $errors[] = "You forgot to add a Title";
        }
        if(empty($editor_data) === true){
           $errors[] = "You forgot to add a Content";
        }
        if(empty($format) === true){
           $errors[] = "Please choose a title format";
        }
        if(empty($page) === true){
           $errors[] = "Please choose a Page";
        }

        //IF THE ERROR ARRAY IS EMPTY THEN PROCESS THE FORM
        if (empty($errors) === true){

           switch ($format)
           {
           case "1":
           $post_title = "<h1>".$title."</h1>";
           break;
           case "2":
           $post_title = "<h2>".$title."</h2>";
           break;
           case "3":
           $post_title = "<h3>".$title."</h3>";
           break;
           case "4":
           $post_title = "<h4>".$title."</h4>";
           break;
           case "5":
           $post_title = "<h5>".$title."</h5>";
           break;
           case "6":
           $post_title = "<h6>".$title."</h6>";
           break;
           default:
           $post_title = $title;
           }//>end of switch

           //current time zone
           date_default_timezone_set('Asia/Manila');
           $date = date('Y-m-d');
           $date_time = date('Y-m-d H:i:s');

           //INSERT VALUES FROM FORM TO THE DATABASE TABLE
           mysql_query("INSERT INTO posts (`post_title`, `post_content`, `post_page`, `date_posted`) VALUES('$post_title', '$editor_data', '$page', '$date_time')");


         // IF  ERROR ARRAY IS NOT EMPTY, OUTPUT ERRORS!
         } else if(empty($errors) === false){
             //output errors
             echo output_errors($errors);
         }//> end of else if errors

      }//>end of isset submit

1 个答案:

答案 0 :(得分:0)

解决了我自己的问题。我现在唯一关心的是如何缩短这段代码。有解决方法吗?

     // CHECK IF THE FORM WAS SUBMITTED AS SAVED
         if( isset( $_POST['save'] ) ){

            $title = $_POST['title'];
                    $editor_data = $_POST[ 'editor1' ];
            $format = $_POST[ 'format' ];
            $page = $_POST['page'];

            //IF FORM WAS SUBMITTED, CHECK IF THERE ARE ERRORS
            if(empty($title) === true){
             $errors[] = "You forgot to add a Title";
            }
            if(empty($editor_data) === true){
             $errors[] = "You forgot to add a Content";
            }
            if(empty($format) === true){
             $errors[] = "Please choose a title format";
            }
            if(empty($page) === true){
             $errors[] = "Please choose a Page";
            }

           //IF THE ERROR ARRAY IS EMPTY THEN PROCESS THE FORM
           if (empty($errors) === true){

           switch ($format)
           {
           case "1":
             $post_title = "<h1>".$title."</h1>";
           break;
           case "2":
             $post_title = "<h2>".$title."</h2>";
           break;
           case "3":
             $post_title = "<h3>".$title."</h3>";
           break;
           case "4":
             $post_title = "<h4>".$title."</h4>";
           break;
           case "5":
             $post_title = "<h5>".$title."</h5>";
           break;
           case "6":
             $post_title = "<h6>".$title."</h6>";
           break;
           default:
             $post_title = $title;
           }//>end of switch

           //current time zone
           date_default_timezone_set('Asia/Manila');
           $date = date('Y-m-d');
           $date_time = date('Y-m-d H:i:s');

           //insert values from form to server
           mysql_query("INSERT INTO posts (`post_title`, `post_content`, `post_page`, `date_posted`) VALUES('$post_title', '$editor_data', '$page', '$date_time')");


            // IF ARRAY ERROR IS NOT EMPTY, OUTPUT ERRORS!
            } else if(empty($errors) === false){
            //output errors
            echo output_errors($errors);
            }//> end of else if errors

        }else if(isset( $_POST['publish'] )){
          $title = $_POST['title'];
                  $editor_data = $_POST[ 'editor1' ];
          $format = $_POST[ 'format' ];
          $page = $_POST['page'];

          //IF FORM WAS SUBMITTED, CHECK IF THERE ARE ERRORS
          if(empty($title) === true){
        $errors[] = "You forgot to add a Title";
          }
          if(empty($editor_data) === true){
        $errors[] = "You forgot to add a Content";
          }
          if(empty($format) === true){
        $errors[] = "Please choose a title format";
          }
          if(empty($page) === true){
        $errors[] = "Please choose a Page";
          }

          //IF THE ERROR ARRAY IS EMPTY THEN PROCESS THE FORM
          if (empty($errors) === true){

        switch ($format)
        {
         case "1":
           $post_title = "<h1>".$title."</h1>";
           break;
         case "2":
           $post_title = "<h2>".$title."</h2>";
           break;
         case "3":
           $post_title = "<h3>".$title."</h3>";
           break;
         case "4":
           $post_title = "<h4>".$title."</h4>";
           break;
         case "5":
           $post_title = "<h5>".$title."</h5>";
           break;
         case "6":
           $post_title = "<h6>".$title."</h6>";
           break;
        default:
           $post_title = $title;
        }//>end of switch

        //current time zone
        date_default_timezone_set('Asia/Manila');
        $date = date('Y-m-d');
        $date_time = date('Y-m-d H:i:s');

        //insert values from form to server
        mysql_query("INSERT INTO posts (`post_title`, `post_content`, `post_page`, `date_posted`, `publish`) VALUES('$post_title', '$editor_data', '$page', '$date_time', '1')");


           // IF ARRAY ERROR IS NOT EMPTY, OUTPUT ERRORS!
           } else if(empty($errors) === false){
         //output errors
         echo output_errors($errors);
           }//> end of else if errors
        }//>end of isset publish