[英]Group results by headings from other table
我有两个表,一个包含页面/标题号和标题文本,另一个包含要在每个标题下打印的值:
标题:
page | heading | text
1 | 1 | "heading 1"
1 | 2 | "heading 2"
内容:
page | heading | contents
1 | 1 | "some stuff"
1 | 1 | "some more stuff"
1 | 2 | "some other stuff"
我希望第1页的结果是:
heading 1
some stuff
some more stuff
heading 2
some other stuff
在需要的地方增加HTML。
使用带有mysql_fetch_array()
两个while循环不起作用,因为您必须先获取,然后才能检查它是否在正确的标题下。 这意味着我错过了每个下一个标题的第一个条目。
$query = "SELECT text,heading FROM headings WHERE page=".$page." ORDER BY heading ASC";
$headings = mysql_query($query) or die(mysql_error());
$query = "SELECT filename,heading FROM pictures WHERE page=".$page." ORDER BY heading ASC";
$pictures = mysql_query($query) or die(mysql_error());
while($heading = mysql_fetch_array($headings)){
echo $heading[0];
while($image = mysql_fetch_array($pictures)){ # This skips entries because I have to check the contents themselves
if($image[1] != $heading[1]){
break;
}
echo $image[0];
}
}
没有为每个标题运行多个查询,有没有办法做到这一点? (页面上可能有很多)
在使用while循环迭代之前,只需将所有结果提取到2个数组中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.