Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在循环中按数据分组显示_Php_Mysql_Foreach - Fatal编程技术网

Php 如何在循环中按数据分组显示

Php 如何在循环中按数据分组显示,php,mysql,foreach,Php,Mysql,Foreach,MySQL表:图像 id | Rating | 1 | 3.0 | 2 | 3.2 | 3 | 4.7 | 4 | 2.4 | 5 | 2.4 | 6 | 4.3 | 7 | 2.4 | 8 | 3.2 | 我基本上是试图查询上面的表,从这两个字段id和Rating中获取数据,以便生成一个“每月图像”页面。 我可以这样查询数据库: $rows = @mysql_rows('SELECT ID FROM Image WHERE L

MySQL表:图像

id | Rating |
 1 | 3.0    |
 2 | 3.2    |
 3 | 4.7    |
 4 | 2.4    |
 5 | 2.4    |
 6 | 4.3    |
 7 | 2.4    |
 8 | 3.2    |
我基本上是试图查询上面的表,从这两个字段id和Rating中获取数据,以便生成一个“每月图像”页面。 我可以这样查询数据库:

 $rows = @mysql_rows('SELECT ID FROM Image WHERE Live = 1 AND '.$whereDateLive.' ORDER BY Rating Desc LIMIT 15');
这给了我所有图像的ID。然后我使用foreach循环从最高级别开始按降序构建图像列表,如下所示-

foreach($rows as $row)
    {
    $img = new Image($row['ID'],true);
$content .= '<img src="/images/'.$img.'.jpg">';
}
这给了我我需要的分组数据,然而,foreach循环只显示组中第一个组的1个图像,因此显然不是正确的使用方法。我想知道的是有一个PHP函数,它将按数据的分组方式显示数据?或者我的代码有问题。多谢各位

期望的结果,我正在努力的目标,将是如下要求-

Image of the Month Page Title
------------------------------
First Place
[imgid 3]
-----------
Second Place
[imgid 6]
-----------
Third Place
[imgid 2] [imgid 8]
----------
Best of the rest
(showing any image with a rating > 2.4)

我刚刚检查了这个,它没有任何错误,但有些东西告诉我,内部连接中的顺序和限制看起来不是很好。但是你可以尝试一下,它有5个限制来保持简洁。还要注意,您必须相应地调整PHP变量

SELECT aa.id, aa.rating
FROM Image AS aa
INNER JOIN (
    SELECT rating
    FROM Image
    GROUP BY rating
    ORDER BY rating DESC
    LIMIT 15
) AS _aa
ON aa.rating = _aa.rating
ORDER BY aa.rating DESC
LIMIT 15

从表中选择ID和额定值。按分级排序,以便首先获得最佳分级的图像

$rows = @mysql_rows
  ('select id, rating from image where live = 1 and '.$whereDateLive.' order by rating');

然后在PHP中有一个循环,在其中考虑评级。在伪代码中:

$rating = -1;
$position = 0;

foreach($rows as $row)
{
  $img = new Image($row['id'], true);
  if(($row['rating'] = $rating) or $position > 3 then
  {
    <show the image next to the one before>
  }
  else
  {
    ++$position;
    $rating = $row['rating'];
    <show the title "n.th place" according to $position>
    <show the image>
  }
}

现场评级的数据类型是什么?嗨,是数据类型“double”,我不明白你在问什么。您有由id标识的不同图像。每个图像都有一个等级。你可以按等级排序图像,你可以限制你的结果,例如,只获得15个最佳等级。但是这一切与分组和月份有什么关系呢?顺便说一句:double是一种几乎永远不会使用的数据类型,因为它是一种近似类型,可能会导致0.1+0.1+0.1 0.3这样的异常。您将始终使用像decimal这样的精确类型。只有在范围不够的情况下,你才会使用double。我们大多数人一生中都不会遇到十进制数字范围不够的情况。请给出所需的结果好吗?Kostas Mitsrakis是对的,gusseing你想要所有排名前15的ID,不管它们有多少?谢谢你。查询可以工作,但当我将WhereDateLive子句放入其中时,它就不工作了,而这在上一个查询中是有效的$rows=@mysql_values'SELECT aa.ID,aa.Rating FROM Image AS aa internal JOIN SELECT Rating FROM Image GROUP BY Rating ORDER BY Rating DESC LIMIT 15 AS _aaon aa.Rating=_aa.Rating WHERE'$whereDateLive.'ORDER BY aa.Rating DESC LIMIT 15';$whereDateLive基本上只是一个过滤器选项,所以类似于:code DateLive BETWEEN’。mktime0,0,0,日期'm'-1,1,日期'Y'和'。mktime0,0,0,date'm',1,date'Y';嗯,再次尝试添加引号,复制粘贴此:'。mktime0,0,0,日期'm'-1,1,日期'Y'和'。mktime0,0,0,date'm',1,date'Y'您是否试图在此处构建密集的排名查询,即显示排名前15位的所有记录,无论这些记录是15条还是数百条?然后你必须去掉最后一行的限制15。按原样,您可以删除整个子查询,但仍然会得到相同的结果,即15条排名靠前的记录,而不是n条排名靠前的15条记录。从您的个人资料判断,我相信您是对的:。我会努力找到更好的解决方案。至于只显示评级>2.4的图像,你可以在where子句中添加这个:当然,评级>2.4。Thorsten,这是一种享受,没有我想象的那么复杂。非常感谢你的帮助。
$rating = -1;
$position = 0;

foreach($rows as $row)
{
  $img = new Image($row['id'], true);
  if(($row['rating'] = $rating) or $position > 3 then
  {
    <show the image next to the one before>
  }
  else
  {
    ++$position;
    $rating = $row['rating'];
    <show the title "n.th place" according to $position>
    <show the image>
  }
}