简体   繁体   English

PHP-从MySQL表获取大型数组的有限块

[英]PHP - Getting limited chunks of a large array from MySQL table

Let's say you've got a table like this: 假设您有一个这样的表:

ID  Cat1  Cat2
1    a     red
2    b     red
3    c     blue
4    d     blue
5    e     blue
6    f     green
etc  etc   etc

The goal is to display the ID and Cat1 (unique pairs), split into groups according to Cat2. 目标是显示ID和Cat1(唯一对),并根据Cat2分为几组。 The easy way out is to run a separate MySQL query for each Cat2, but with a lot of different Cat2 values that produces an order of magnitude more queries to display one page to the viewer than just grabbing the whole lot of data in one query and slicing it apart with PHP. 一种简单的方法是为每个Cat2运行一个单独的MySQL查询,但是使用许多不同的Cat2值,生成的查询要多一个数量级,以向查看器显示一页,而不仅仅是在一个查询中获取全部数据,用PHP分割它。

I've got my formatting finished. 我已经完成格式化。 But I'm having a hard time figuring out how to limit my row output based on the third column value in the array $value[2] taken from $value = mysql_fetch_row($result) after using 但是我很难弄清楚如何根据使用后从$value = mysql_fetch_row($result)获取的数组$value[2]的第三列值来限制行输出
$sql = "SELECT ID,Cat1,Cat2 FROM MyTable ORDER BY Cat2,ID"

In plain English (clearly NOT real code) it would look like: 用普通的英语(显然不是真实的代码),它看起来像:

$result = mysql_query($sql);

IF ($value[2] from $value = mysql_fetch_row($result) = "red")
    {
        echo "Red Group";
        WHILE ()
    {
        echo $value[0] and $value[1] for all the rows where Cat2 = red
    }
    }

IF ($value[2] from $value = mysql_fetch_row($result) = "blue")
    {
        echo "Blue Group";
        WHILE ()
     {
        echo $value[0] and $value[1] for all the rows where Cat2 = blue
     }
    }
etc

Does all that make sense? 所有这些有意义吗? How do you grab those groups of data out of the $result array? 您如何从$result数组中获取这些数据组?

Apparently is does NOT all make sense! 显然是没有道理的! :-) Here's an example of desired output: :-)这是所需输出的示例:

RED GROUP
1  a
2  b

BLUE GROUP
3  c
4  d
5  e

GREEN GROUP
6  f

etc

Thank you! 谢谢!

I don't know if I understand you correctly, but would this solve your problem? 我不知道我是否正确理解您的身份,但这可以解决您的问题吗?

The basic idea is that you loop through all rows and always save the last value of Cat2. 基本思想是,您遍历所有行并始终保存Cat2的最后一个值。 Whenever you detect that Cat2 changes, you can insert the "header" for a new Cat2. 每当您检测到Cat2发生更改时,就可以为新的Cat2插入“标题”。 Because you sort your results according to Cat2 first, this will create the output you want. 因为您首先根据Cat2对结果进行排序,所以这将创建所需的输出。

$query = "SELECT ID,Cat1,Cat2 FROM MyTable ORDER BY Cat2,ID"
$result = mysql_query($query);
$last = "";
while ($line = mysql_fetch_row($result)) {
  if ($line[2] != $last) {
    echo $line[2] . ' Group';
    $last = $line[2];
  }
  echo $line[0] . ' ' . $line[1];
}

What you want to do is just limit the rows that your database will return using SQL's WHERE statement. 您要做的只是使用SQL的 WHERE语句限制数据库将返回的行。 Much easier: 容易得多:

$sql = "SELECT ID,Cat1,Cat2 FROM MyTable ORDER BY Cat2,ID";
$result = mysql_query($sql, $link) or die('Error');

$last_group = '';

echo '<table>';

while (list($id, $cat1, $cat2) = mysql_fetch_row($result))
{
    if ($cat2 != $last_group)
    {
        echo '<tr><td colspan="2">'.$cat2.' group</td></tr>';
    }

    echo '<tr><td>'.$id.'</td><td>'.$cat1.'</td></tr>';

    $last_group = $cat2;
}

echo '</table>';

I'll say, would probably do it 我会说,可能会做到

$result = array();
foreach($data as $row){
 if(isset($result[$row['Cat2']])){
   $result[$row['Cat2']][] = $row;
 } else {
   $result[$row['Cat2']] = array( $row );
 }
}

it would give an array grouped by Cat2 after it's just double looping to that array to get the output desired. 它会在两次循环到该数组以得到所需的输出之后,提供按Cat2分组的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM