I'm writing a simple class to manage a warehouse, so it's supposed that it will use a database connection. All works fine for now, and I'm actually working to finish the UI. Since I think to be a newbie in PHP OOP, I would appreciate much if anyone can review my code to help me improving it.
Here is the class code:
class market{
private $db;
private $stmt;
private function database(){
if($this->db === null){
$this->db = new PDO('mysql:host=localhost;dbname=marymarket','root','root');
}
return $this->db;
}
/* I need to pass an array with arguments,any help will be appreciated */
public function insert_article($barcode,$code,$qty,$brand,$article_name,$price,$article_type,$promo_stats,$note){
$stmt = $this->database()->prepare('INSERT INTO products (barcode,code,qty,brand,article_name,price,article_type, promo_stats,note) VALUES (:barcode,:code,:qty,:brand,:article_name,:price,:article_type,:promo_stats,:note)');
$stmt->bindParam(':barcode',$barcode,PDO::PARAM_STR);
$stmt->bindParam(':code',$code,PDO::PARAM_INT);
$stmt->bindParam(':qty',$qty,PDO::PARAM_STR);
$stmt->bindParam(':brand',$brand,PDO::PARAM_STR);
$stmt->bindParam(':article_name',$article_name,PDO::PARAM_STR);
$stmt->bindParam(':price',$price,PDO::PARAM_STR);
$stmt->bindParam(':article_type',$article_type,PDO::PARAM_STR);
$stmt->bindParam(':promo_stats',$promo_stats,PDO::PARAM_INT);
$stmt->bindParam(':note',$note,PDO::PARAM_STR);
if($stmt->execute()){
echo 'ok';
} else {
echo 'error';
}
}
// this part is commented because i will implement it after the UI will be ready.
//public function insert_supplier(){
//$stmt = $this->database()->prepare('INSERT INTO suppliers (code,supplier_name,iva,tel,fax,email,address,city,cap) VALUES ()');
//
//$stmt->bindParam(':',PDO::PARAM_STR);
//$stmt->bindParam(':',PDO::PARAM_STR);
//$stmt->bindParam(':',PDO::PARAM_STR);
//$stmt->bindParam(':',PDO::PARAM_STR);
//$stmt->bindParam(':',PDO::PARAM_STR);
//$stmt->bindParam(':',PDO::PARAM_STR);
//$stmt->bindParam(':',PDO::PARAM_STR);
//$stmt->bindParam(':',PDO::PARAM_STR);
//$stmt->bindParam(':',PDO::PARAM_STR);
//$stmt->bindParam(':',PDO::PARAM_STR);
//
//$stmt->execute();
//
//}
//private function insert_brand(){
//$stmt = $this->database()->prepare('INSERT INTO brands () VALUES ()');
//
//$stmt->bindParam(':',PDO::PARAM_STR);
//$stmt->bindParam(':',PDO::PARAM_STR);
//$stmt->bindParam(':',PDO::PARAM_STR);
//
//$stmt->execute();
//
//}
// TO DO
#public function mod_article(){}
// TO DO
#public function del_article(){}
public function list_articles(){
$stmt = $this->database()->query('SELECT * FROM products');
foreach($stmt as $result):
echo '<tr><td id="list-tbl-label">'.$result['barcode'].'</td>';
echo '<td id="list-tbl-label">'.$result['article_code'].'</td>';
echo '<td id="list-tbl-label">'.$result['brand'].'</td>';
echo '<td id="list-tbl-label">'.$result['article_name'].'</td>';
echo '<td id="list-tbl-label">'.$result['price'].'</td>';
echo '<td id="list-tbl-label">'.$result['article_type'].'</td></tr>';
#echo
endforeach;
}
public function search($q){
$stmt = $this->database()->prepare('SELECT * FROM products WHERE article_name LIKE :q OR brand LIKE :q OR article_code LIKE :q');
$stmt->bindParam(':q',$q,PDO::PARAM_STR);
$stmt->execute();
if($stmt->rowCount() > 0){
$results = $stmt->fetchAll();
foreach($results as $result):
echo '<table class="table table-bordered" id=""> <thead>
<tr>
<th scope="col">INT. CODE</th>
<th scope="col">BARCODE</th>
<th scope="col">QTY</th>
<th scope="col">BRAND</th>
<th scope="col">PRODUCT NAME</th>
<th scope="col">PRICE</th>
<th scope="col">SUPPLIER</th>
</tr>
</thead><tbody>';
echo '<tr><td>'.$result['article_barcode'].'</td>';
echo '<td>'.$result['article_code'].'</td>';
echo '<td>'.$result['brand'].'</td>';
echo '<td>'.$result['article_name'].'</td>';
echo '<td>'.$result['price'].'</td>';
echo '<td>'.$result['article_type'].'</td></tr></tbody></table>';
endforeach;
}
else {
return false;
}
}
}
Since I don't like to write directly PHP code on my html pages, I also wrote a controller that is called by AJAX. It's supposed that will manage all the necessary operations made by the user who post or need to get data. Here is the controller code:
require_once 'core.php';
$core = new market();
if(isset($_POST['code'] )){
$code = filter_var($_POST['code'],FILTER_SANITIZE_STRING);
# $barcode = filter_var($_POST['barcode'],FILTER_SANITIZE_INT);
$qty = filter_var($_POST['quantity'],FILTER_SANITIZE_STRING);
$brand = filter_var($_POST['brand'],FILTER_SANITIZE_STRING);
$article_name = filter_var($_POST['artName'],FILTER_SANITIZE_STRING);
$price = filter_var($_POST['price'],FILTER_SANITIZE_STRING);
$article_type = filter_var($_POST['artType'],FILTER_SANITIZE_STRING);
$note = filter_var($_POST['note'],FILTER_SANITIZE_STRING);
$save = $core->insert_article($code,$qty,$brand,$article_name,$price,$article_type,$note);
if($save){
echo 'ok';
} else {
echo 'error';
}
}
if(isset($_POST['q'])){
$q = filter_var($_POST['q'],FILTER_SANITIZE_STRING);
$results = $core->search($q);
echo $results;
}