Jump to: navigation, search

Php oop product management product list

From w3cyberlearnings

Contents

Objective

List all products from the product table, and in this page we use pagination to display the products by paging.

list.php

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>List</title>
		<style>
			.odd {
				background-color: #e88989;

			}
			.even {
				background-color:#a8a8a8;
			}
		</style>
	</head>
	<body>

		<h3>Product List</h3>

		<?php
		require_once 'product.php';
		require_once 'pagination.php';

		$pagination = "";

	        $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']) 
                          ? $_REQUEST['page'] :0);
		$perpage = $_REQUEST['perpage'] = 5;
		$baseurl = 'list.php?';

		$product1 = new product();

		$count_product = $product1->count_product();
		$pagination = mypaging_bar($count_product, $page, $perpage, 
                       $baseurl, $pagevar = 'page', $nocurr = false, true);

		if ($count_product > $perpage) {
			$all_products = $product1->get_products_page($page * $perpage, $perpage);
		} else {
			$all_products = $product1->get_products();
		}

		echo $pagination;
		echo '<table border="0" cellspacing="4" cellpadding="4">';

		if ($page == 0) {
			$count = 1;
		} else {
			$count = 1 * $perpage * $page;
		}

		foreach ($all_products as $p) {
			$edit = "<a href=\"edit.php?id={$p['id']}\">[Edit]</a>";
			$detail = "<a href=\"detail.php?id={$p['id']}\">[Detail]</a>";
			$delete = "<a href=\"delete.php?id={$p['id']}\">[Delete]</a>";

			if ($count % 2 == 0) {
				echo '<tr class="even">';
			} else {
				echo '<tr class="odd">';
			}

			echo '<td>' . $count . '</td>';
			echo '<td>' . $p['pname'] . '</td>';
			echo '<td>' . $p['price'] . '</td>';
			echo '<td>' . $p['seller'] . '</td>';
			echo '<td>' . $edit . $detail . $delete . '</td>';
			echo '</tr>';
			$count++;
		}

		echo '</table>';

		echo $pagination;
		?>
		<a href="add.php">Add</a>
	</body>
</html>

css for table

This CSS uses for table's background. The odd row will use the css .odd and the even row will use the css .even

<style>
	.odd {
		background-color: #e88989;

	}
	.even {
		background-color:#a8a8a8;
	}
</style>

Step 1

Include the product.php and pagination.php files. If you want to know more about the pagination class please check

// included two php files
require_once 'product.php';
require_once 'pagination.php';

$pagination = ""; // for pagiantion

// page variable is used for display pagination 
$page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']) ? $_REQUEST['page'] : 0);
// perpage is for display record in each page, and we set the default value to 5
$perpage = $_REQUEST['perpage'] = 5;

// baseurl is required by the mypaging_bar function to generate the pagination 
$baseurl = 'list.php?';

// create the product object
$product1 = new product();

// get all products from the table
$count_product = $product1->count_product();
// generate the pagiantion link, and we set this function to return value not to display
$pagination = mypaging_bar($count_product, $page, $perpage,
 $baseurl, $pagevar = 'page', $nocurr = false, true);

Step 2

// when the total record is more than the perpage
// we need to generate paging 
if ($count_product > $perpage) {
	$all_products = $product1->get_products_page($page * $perpage, $perpage);
} else {
  // when total records less than perpage we just get all the records.
        $all_products = $product1->get_products();
}

// display pagination link
echo $pagination;

// display table 
echo '<table border="0" cellspacing="4" cellpadding="4">';

// check and generate record index
if ($page == 0) {
	$count = 1;
} else {
	$count = 1 * $perpage * $page;
}

Step 3

// loop through the product record
foreach ($all_products as $p) {
       // create the URL link for edit record
	$edit = "<a href=\"edit.php?id={$p['id']}\">[Edit]</a>";

       // create the URL link for display product detail
	$detail = "<a href=\"detail.php?id={$p['id']}\">[Detail]</a>";

       // create the URL link for delete product 
	$delete = "<a href=\"delete.php?id={$p['id']}\">[Delete]</a>";

       // assign the css to the table row
	if ($count % 2 == 0) {
		echo '<tr class="even">';
	} else {
		echo '<tr class="odd">';
	}
	// create table rows
	echo '<td>' . $count . '</td>';
	echo '<td>' . $p['pname'] . '</td>';
	echo '<td>' . $p['price'] . '</td>';
	echo '<td>' . $p['seller'] . '</td>';
	echo '<td>' . $edit . $detail . $delete . '</td>';
	echo '</tr>';
	$count++; // increasement the count
}

Step 4

echo '</table>';
// generate pagination for the bottom of the page
echo $pagination;
?>
<!-- display add link to add product-->
<a href="add.php">Add</a>

Print Screen

Php oop product management product list.png

Related Links


  1. Chapter 1: Singleton class
  2. Chapter 2: Table
  3. Chapter 3: Product Class
  4. Chapter 4: Add Product
  5. Chapter 5: Activity Menu
  6. Chapter 6: List All Products
  7. Chapter 7: Product Detail
  8. Chapter 8: Update or Edit Product
  9. Chapter 9: Delete Product
Navigation
Web
SQL
MISC
References