正如标题中所说,我希望我的代码可以重复使用。我不想在添加新数据库内容时编辑大部分内容。
我的代码在多个文件中类似:
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和其他都包含类似代码的文件。任何人都可以帮我重构这些东西吗?
我还在学习谢谢。