Some checks failed
SonarQube Scan / SonarQube Trigger (push) Failing after 5m27s
74 lines
2.0 KiB
PHP
74 lines
2.0 KiB
PHP
<?php
|
|
namespace DJMixHosting;
|
|
|
|
class Mixes
|
|
{
|
|
private $db;
|
|
private $mixes = [];
|
|
|
|
public function __construct($db)
|
|
{
|
|
$this->db = $db;
|
|
// Automatically load all mixes upon instantiation.
|
|
if (!$this->load_all_mixes()) {
|
|
// Optionally, handle errors or fallback logic here.
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Load all mixes from the database.
|
|
*
|
|
* @return bool
|
|
*/
|
|
private function load_all_mixes(): bool
|
|
{
|
|
$mixes = $this->get_all_mixes();
|
|
if ($mixes) {
|
|
$this->mixes = $mixes;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Retrieve all mixes.
|
|
*
|
|
* @param string $order The sort order (ASC or DESC).
|
|
* @return array
|
|
*/
|
|
public function get_all_mixes(string $order = "ASC"): array
|
|
{
|
|
// Assuming your mix table has a column called "name"
|
|
$stmt = $this->db->prepare("SELECT * FROM mix ORDER BY title $order");
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
$mixes = $result->fetch_all(MYSQLI_ASSOC);
|
|
$stmt->close();
|
|
return $mixes;
|
|
}
|
|
|
|
/**
|
|
* Search mixes by name and description.
|
|
*
|
|
* @param string $query The search keyword.
|
|
* @param int $page The current page number.
|
|
* @param int $resultsPerPage The number of results per page.
|
|
* @return array
|
|
*/
|
|
public function search(string $query, int $page = 1, int $resultsPerPage = 10): array
|
|
{
|
|
$offset = ($page - 1) * $resultsPerPage;
|
|
$likeQuery = "%" . $query . "%";
|
|
// Adjust the SQL if your mix table uses different column names (e.g., title instead of name)
|
|
$stmt = $this->db->prepare("SELECT * FROM mix WHERE title LIKE ? OR description LIKE ? LIMIT ?, ?");
|
|
$stmt->bind_param("ssii", $likeQuery, $likeQuery, $offset, $resultsPerPage);
|
|
$stmt->execute();
|
|
$result = $stmt->get_result();
|
|
$mixes = $result->fetch_all(MYSQLI_ASSOC);
|
|
$stmt->close();
|
|
return $mixes;
|
|
}
|
|
}
|