0

在过去的几天里,我似乎对我的项目感到纠结。我正在尝试构建一个自定义 cms,并且我已经准备好为网站页面创建帖子。

我有两 (2) 个按钮 -> ' SAVE ' 和 ' SAVE & 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
4

1 回答 1

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
于 2013-04-16T04:57:33.797 回答