데이터베이스에서 장르 목록을 가져 오려고합니다. 각 노래의 데이터베이스에 장르 목록을 입력 한 다음 각 장르의 장르를 목록으로 가져 와서 가장 많이 발생하는 상위 3 개 항목으로 정렬합니다.PHP에서 데이터베이스의 상위 3 개 항목을 당깁니다
$sql = "SELECT `song_name`, `song_genres` FROM `songs` WHERE `album_id` = '$songAlbumId'";
$query = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($query)){
$song_name = $row['song_name'];
$song_genres = explode(", ", $row['song_genres']);
for ($i = 0; $i < count($song_genres); $i++){
$count=array_count_values($song_genres);//Counts the values in the array, returns associatve array
arsort($count);//Sort it from highest to lowest
$keys=array_keys($count);//Split the array so we can find the most occuring key
echo $keys[$i] . "<br>";
}
}
:
여기blues rock, garage rock, hard rock
내 코드입니다 :
(기본 패션이 아닌 실제 결과) :
장르는 그런 방식으로 하나의 텍스트 필드에 넣어 얻을 이것은 나를주는 것을 끝내게된다 :
Hard Rock
Garage Rock
Hard Rock
Garage Rock
Psychedelic Rock
Blues Rock
Garage Rock
Hard Rock
앨범 ID 나 그 밖의 다른 것에는 아무런 문제가 없습니다. 그것은 특별히 쟝르가 옳은 것으로 명령받는 것과 관련이 있습니다. 당신의 장르가
while ($row = mysqli_fetch_array($query)){
$song_name = $row['song_name'];
$song_genres = explode(", ", $row['song_genres']);
foreach($song_name as $songname){
for ($i = 0; $i < count($song_genres); $i++){
$count=array_count_values($song_genres);//Counts the values in the array, returns associatve array
arsort($count);//Sort it from highest to lowest
$keys=array_keys($count);//Split the array so we can find the most occuring key
echo $keys[$i] . "<br>";
}
}
}
'GROUP BY' 문을 사용하십시오. –
@u_mulder, 제가 사용했던 방식대로 작동합니까? 주석은 쉼표로 구분할 수 있지만 데이터베이스에서 나올 때는 긴 문자열입니다. 'GROUP BY'가 이것을 위해서 작동합니까? –