Mini Shell
<?php
//$rootPath = realpath(dirname(__FILE__) . '/..');
require_once $rootPath . '/PortalMM/config/config.php'; // Use clean config file
// Create connection
function getDBConnection() {
try {
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($conn->connect_error) {
throw new Exception("Connection failed: " . $conn->connect_error);
}
return $conn;
} catch (Exception $e) {
die("Database connection error: " . $e->getMessage());
}
}
class ChartData {
// Example 1: Get sales by month
public static function getSalesByMonth($year = null) {
$conn = getDBConnection();
$year = $year ?: date('Y');
$query = "
SELECT
MONTH(order_date) as month,
MONTHNAME(order_date) as month_name,
SUM(total_amount) as total_sales
FROM orders
WHERE YEAR(order_date) = ?
GROUP BY MONTH(order_date)
ORDER BY MONTH(order_date)
";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $year);
$stmt->execute();
$result = $stmt->get_result();
$data = [];
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
$stmt->close();
$conn->close();
return $data;
}
// Example 2: Get product categories count
public static function getProductCategories() {
$conn = getDBConnection();
$query = "
SELECT
c.category_name,
COUNT(p.product_id) as product_count
FROM categories c
LEFT JOIN products p ON c.category_id = p.category_id
GROUP BY c.category_id
ORDER BY product_count DESC
LIMIT 10
";
$result = $conn->query($query);
$data = [];
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
$conn->close();
return $data;
}
// Example 3: Custom query - modify as needed
public static function getCustomData($table, $categoryColumn, $valueColumn) {
$conn = getDBConnection();
$query = "
SELECT
$categoryColumn as category,
COUNT($valueColumn) as count_value
FROM $table
GROUP BY $categoryColumn
ORDER BY count_value DESC
LIMIT 15
";
$result = $conn->query($query);
$data = [];
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
$conn->close();
return $data;
}
}
?>