Cody Cook 635b3ddcbc
Some checks failed
SonarQube Scan / SonarQube Trigger (push) Failing after 5m27s
Address changes.
2025-02-22 17:20:19 -08:00

69 lines
1.7 KiB
PHP

<?php
namespace DJMixHosting;
class Genres
{
private $db;
private $genres = [];
public function __construct($db)
{
$this->db = $db;
if (!$this->load_all_genres()) {
return false;
} else {
return true;
}
}
private function load_all_genres(): bool
{
$genres = $this->get_all_genres();
if ($genres) {
$this->genres = $genres;
return true;
} else {
return false;
}
}
public function get_all_genres($order = "ASC")
{
$stmt = $this->db->prepare("SELECT * FROM genres ORDER BY name $order");
$stmt->execute();
$result = $stmt->get_result();
$genres = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $genres;
}
public function get_nonzero_genres()
{
$stmt = $this->db->prepare("SELECT * FROM genres WHERE count > 0 ORDER BY name ASC");
$stmt->execute();
$result = $stmt->get_result();
$genres = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $genres;
}
public function search(string $query, int $page = 1, int $resultsPerPage = 10): array {
$offset = ($page - 1) * $resultsPerPage;
$likeQuery = "%" . $query . "%";
$stmt = $this->db->prepare("SELECT * FROM genres WHERE name LIKE ? LIMIT ?, ?");
$stmt->bind_param("sii", $likeQuery, $offset, $resultsPerPage);
$stmt->execute();
$result = $stmt->get_result();
$genres = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $genres;
}
}