227 lines
6.0 KiB
PHP
227 lines
6.0 KiB
PHP
<?php
|
|
include_once ('Database.php');
|
|
|
|
class Brand
|
|
{
|
|
|
|
public function __construct ($item = array ())
|
|
{
|
|
foreach ($item as $k => $v)
|
|
{
|
|
$this->{$k} = mysql_real_escape_string ($v);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
class BrandService extends Database
|
|
{
|
|
|
|
var $tablename = "brand";
|
|
var $connection;
|
|
|
|
public function __construct ($tablename="")
|
|
{
|
|
if (! empty($tablename))
|
|
$this->tablename = $tablename;
|
|
|
|
$this->connection = mysqli_connect (
|
|
$this->server, $this->username, $this->password, $this->databasename
|
|
);
|
|
|
|
$this->throwExceptionOnError ($this->connection);
|
|
}
|
|
|
|
public function getAllBrands ($updater = false)
|
|
{
|
|
|
|
if ($updater)
|
|
$query = "SELECT * FROM $this->tablename";
|
|
else
|
|
$query = "SELECT * FROM $this->tablename WHERE approved=1 ORDER BY name ASC";
|
|
|
|
$stmt = mysqli_prepare ($this->connection, $query);
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_execute ($stmt);
|
|
$this->throwExceptionOnError ();
|
|
|
|
$rows = array ();
|
|
|
|
mysqli_stmt_bind_result ($stmt, $row->id, $row->name, $row->website, $row->approved, $row->user_id);
|
|
|
|
while (mysqli_stmt_fetch ($stmt))
|
|
{
|
|
$rows[] = $row;
|
|
$row = new stdClass();
|
|
mysqli_stmt_bind_result ($stmt, $row->id, $row->name, $row->website, $row->approved, $row->user_id);
|
|
}
|
|
|
|
mysqli_stmt_free_result ($stmt);
|
|
mysqli_close ($this->connection);
|
|
|
|
return $rows;
|
|
}
|
|
|
|
public function getBrandByName ($name)
|
|
{
|
|
|
|
$stmt = mysqli_prepare ($this->connection, "SELECT * FROM $this->tablename where name like ?");
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_bind_param ($stmt, 's', $name);
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_execute ($stmt);
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_bind_result ($stmt, $row->id, $row->name, $row->website, $row->approved, $row->user_id);
|
|
|
|
if (mysqli_stmt_fetch ($stmt))
|
|
{
|
|
return $row;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function getBrandByID ($itemID)
|
|
{
|
|
|
|
$stmt = mysqli_prepare ($this->connection, "SELECT * FROM $this->tablename where id=?");
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_bind_param ($stmt, 'i', $itemID);
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_execute ($stmt);
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_bind_result ($stmt, $row->id, $row->name, $row->website, $row->approved, $row->user_id);
|
|
|
|
if (mysqli_stmt_fetch ($stmt))
|
|
{
|
|
return $row;
|
|
}
|
|
else
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function createBrand ($item)
|
|
{
|
|
|
|
$stmt = mysqli_prepare ($this->connection, "INSERT INTO $this->tablename (name, website, approved, user_id) VALUES (?, ?, ?, ?)");
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_bind_param ($stmt, 'ssii', $item->name, $item->website, $item->approved, $item->user_id);
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_execute ($stmt);
|
|
$this->throwExceptionOnError ();
|
|
|
|
$autoid = mysqli_stmt_insert_id ($stmt);
|
|
|
|
mysqli_stmt_free_result ($stmt);
|
|
mysqli_close ($this->connection);
|
|
|
|
return $autoid;
|
|
}
|
|
|
|
public function updateBrand ($item)
|
|
{
|
|
|
|
$stmt = mysqli_prepare ($this->connection, "UPDATE $this->tablename SET name=?, website=?, approved=?, user_id=? WHERE id=?");
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_bind_param ($stmt, 'ssiii', $item->name, $item->website, $item->approved, $item->user_id, $item->id);
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_execute ($stmt);
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_free_result ($stmt);
|
|
mysqli_close ($this->connection);
|
|
}
|
|
|
|
public function deleteBrand ($itemID)
|
|
{
|
|
|
|
$stmt = mysqli_prepare ($this->connection, "DELETE FROM $this->tablename WHERE id = ?");
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_bind_param ($stmt, 'i', $itemID);
|
|
mysqli_stmt_execute ($stmt);
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_free_result ($stmt);
|
|
mysqli_close ($this->connection);
|
|
}
|
|
|
|
public function count ()
|
|
{
|
|
$stmt = mysqli_prepare ($this->connection, "SELECT COUNT(*) AS COUNT FROM $this->tablename");
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_execute ($stmt);
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_bind_result ($stmt, $rec_count);
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_fetch ($stmt);
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_free_result ($stmt);
|
|
mysqli_close ($this->connection);
|
|
|
|
return $rec_count;
|
|
}
|
|
|
|
public function getBrand_paged ($startIndex, $numItems)
|
|
{
|
|
|
|
$stmt = mysqli_prepare ($this->connection, "SELECT * FROM $this->tablename LIMIT ?, ?");
|
|
$this->throwExceptionOnError ();
|
|
|
|
mysqli_stmt_bind_param ($stmt, 'ii', $startIndex, $numItems);
|
|
mysqli_stmt_execute ($stmt);
|
|
$this->throwExceptionOnError ();
|
|
|
|
$rows = array ();
|
|
|
|
mysqli_stmt_bind_result ($stmt, $row->id, $row->name, $row->website, $row->approved, $row->user_id);
|
|
|
|
while (mysqli_stmt_fetch ($stmt))
|
|
{
|
|
$rows[] = $row;
|
|
$row = new stdClass();
|
|
mysqli_stmt_bind_result ($stmt, $row->id, $row->name, $row->website, $row->approved, $row->user_id);
|
|
}
|
|
|
|
mysqli_stmt_free_result ($stmt);
|
|
mysqli_close ($this->connection);
|
|
|
|
return $rows;
|
|
}
|
|
|
|
private function throwExceptionOnError ($link = null)
|
|
{
|
|
if ($link == null)
|
|
{
|
|
$link = $this->connection;
|
|
}
|
|
if (mysqli_error ($link))
|
|
{
|
|
$msg = mysqli_errno ($link) . ": " . mysqli_error ($link);
|
|
throw new Exception ('MySQL Error - ' . $msg);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
?>
|