diff --git a/CodeIgniter-3.1.13/application/controllers/Albums.php b/CodeIgniter-3.1.13/application/controllers/Albums.php index 67abe11..8405b2a 100644 --- a/CodeIgniter-3.1.13/application/controllers/Albums.php +++ b/CodeIgniter-3.1.13/application/controllers/Albums.php @@ -12,12 +12,25 @@ class Albums extends CI_Controller { public function index($page = 1){ $limit = 21; // Nombre d'albums max par page $offset = ($page - 1) * $limit; - $albums = $this->model_music->getAlbums($limit, $offset); - $total_albums = $this->model_music->get_total_albums(); + // Récupérer les paramètres de tri et de filtre + $order_by = $this->input->get('order_by') ? $this->input->get('order_by') : 'year'; + $genre_id = $this->input->get('genre_id'); + $artist_id = $this->input->get('artist_id'); + + $albums = $this->model_music->getAlbums($limit, $offset, $order_by, $genre_id, $artist_id); + $total_albums = $this->model_music->get_total_albums($genre_id, $artist_id); + $data['total_pages'] = ceil($total_albums / $limit); $data['current_page'] = $page; $data['albums'] = $albums; + $data['order_by'] = $order_by; + $data['genre_id'] = $genre_id; + $data['artist_id'] = $artist_id; + + // Récupérer les genres et les artistes pour les filtres + $data['genres'] = $this->model_music->getGenres(); + $data['artists'] = $this->model_music->getArtists(); $this->load->view('layout/header_not_logged_dark'); $this->load->view('albums_list', $data); @@ -32,6 +45,5 @@ class Albums extends CI_Controller { $this->load->view('album_view', $data); $this->load->view('layout/footer_dark'); } - } ?> diff --git a/CodeIgniter-3.1.13/application/models/Model_music.php b/CodeIgniter-3.1.13/application/models/Model_music.php index 30a05f9..ec0293e 100644 --- a/CodeIgniter-3.1.13/application/models/Model_music.php +++ b/CodeIgniter-3.1.13/application/models/Model_music.php @@ -5,27 +5,74 @@ class Model_music extends CI_Model { $this->load->database(); } - public function getAlbums($limit, $offset){ - $query = $this->db->query( - "SELECT album.id, album.name, album.year, artist.name as artistName, genre.name as genreName, cover.jpeg - FROM album - JOIN artist ON album.artistid = artist.id - JOIN genre ON album.genreid = genre.id - JOIN cover ON album.coverid = cover.id - ORDER BY album.year - LIMIT $limit OFFSET $offset" - ); + public function getAlbums($limit, $offset, $order_by = 'year', $genre_id = null, $artist_id = null){ + $sql = "SELECT album.id, album.name, album.year, artist.name as artistName, genre.name as genreName, cover.jpeg + FROM album + JOIN artist ON album.artistid = artist.id + JOIN genre ON album.genreid = genre.id + JOIN cover ON album.coverid = cover.id"; + + if ($genre_id) { + $sql .= " WHERE genre.id = ?"; + $params[] = $genre_id; + } + + if ($artist_id) { + if ($genre_id) { + $sql .= " AND artist.id = ?"; + } else { + $sql .= " WHERE artist.id = ?"; + } + $params[] = $artist_id; + } + + $sql .= " ORDER BY album." . $order_by . " LIMIT ? OFFSET ?"; + $params[] = $limit; + $params[] = $offset; + + $query = $this->db->query($sql, $params); return $query->result(); } - public function get_total_albums(){ - $query = $this->db->query("SELECT COUNT(*) as total_albums FROM album"); + public function get_total_albums($genre_id = null, $artist_id = null){ + $sql = "SELECT COUNT(*) as total_albums FROM album + JOIN genre ON album.genreid = genre.id + JOIN artist ON album.artistid = artist.id"; + + $params = array(); // Initialiser le tableau de paramètres + + if ($genre_id) { + $sql .= " WHERE genre.id = ?"; + $params[] = $genre_id; + } + + if ($artist_id) { + if ($genre_id) { + $sql .= " AND artist.id = ?"; + } else { + $sql .= " WHERE artist.id = ?"; + } + $params[] = $artist_id; + } + + $query = $this->db->query($sql, $params); $result = $query->row(); return $result->total_albums; } + + // Méthodes pour obtenir les genres et les artistes pour les filtres + public function getGenres(){ + $query = $this->db->query("SELECT id, name FROM genre ORDER BY name"); + return $query->result(); + } + + public function getArtists(){ + $query = $this->db->query("SELECT id, name FROM artist ORDER BY name"); + return $query->result(); + } + public function get_album_by_id($id){ - // Fetch album details $query = $this->db->query( "SELECT album.id, album.name, album.year, artist.name as artistName, genre.name as genreName, cover.jpeg FROM album @@ -35,9 +82,8 @@ class Model_music extends CI_Model { WHERE album.id = ?", array($id) ); $album = $query->row(); - + if ($album) { - // Fetch album tracks $query = $this->db->query( "SELECT track.id, track.diskNumber, track.number, track.duration, song.name as songName FROM track @@ -47,10 +93,10 @@ class Model_music extends CI_Model { ); $album->tracks = $query->result(); } - + return $album; } - + public function getMusiques($limit, $offset) { $query = $this->db->query( "SELECT song.id, song.name, artist.id as artist_id, artist.name as artistName, album.name as album_name, track.albumid as album_id, cover.jpeg as cover @@ -63,14 +109,14 @@ class Model_music extends CI_Model { ); return $query->result(); } - + public function get_total_musiques(){ $query = $this->db->query("SELECT COUNT(*) as total_musiques FROM song"); $result = $query->row(); return $result->total_musiques; } - + public function getAlbumsByArtiste($artiste_id){ $query = $this->db->query(" SELECT album.id, album.name, album.year, artist.name as artistName, genre.name as genreName, cover.jpeg @@ -82,8 +128,5 @@ class Model_music extends CI_Model { ", array($artiste_id)); return $query->result(); } - - - } ?> diff --git a/CodeIgniter-3.1.13/application/views/album_musiques.php b/CodeIgniter-3.1.13/application/views/album_musiques.php deleted file mode 100644 index 0770d25..0000000 --- a/CodeIgniter-3.1.13/application/views/album_musiques.php +++ /dev/null @@ -1,31 +0,0 @@ - - -
- - - -Artiste : artistName; ?>
-Année : year; ?>
-Genre : genreName; ?>
- - - tracks)): ?> -Aucune musique n'est disponible dans cet album...
- -