I have three tables (individuals, groups & recordlabels) and want use the information between them to make a listing of people within the record label. Example: It should find the individuals and groups with the companyID of # and list them. How do I create this correctly without duplicates of the results?
// Connect to database
include "mysqli_connect.php";
// Set variables
$url_num = $_GET['company_id'];
$company_num = "";
$company_members = "";
$company_members2 = "";
//Check for artist id pagination
if(is_numeric($url_num)){
$company = intval($url_num);
}else{
$company = 1;
}
// Construct our join query
$sqli = "SELECT DISTINCT * FROM recordlabels
INNER JOIN individuals ON individuals.companyID=recordlabels.companyID
INNER JOIN groups ON groups.companyID=recordlabels.companyID
WHERE recordlabels.companyID = '{$company}'";
// Create results
$result = mysqli_query($link, $sqli);
//Check for albums
$totalmembers = mysqli_num_rows($result);
// Checking if query is successful
if($result){
// Print out the contents of each row into a table
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
// Assign value of column if not empty, otherwise "DamJuNoImage" (Thanks to Jonathan Sampson from Stack Overflow)
$companyen = empty( $row["companyen"] )
? "Unknown"
: $row["companyen"];
$companyko = empty( $row["companyko"] )
? "Unknown"
: $row["companyko"];
$companyType = empty( $row["companyType"] )
? "Unknown"
: $row["companyType"];
$founded = empty( $row["established"] )
? "Unknown"
: $row["established"];
$founder = empty( $row["companyFounder"] )
? "Unknown"
: $row["companyFounder"];
$information = empty( $row["information"] )
? "Unknown"
: $row["information"];
$location = empty( $row["companyLocation"] )
? "Unknown"
: $row["companyLocation"];
$homepage = empty( $row["companyPage"] )
? "#"
: $row["homepage"];
$solopic = empty( $row["solopic"] )
? "DamjuNoImage"
: $row["solopic"];
$soloen = empty( $row["soloen"] )
? "Unknown"
: $row["soloen"];
$solokn = empty( $row["solokn"] )
? "Unknown"
: $row["solokn"];
$grouppic = empty( $row["grouppic"] )
? "DamjuNoImage"
: $row["grouppic"];
$groupen = empty( $row["groupen"] )
? "Unknown"
: $row["groupen"];
$groupkn = empty( $row["groupkn"] )
? "Unknown"
: $row["groupkn"];
$company_members .= '<li><a href="#">
<div class="image"><img src="pathhere/' . $solopic . '"></div>
<p class="datatitle2">' . $soloen . '</p>
<p class="data-info2">' . $solokn . '</p>
</a></li>';
$company_members2 .= '<li><a href="#">
<div class="image"><img src="pathhere' . $grouppic . '"></div>
<p class="datatitle2">' . $groupen . '</p>
<p class="data-info2">' . $groupkn . '</p>
</a></li>';
$listofmembers = $company_members . $company_members2;
} // End of while statement
}else{
echo "No people under $companyen";
} // End of If statement
To give a (hopefully) better visual.
- Bob (individuals)
- Lisa (individuals)
- Mania (groups)
My testing before seeking help gave this result:
- Bob (individuals)
- Lisa (individuals)
- Mania (groups)
- Mania (groups)
That was because I put the output of individuals and groups in their own separate value. Then I echoed $var1 . $var2 which I felt would go wrong.
UPDATE: If nobody can figure it out, I’ll just redo my database and tables. Thank you all you tried to help.
The issue is most likely coming from your SQL code. Specifically, you appear to have some denormalization, with
companyIdin bothindividualandgroup, andindividualhaving agroupIdforeign key.Given this relationship, you most probably want to write your statement along these lines:
Other minor notes:
From what I’ve heard, best practice usually gives tables singular names, not plural ones.
What’s up with the
....Enand...Kocolumns? It seems to me like you may be doing some internationalization (English and Korean?) – if so, you’re going to want to extract those columns out of the original tables, and start translation tables. This will aid you greatly if you need to add support for additional languages later.EDIT:
Although there’s multiple ways to handle translation tables, you probably want one translation table per table that needs translated data. You’ll also probably want a standard language table for reference:
Taking
recordLabelas an example:1) Create your translated table, with all columns that will need to be translated:
2) Remove all columns that were translated (
companyEn,companyKo)3) (optional) code up a view so you have an easy reference. There are two flavours:
-> simple join for language
-> join for language, with default to english (or some other language)