简体   繁体   中英

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. 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.

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
$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?

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. Whenever you detect that Cat2 changes, you can insert the "header" for a new Cat2. Because you sort your results according to Cat2 first, this will create the output you want.

$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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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