The problem is you're trying to use a one at a time operation as a global operation. fetch() returns one row at a time, so you can't parse out looking for a maximum value for one row.
Way I see it, you have two choices:
- Run two db queries using the same criteria for both, one looking for a max value, and one returning the full recordset
- advantage - less php processing
- disadvantage - second trip to the database
- Place the result set into an associative array (using fetchAll() for example), then loop through the array twice, once to find max value of column, once to display
- advantage - single trip to the database, no chance of difference in result set.
- disadvantage - depending on size of returned array, multiple loops through the arrays can be expensive and slow.
note: For option two, you can use usort as well, but you'll want to do that with a different array as it reorders the entire array and that may be a different order than you actually want - may also be expensive depending on size of result set as well.