Re: sql, grouping problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



mayo wrote:

cold fusion allows you to group output (see below)

  Select c.classId, c.classTexts, c.classDescription,
  cc.classCodeSection, cc.classDate, cc.classTime,
  cc.classLocation, cc.classInstructor
  FROM CLASSES c, CLASSCODES cc
  WHERE c.classId = cc.classId
  AND ...
  ORDER BY c.classId, ...

<cfoutput query="myQuery" group="classId">
#classTitle#
#classDescription#<br>...
<cfoutput>
#classCodeSection#<br>
#classDate#<br>
#classTime#<br>


  </cfoutput>
  </cfoutput>

I can't figure out how to do this in php.

You just have to "remember" the value of the classID as you loop through the results, and only show the "header" row when the classID changes.


//Empty classID
$old_classID = '';

//Loop through results
while($row = mysql_fetch_assoc($result))
{
  //show title and description when
  //classID changes
  if($row['classID'] != $old_classID)
  {
    echo "<tr><td colspan=\"3\">{$row['title']}</td></tr>";
    echo "<tr><td colspan=\"3\">{$row['description']}</td></tr>";
    $old_classID = $row['classID'];
  }

  //show rest of data
  echo "<tr><td>{$row['code']}</td>";
  echo "<td>{$row['section']}</td>";
  echo "<td>{$row['location']}</td></tr>";
}

The logic is that the title and description rows are only shown when classID changes in the result set. I showed it using MySQL functions, but that can apply to any database/abstraction layer you've got running.

Hope that helps.

--
---John Holmes...

Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/

php|architect: The Magazine for PHP Professionals – www.phparch.com

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux