Search code examples
phprefactoringreusability

I want to make my form processing code reusable but how?


As it says in the title i want my code to be reusable. I dont want to edit large portions when im adding new database content.

My code that is similar across multiple files:

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" );
    }
}
}

I have different files game_new.php, game_edit.php, tech_new.php, tech_edit.php and others which all include similar code. Can anybody give me some help refactoring this stuff ?

Im still learning thanks.


Solution

  • You may want to try some PHP Classes and use it in the traditional OOP fashion. Then you can just recall some of those in code later. You could also use PDO for your SQL statements.