我有三个表(个人,群组和记录标签),并希望使用它们之间的信息来制作记录标签中的人员列表。示例:它应该找到#的companyID的个人和组并列出它们。如何在不重复结果的情况下正确创建?
// 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
给予(希望)更好的视觉效果。
我在寻求帮助之前的测试给出了这个结果:
那是因为我将个人和群体的输出放在他们自己的单独价值中。然后我回应$var1 . $var2
我认为会出错。
更新:如果没有人能搞清楚,我只会重做我的数据库和表格。谢谢你们所有的帮助。
答案 0 :(得分:1)
问题很可能来自您的SQL代码。具体来说,您似乎有一些非规范化,companyId
和 individual
都有group
,individual
有groupId
外国人键。
鉴于这种关系,你很可能希望按照这些方式写下你的陈述:
SELECT companyEn, companyKo, companyType, established, companyFounder,
information, companyLocation, companyPage,
soloPic, soloEn, soloKn,
groupPic, groupEn, groupKn
FROM recordLabels
JOIN groups
ON groups.companyId = recordLabels.companyId
JOIN individuals
ON individuals.groupId = groups.groupId
WHERE recordLabels.companyId = $company
其他小调:
从我所听到的情况来看,最佳做法通常会给出表格单数名称,而不是复数名称
....En
和...Ko
列的内容是什么?在我看来,你可能正在做一些国际化(英语和韩语?) - 如果是这样,你将要从原始表中提取这些列,并启动转换表。如果您以后需要添加对其他语言的支持,这将有助于您极大。
虽然有多种方法可以处理转换表,但您可能希望每个表需要一个需要翻译数据的转换表。您可能还需要一个标准语言表供参考:
language
============
id -- autoincrement - or possibly just use code
code -- ISO 3-letter code, unique
shortCode -- ISO 2-letter code, unique (I think?)
name -- ISO standard name
以recordLabel
为例:
1)创建您的翻译表,其中包含需要翻译的所有列:
recordLabelTranslation
=====================
companyId -- fk to recordLabel.companyId, or whatever the primary key of that table is
languageId -- fk to language.id
company -- whatever 'companyEn' and 'companyKo' was (company name? why translate?)
2)删除所有已翻译的列(companyEn
,companyKo
)
3)(可选)对视图进行编码,以便您轻松参考。有两种口味:
- &GT;简单的语言连接
CREATE VIEW Record_Label_Language as
SELECT recordLabel.companyId, recordLabel.information, -- all current columns...
recordLabelTranslation.languageId, recordLabelTranslation.company
FROM recordLabel
JOIN recordLabelTranslation
ON recordLabelTranslation.companyId = recordLabel.companyid
- &GT;加入语言,默认为英语(或其他语言)
CREATE VIEW Record_Label_Language_Default as
SELECT recordLabel.companyId, recordLabel.information, -- all current columns...
COALESCE(recordLabelTranslation.languageId, language.languageId),
COALESCE(recordLabelTranslation.company, dflt.company)
FROM recordLabel
JOIN recordLabelTranslation as dflt
ON dflt.companyId = recordLabel.companyid
AND dflt.languageId = [englishLanguageId]
CROSS JOIN language
LEFT JOIN recordLabelTranslation
ON recordLabelTranslation.companyId = recordLabel.companyId
AND recordLabelTranslation.languageId = language.id