CREATE TABLE `assignments`.`products` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` VARCHAR( 250 ) NOT NULL ,
`cost` DECIMAL( 10, 2 ) NOT NULL
) ENGINE = INNODB;
CREATE TABLE `assignments`.`transactions` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`order_id` INT NOT NULL ,
`income` DECIMAL( 10, 2 ) NOT NULL ,
`cost` DECIMAL( 10, 2 ) NOT NULL ,
`data` DATE NOT NULL
) ENGINE = INNODB;
CREATE TABLE `assignments`.`orders` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`client_id` INT NOT NULL ,
`product_id` INT NOT NULL ,
`data` DATE NOT NULL
) ENGINE = INNODB;
CREATE TABLE `assignments`.`clients` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` INT( 250 ) NOT NULL ,
`lastname` INT( 250 ) NOT NULL
) ENGINE = INNODB;
<?php
class Myclass {
protected $db;
public function __construct()
{
$this->db = new PDO("mysql:host=localhost;dbname=zadania", "root", "");
//mysql_connect("localhost" , "root", "");
//mysql_select_db("zadania");
}
public function getIncomeStatistics($month=1) {
$query = "SELECT DAYOFMONTH(data) AS day, SUM(income) AS income, SUM(cost) AS cost
FROM transactions GROUP BY day";
$results = $this->db->query($query);
$results = $results->fetchAll();
foreach($results as $result)
{
$item['day'] = $result['day'];
$item['profit'] = $result['income'] - $result['cost'];
$return[] = $item;
}
return $return;
}
public function getOrdersStatistics($month=1){
$query = "SELECT products.name AS product, COUNT(orders.product_id) AS orders_num
FROM products, orders WHERE products.id = orders.product_id GROUP BY products.id";
$results = $this->db->query($query);
$results = $results->fetchAll();
foreach($results as $result)
{
$item['produkt'] = $result['produkt'];
$item['liczba_zamowien'] = $result['liczba_zamowien'];
$return[] = $item;
}
return $return;
}
public function getTopClients(){
$query = "SELECT clients.name AS client, COUNT(orders.client_id) AS orders_num ,
SUM(transactions.income) - SUM(transactions.cost) AS income FROM clients, orders, transactions
WHERE clients.id = orders.client_id AND orders.id = transactions.order_id
GROUP BY clients.id ORDER BY income DESC";
$results = $this->db->query($query);
$results = $results->fetchAll();
foreach($results as $result)
{
$client['client'] = $result['client'];
$client['orders_num'] = $result['orders_num'];
$return[] = $client;
}
return $return;
}
}
?>