Php 如何使这个简单的块更短更好?
我正在使用以下未经深思熟虑的代码块来检索类别及其主题Php 如何使这个简单的块更短更好?,php,mysql,Php,Mysql,我正在使用以下未经深思熟虑的代码块来检索类别及其主题 $query1 = "SELECT * from categories"; $result = mysql_query($query1); while ($out = mysql_fetch_assoc($result)){ //category print '<h2>' . $out['name'] . '</h2>'; $query2 = "SELECT * from topics w
$query1 = "SELECT * from categories";
$result = mysql_query($query1);
while ($out = mysql_fetch_assoc($result)){
//category
print '<h2>' . $out['name'] . '</h2>';
$query2 = "SELECT * from topics where fkid = $out[id]";
$result2 = mysql_query($query2);
while($top = mysql_fetch_assoc($result2)){
//topic
print $top['name'] . '<br>';
}
}
$query1=“从类别中选择*”;
$result=mysql\u查询($query1);
而($out=mysql\u fetch\u assoc($result)){
//类别
打印“.$out['name']”;
$query2=“从fkid=$out[id]的主题中选择*”;
$result2=mysql\u查询($query2);
而($top=mysql\u fetch\u assoc($result2)){
//话题
打印$top['name']。
;
}
}
以上就是关键。我知道这不是最实际的,这也是我问小组的原因
如何改进此功能,使其更加实用和简单?连接的经典案例:
SELECT * FROM categories JOIN topics ON categories.id = topic.fkid ORDER BY categories.name;
然后要打印,我们只打印更改了的标题(谢谢,Rajasur!):
连接的典型情况:
SELECT * FROM categories JOIN topics ON categories.id = topic.fkid ORDER BY categories.name;
然后要打印,我们只打印更改了的标题(谢谢,Rajasur!):
只需使用一个连接两个表的查询
$query = "SELECT categories.name AS category_name, topics.name AS topic_name
FROM categories JOIN topics ON categories.id = topics.fkid
ORDER BY categories.name ASC, topics.name ASC";
$resultSet = mysql_query($query);
$currentCategory = "";
while ($cRecord = mysql_fetch_assoc($resultSet)) {
if ($currentCategory != $cRecord['category_name']) {
$currentCategory = $cRecord['category_name'];
echo "<h2>" . $currentCategory . "</h2>";
}
echo $cRecord['topic_name'] . "<br/>";
}
mysql_close($resultSet);
$query=“选择categories.name作为category\u name,选择topics.name作为topic\u name
从类别在categories.id=topics.fkid上连接主题
按类别排序。名称ASC,主题。名称ASC”;
$resultSet=mysql\u query($query);
$currentCategory=“”;
而($cRecord=mysql\u fetch\u assoc($resultSet)){
如果($currentCategory!=$cRecord['category\u name']){
$currentCategory=$cRecord['category_name'];
回显“$currentCategory.”;
}
echo$cRecord['topic_name']。“
”;
}
mysql_close($resultSet);
只需使用一个连接两个表的查询即可
$query = "SELECT categories.name AS category_name, topics.name AS topic_name
FROM categories JOIN topics ON categories.id = topics.fkid
ORDER BY categories.name ASC, topics.name ASC";
$resultSet = mysql_query($query);
$currentCategory = "";
while ($cRecord = mysql_fetch_assoc($resultSet)) {
if ($currentCategory != $cRecord['category_name']) {
$currentCategory = $cRecord['category_name'];
echo "<h2>" . $currentCategory . "</h2>";
}
echo $cRecord['topic_name'] . "<br/>";
}
mysql_close($resultSet);
$query=“选择categories.name作为category\u name,选择topics.name作为topic\u name
从类别在categories.id=topics.fkid上连接主题
按类别排序。名称ASC,主题。名称ASC”;
$resultSet=mysql\u query($query);
$currentCategory=“”;
而($cRecord=mysql\u fetch\u assoc($resultSet)){
如果($currentCategory!=$cRecord['category\u name']){
$currentCategory=$cRecord['category_name'];
回显“$currentCategory.”;
}
echo$cRecord['topic_name']。“
”;
}
mysql_close($resultSet);
这将适用于SQL,但我猜OP的输出方式是先打印类别名称(作为标题),然后列出主题。像上面那样执行SQL将导致一些像原始代码片段一样打印的处理。@Rajasur:这一点很好。好的,按类别排序。命名,然后只打印第一个匹配项。让我编辑一下。这对SQL来说是可行的,但我想OP的输出方式是先打印类别名称(作为标题),然后再列出主题。像上面那样执行SQL将导致一些像原始代码片段一样打印的处理。@Rajasur:这一点很好。好的,按类别排序。命名,然后只打印第一个匹配项。让我来编辑。