I have made a search script in OO PHP that searches for products based on the inserted criteria, selects them and checks whether a sale is on, if it's in stock and other features.
I am new to OOP so I would like to get your feedback on it and whether I am doing it correctly.
The whole script works but I need to know if I'm doing OK.
Many thanks.
Here is my search class (class.search.php)
<?php
Class Search{
public $db, $productTitle = array(), $productImage = array(), $productPrice = array(), $productStock = array(),
$productId = array(), $productSalePrice = array(), $productCount = 0;
function __construct($db_connection){
$this->db = $db_connection; //stores provided db connection in variable
}
function search($searchContent, $max){
$searchContent = "%".$searchContent."%";
if ($max == 0){
$searchQuery = "SELECT * FROM products WHERE title LIKE ?";
}else{
$searchQuery = "SELECT * FROM products WHERE title LIKE ? LIMIT $max";
}
try{
$stmt = $this->db->prepare($searchQuery); //querys db for products that contain the query in the title
$stmt->bindParam(1, $searchContent);
$stmt->execute();
}catch(PDOException $e) {
echo $e->getMessage();
}
$result = $stmt->fetchAll(PDO::FETCH_ASSOC); //fetches results
if ($stmt->rowCount() != 0){ ///checks if there were any results
foreach ($result as $row) { //stores all info/variables in an array
$this->productCount++; //adds to the number of products
$this->productTitle[] = $row["title"];
$this->productImage[] = $row["image"];
$this->productPrice[] = $row["price"];
$this->productSalePrice[] = $row["salePrice"];
$this->productStock[] = $row['quantity'];
$this->productId[] = $row['id'];
}
}
}
function getPriceToDisplay($productSalePrice, $regularPrice){
if ($productSalePrice != 0){
return "<span class='linePrice'>£".$regularPrice."</span> £".$productSalePrice;
}else{
return "£".$regularPrice;
}
}
function getStockClass($stock){ //method to get the class for the css
if ($stock < 10){
return "low_stock";
}else{
return "high_stock";
}
}
function getStockStatus($stock){ //method to get the stock status for the product - a few left or in stock
if ($stock < 10 && $stock != 0){
return "A few left!";
}else if ($stock == 0){
return "Out of stock";
}else{
return "In stock";
}
}
function searchError($error){ //method which deals with any errors that may happen
if ($error == "no_results"){
return "No matches to your query have been found";
}else if($error == "too_short"){
return "Your query must be at least two characters long";
}
}
}
?>
Here is the search script (searchStore.php)
<?php
include_once "includes/header.php";
require_once "classes/class.db.php";
require_once "classes/class.search.php";
if ($_SERVER["REQUEST_METHOD"] == "GET"){
if (isset($_GET['searchContent'])){$searchContent = $_GET['searchContent'];}
if (isset($_GET['order'])){$order = $_GET['order'];}
$search = new Search($db);
$search->search($searchContent, 0);
if (strLen($searchContent) >= 2 && $search->productCount != 0){
echo "<div id='searchProductsContainer'>";
for ($i = 0; $i < $search->productCount; $i++) {
$id = $search->productId[$i];
$title = $search->productTitle[$i];
$image = $search->productImage[$i];
$price = $search->productPrice[$i];
$salePrice = $search->productSalePrice[$i];
$stock = $search->productStock[$i];
echo "<div class='searchProduct'><a href='item.php?item_id=".$id."'><p class='title'>".$title."</p>
<img src='".$image."'/></a>
<p class='price_stock'>".$search->getPriceToDisplay($salePrice, $price)."<span class='".$search->getStockClass($stock)."'>"$search->getStockStatus($stock)."</span></p>
</div>";
}
echo "</div>";
}else{
$errorMessage = array();
//checks what the error is
if ($search->productCount == 0){
$errorMessage[] = $search->searchError("no_results");
}
if(strLen($searchContent) < 2){
$errorMessage[] = $search->searchError("too_short");
}
echo "<div id='searchError'>";
foreach ($errorMessage as $error){
echo "<p>".$error."</p>";
}
echo "</div>";
}
}
?>