<?php
include('../db.php');
include('functions.php');
$filtertext = $_GET['filtertext'];
#Break string into array with whitespace as the seperator
$separatedFilter = preg_split("/[\s]/", $filtertext);
#Determine query
if ($separatedFilter) {
$where = "where (artist_name like '%".$separatedFilter[0]."%' or first_name like '%".$separatedFilter[0]."%' or last_name like '%".$separatedFilter[0]."%') and";
for ($i=1; $i<count($separatedFilter); $i++) {
if ($separatedFilter[$i] != "") {
$where = $where." (artist_name like '%$separatedFilter[$i]%' or first_name like '%$separatedFilter[$i]%' or last_name like '%$separatedFilter[$i]%') and";
}
}
}
else{
$where = "where";
}
$bandmemberlist = [];
#Get member id's
$query1 = "select * from bands, members m1 $where band_id in (select band_id from band_members bm2 where bm2.member_id = m1.member_id) order by name, first_name, last_name, artist_name";
$result1 = mysqli_query($db, $query1) or die(mysqli_error($db));
while ($row1 = mysqli_fetch_array($result1)){
$temp[$row1['band_id']] = $row1['member_id'];
array_push($bandmemberlist, $temp);
$temp = [];
$combinednames[$row1['band_id']] = [];
}
foreach ($bandmemberlist as $keyOuter => $valueOuter) {
foreach ($valueOuter as $keyInner => $valueInner) {
array_push($combinednames[$keyInner], $valueInner);
}
}
#Get band id's
$query2 = "select * from bands where name like '%$filtertext%' order by name";
$result2 = mysqli_query($db, $query2) or die(mysqli_error($db));
while ($row2 = mysqli_fetch_array($result2)){
if(!$combinednames[$row2['band_id']]){
$combinednames[$row2['band_id']] = 'band';
}
}
#Show bandnames
if($combinednames){
foreach ($combinednames as $keyOuter => $valueOuter) {
$query3 = "select * from bands where band_id=$keyOuter";
$result3 = mysqli_query($db, $query3) or die(mysqli_error($db));
$row3 = mysqli_fetch_array($result3);
echo "<div><a href=bandinfo.php?band_id=$keyOuter>$row3[name]</a>";
#Check if band has members that meet the filter
if($valueOuter != 'band'){
$query4 = "select * from members where member_id=".$combinednames[$keyOuter][0];
$result4 = mysqli_query($db, $query4) or die(mysqli_error($db));
$row4 = mysqli_fetch_array($result4);
#Show first member's name
$fullname = nameDetermine($row4);
echo " (<a href=memberinfo.php?member_id=".$combinednames[$keyOuter][0].">$fullname</a>";
#Show other member's names
for ($i=1; $i < count($combinednames[$keyOuter]); $i++) {
$query5 = "select * from members where member_id=".$combinednames[$keyOuter][$i];
$result5 = mysqli_query($db, $query5) or die(mysqli_error($db));
$row5 = mysqli_fetch_array($result5);
$fullname = nameDetermine($row5);
echo ", <a href=memberinfo.php?member_id=".$combinednames[$keyOuter][$i].">$fullname</a>";
}
echo ")";
}
echo "</div>";
}
}
else{
echo "No bands or members found";
}
#You can have strange sequences of text still show names e.g. "e r b cc a ma int y m e a r" will still show Rebecca MacIntyre "Becca" because all of the fragments are in the name
#Could concat within the query to fix it but that creates a problem where the order in which each part of the name is typed matters, mainly an issue because artist_name can go anywhere
#Filter can stop working properly if you type fast
?>