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

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

Categories drop down in the form.

When you fill out the form and clicked the "Create" button.

Changes in the database.

6.0 READING AND PAGING RECORD IN PHP THE OOP WAY

In this part of our PHP OOP CRUD tutorial, we will list the records from the database.

6.1 Create File: index.php

Create a new file and name it index.php. This file will show the main page of our web app. Put the following code inside it.

// set page header

$page_title = "Read Products";

include_once "layout_header.php";

 

// contents will be here

 

// set page footer

include_once "layout_footer.php";

?>

6.2 Add a "Create Product" button

The following code will render a button. When this button was clicked, it will show us a page where we can create a record. Replace the // contents will be here comments in the previous section with the following code.

echo "

";

 

6.3 Configure Pagination Variables

Pagination is very important if you have thousands of data from the database. Put the following code before the set page header comment of index.php file.

// page given in URL parameter, default page is one

$page = isset($_GET['page']) ? $_GET['page'] : 1;

 

// set number of records per page

$records_per_page = 5;

 

// calculate for the query LIMIT clause

$from_record_num = ($records_per_page * $page) - $records_per_page;

 

// retrieve records here

6.4 Retrieve Records from the Database

Now we will retrieve data from the database. Replace // retrieve records here comment of index.php with the following code.

// include database and object files

include_once 'config/database.php';

include_once 'objects/product.php';

include_once 'objects/category.php';

 

// instantiate database and objects

$database = new Database();

$db = $database->getConnection();

 

$product = new Product($db);

$category = new Category($db);

 

// query products

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

$num = $stmt->rowCount();

6.5 Add readAll() Method in product.php

Retrieving records in the previous section won't work without this method. Put the following code inside our "product.php" file which is inside the "objects" folder.

function readAll($from_record_num, $records_per_page){

 

    $query = "SELECT

                id, name, description, price, category_id

            FROM

                " . $this->table_name . "

            ORDER BY

                name ASC

            LIMIT

                {$from_record_num}, {$records_per_page}";

 

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

    $stmt->execute();

 

    return $stmt;

}

6.6 Display data from the database

This time, we will show the list of records to the user. An HTML table will hold our data. Put the following code after the section 6.2 code.

// display the products if there are any

if($num>0){

 

    echo "";

 

        echo "

";

 

            echo "

";

 

            echo "

";

 

            echo "

";

 

            echo "

";

 

            echo "

";

 

        echo "

";

 

 

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

 

            extract($row);

 

            echo "

";

 

                echo "

";

 

                echo "

";

 

                echo "

";

 

                echo "

";

 

 

                echo "

";

 

 

            echo "

";

 

 

        }

 

    echo "

Product Price Description Category Actions
{$name} {$price} {$description} ";

 

                    $category->id = $category_id;

                    $category->readName();

                    echo $category->name;

                echo "

";

 

                    // read one, edit and delete button will be here

                echo "

";

 

    // paging buttons will be here

}

 

// tell the user there are no products

else{

    echo "

No products found.
";

 

}

6.7 Put the Read, Edit and Delete Action Buttons

The following code will render three buttons: Read, Edit and Delete button.

Inside the "while" loop of the previous section, there is a comment "read one, edit and delete button will be here", replace that with the following code.

// read, edit and delete buttons

echo "'read_one.php?id={$id}' class='btn btn-primary left-margin'>

    class='glyphicon glyphicon-list'> Read

 

 

'update_product.php?id={$id}' class='btn btn-info left-margin'>

    class='glyphicon glyphicon-edit'> Edit

 

 

delete-id='{$id}' class='btn btn-danger delete-object'>

    class='glyphicon glyphicon-remove'> Delete

";

6.8 Create paging.php for Paging Buttons

The following code will show our pagination buttons. Create a new file and name it "paging.php". Open that file and put the following code.

echo "

  • ";

     

     

    // button for first page

    if($page>1){

        echo "

  • ";

     

            echo "First";

        echo "

  • ";

     

    }

     

    // calculate total pages

    $total_pages = ceil($total_rows / $records_per_page);

     

    // range of links to show

    $range = 2;

     

    // display links to 'range of pages' around 'current page'

    $initial_num = $page - $range;

    $condition_limit_num = ($page + $range)  + 1;

     

    for ($x=$initial_num; $x<$condition_limit_num; $x++) {

     

        // be sure '$x is greater than 0' AND 'less than or equal to the $total_pages'

        if (($x > 0) && ($x <= $total_pages)) {

     

            // current page

            if ($x == $page) {

                echo "

  • $x (current)
  • ";

     

            }

     

            // not current page

            else {

                echo "

  • $x
  • ";

     

            }

        }

    }

     

    // button for last page

    if($page<$total_pages){

        echo "

  • ";

     

            echo "Last";

        echo "

  • ";

     

    }

     

    echo "

";

 

?>

6.9 Add the countAll() method in objects/product.php

The following code will be used to count the total number of records in the database. This will be used for pagination.

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

// used for paging products

public function countAll(){

 

    $query = "SELECT id FROM " . $this->table_name . "";

 

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

    $stmt->execute();

 

    $num = $stmt->rowCount();

 

    return $num;

}

6.10 Include paging.php in index.php

The following code will show our pagination buttons under our records list. Put the following code after the closing "table" tag of section 6.6 above.

// the page where this paging is used

$page_url = "index.php?";

 

// count all products in the database to calculate total pages

$total_rows = $product->countAll();

 

// paging buttons here

include_once 'paging.php';

6.11 Output

Run http://localhost/php-oop-crud-level-1/index.php on your browser, you should see something like the image below.

List of records, page 1.

List of records, page 2.

Ostavite komentar

Treba vam web sajt ?

Kontakt