【问题标题】:I want to make my form processing code reusable but how?我想让我的表单处理代码可重用但如何?
【发布时间】:2014-03-04 23:36:04
【问题描述】:

正如标题中所说,我希望我的代码可重用。当我添加新的数据库内容时,我不想编辑大部分内容。

我的代码在多个文件中相似:

if ( isset( $_POST["submit"] ) ) {

$_SESSION["message"] = "";
$sticky = $_POST["sticky"];
$visible = $_POST["visible"];
$title = mysql_prep( trim( $_POST["title"] ) );
$trailer = mysql_prep( trim( $_POST["trailer"] ) );
$description = mysql_prep( trim( $_POST["description"] ) );
$price = mysql_prep( trim( $_POST["price"] ) );
$source = mysql_prep( $_POST["source"] );
$sourceurl = mysql_prep( $_POST["sourceurl"] );
$appstore = mysql_prep( $_POST["appstore"] );
$googleplay = mysql_prep( $_POST["googleplay"] );
$device = mysql_prep( $_POST["device"] );
$rating = $_POST["rating"];

//form validations
$required_fields = array( "title", "trailer", "description" );

required_post_fields( $required_fields );
//end form validations

if( $_FILES["image_file"]["size"] != 0 ) {

    $image_ext = pathinfo( $_FILES["image_file"]["name"], PATHINFO_EXTENSION );
    $image_name = remove_bad_words( str_replace( " ", "_", strtolower( $title ) ), "/[^a-zA-Z0-9\s_]/" ) . "." . $image_ext;
    $folder_location = "../images/game/";
    $upload_path = $folder_location . $image_name;

    if( file_exists( $folder_location . $_SESSION["image_name"] ) ) {

        if( $_SESSION["image_name"] != $image_name ) {

            $_SESSION["message"] .= $_SESSION["image_name"] . " has been deleted successfully.<next>";
        }
        unlink( $folder_location . $_SESSION["image_name"] );
    }

    upload_check( $image_ext, $upload_path, $image_name );

    if( empty( $errors ) ) {

        $_SESSION["message"] .= "{$image_name} was uploaded successfully.<next>";

        move_uploaded_file( $_FILES["image_file"]["tmp_name"],  $upload_path );
    }

} else {

    $image_name = $_SESSION["image_name"];
}


if ( isset( $_POST["sticky"] ) && $sticky == 1 && empty( $errors ) ) {

    $query = "SELECT * FROM `game_content` WHERE `sticky` = 1";

    $sticky_result = mysqli_query( $connection, $query );

    if( $sticky_result && mysqli_num_rows( $sticky_result ) > 0 ) {

        $sticky_data = mysqli_fetch_assoc( $sticky_result );

        $query = "UPDATE `game_content` SET `sticky` = 0 WHERE `id` = {$sticky_data["id"]}";

        $stickied = mysqli_query( $connection, $query );
        check_query( $stickied );

        if ( $stickied && $sticky_data["id"] != $id ) {

            $_SESSION["message"] .= "{$sticky_data["title"]} is no longer stickied.<next>";
        }
    }
}

if ( empty( $errors ) ) {

    $query = "UPDATE `game_content` SET ";
    $query .= "`sticky` = '{$sticky}', ";
    $query .= "`visible` = '{$visible}', ";
    $query .= "`title` = '{$title}', ";
    $query .= "`logo` = '{$image_name}', ";
    $query .= "`trailer` = '{$trailer}', ";
    $query .= "`description` = '{$description}', ";
    $query .= "`price` = '{$price}', ";
    $query .= "`source` = '{$source}', ";
    $query .= "`sourceurl` = '{$sourceurl}', ";
    $query .= "`appstore` = '{$appstore}', ";
    $query .= "`googleplay` = '{$googleplay}', ";
    $query .= "`device` = '{$device}', ";
    $query .= "`rating` = '{$rating}' ";
    $query .= "WHERE id = {$id} ";
    $query .= "LIMIT 1";

    $result = mysqli_query( $connection, $query );
    check_query( $result );

    if ( $result ) {

        //forget the stored image name
        unset( $_SESSION["image_name"] );
        if( $sticky == 1 ) {

            $_SESSION["message"] .= "{$title} was updated successfully & has been stickied.";

        } else {

            $_SESSION["message"] .= "{$title} was updated successfully.";
        }
        redirect_to( ROOT_PATH . "/" . ADMIN_FOLDER . "/game_edit.php?id=" . $id );

    } else {

        redirect_to( ROOT_PATH . "/" . ADMIN_FOLDER . "/game.php" );
    }
}
}

我有不同的文件 game_new.php、game_edit.php、tech_new.php、tech_edit.php 和其他文件,它们都包含类似的代码。有人可以帮我重构这些东西吗?

我还在学习谢谢。

【问题讨论】:

    标签: php refactoring reusability


    【解决方案1】:

    您可能想尝试一些 PHP Classes 并以传统的 OOP 方式使用它。然后你可以稍后在代码中回忆其中的一些。您也可以将PDO 用于您的 SQL 语句。

    【讨论】:

    • 我从来没有上过课,谢谢你指引我正确的方向。
    猜你喜欢
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    相关资源
    最近更新 更多