PHP OOP CRUD Tutorial – Step By Step Guide! Part 9
Dodato 9 months ago u OOP PHP od Milan Janković

PHP OOP CRUD Tutorial – Step By Step Guide! Part 9

10.5 Include core.php and read_template.php

The core.php file will be included at the beginning of index.php file. The read_template.php will be included before the layout_footer.php inclusion. The new index.php will look like the following code

<?php

// core.php holds pagination variables

include_once 'config/core.php';

 

// include database and object files

include_once 'config/database.php';

include_once 'objects/product.php';

include_once 'objects/category.php';

 

// instantiate database and product object

$database = new Database();

$db = $database->getConnection();

 

$product = new Product($db);

$category = new Category($db);

 

$page_title = "Read Products";

include_once "layout_header.php";

 

// query products

$stmt = $product->readAll($from_record_num, $records_per_page);

 

// specify the page where paging is used

$page_url = "index.php?";

 

// count total rows - used for pagination

$total_rows=$product->countAll();

 

// read_template.php controls how the product list will be rendered

include_once "read_template.php";

 

// layout_footer.php holds our javascript and closing html tags

include_once "layout_footer.php";

?>

10.6 Create search.php

This is the most important file of this section. This file will display the records based on a user's search term.

Create a new file and name it "search.php". Open that file and put the following code.

<?php

// core.php holds pagination variables

include_once 'config/core.php';

 

// include database and object files

include_once 'config/database.php';

include_once 'objects/product.php';

include_once 'objects/category.php';

 

// instantiate database and product object

$database = new Database();

$db = $database->getConnection();

 

$product = new Product($db);

$category = new Category($db);

 

// get search term

$search_term=isset($_GET['s']) ? $_GET['s'] : '';

 

$page_title = "You searched for \"{$search_term}\"";

include_once "layout_header.php";

 

// query products

$stmt = $product->search($search_term, $from_record_num, $records_per_page);

 

// specify the page where paging is used

$page_url="search.php?s={$search_term}&";

 

// count total rows - used for pagination

$total_rows=$product->countAll_BySearch($search_term);

 

// read_template.php controls how the product list will be rendered

include_once "read_template.php";

 

// layout_footer.php holds our javascript and closing html tags

include_once "layout_footer.php";

?>

10.7 Add search() and countAll_BySearch() methods

Open "product.php" file which is inside the "objects" folder. Add the following methods in the class.

// read products by search term

public function search($search_term, $from_record_num, $records_per_page){

 

    // select query

    $query = "SELECT

                c.name as category_name, p.id, p.name, p.description, p.price, p.category_id, p.created

            FROM

                " . $this->table_name . " p

                LEFT JOIN

                    categories c

                        ON p.category_id = c.id

            WHERE

                p.name LIKE ? OR p.description LIKE ?

            ORDER BY

                p.name ASC

            LIMIT

                ?, ?";

 

    // prepare query statement

    $stmt = $this->conn->prepare( $query );

 

    // bind variable values

    $search_term = "%{$search_term}%";

    $stmt->bindParam(1, $search_term);

    $stmt->bindParam(2, $search_term);

    $stmt->bindParam(3, $from_record_num, PDO::PARAM_INT);

    $stmt->bindParam(4, $records_per_page, PDO::PARAM_INT);

 

    // execute query

    $stmt->execute();

 

    // return values from database

    return $stmt;

}

 

public function countAll_BySearch($search_term){

 

    // select query

    $query = "SELECT

                COUNT(*) as total_rows

            FROM

                " . $this->table_name . " p

            WHERE

                p.name LIKE ? OR p.description LIKE ?";

 

    // prepare query statement

    $stmt = $this->conn->prepare( $query );

 

    // bind variable values

    $search_term = "%{$search_term}%";

    $stmt->bindParam(1, $search_term);

 

    $stmt->execute();

    $row = $stmt->fetch(PDO::FETCH_ASSOC);

 

    return $row['total_rows'];

}

10.8 Output

php-mysql-oop-crud-tutorial

11.0 FILE UPLOAD IN PHP THE OOP WAY

In this section, we will add a "file upload" feature. This feature is included in the LEVEL 2 source code download.

11.1 Change HTML form

Open create_product.php and find the "form" tag. Change that line to the following code. The "enctype" enables the form to submit a file to the server.

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">

On the same HTML table, find the closing "tr" tag of the "Category" field. Add the following code. This adds an input field where the user can browse the file he wants to upload.

<tr>

    <td>Photo</td>

    <td><input type="file" name="image" /></td>

</tr>

11.2 Set value of "image" field

Open create_product.php and add the new "image" field. The value will be the file name of the submitted file. We used the built-in sha1_file() function the make the file name unique.

Open create_product.php file. Place the following code under $product->category_id = $_POST['category_id']; code.

$image=!empty($_FILES["image"]["name"])

        ? sha1_file($_FILES['image']['tmp_name']) . "-" . basename($_FILES["image"]["name"]) : "";

$product->image = $image;

Ostavite komentar

Treba vam web sajt ?

Kontakt