To view the terms under which this email is distributed, please go to 
http://disclaimer.leedsmet.ac.uk/email.htm



On 22 November 2004 17:33, Chris Lott wrote:

> 
> +---------+----------+----------+-----------+-------+
> > first   | last     | relation | city      | state |
> +---------+----------+----------+-----------+-------+
> > Chris   | Beks     | business | Fairbanks | AK    |
> > Robert  | Hannon   | friend   | Fairbanks | AK    |
> > Cindy   | Lott     | family   | Fresno    | CA    |
> > Derryl  | Hartz    | business | Seattle   | WA    |
> > Kirsten | O'Malley | friend   | Seattle   | WA    |
> +---------+----------+----------+-----------+-------+
> 
> I want an output routine that does this:
> 
> CITY
>   person
>   person
> 
> CITY
>   person
> 
> CITY
>   person
> 
> There has to be a more elegant way than making a new query for each
> city-- which is what the query I mentioned above fixes... but is the
> kind of code I have put, with its ugly indexes and counters really the
> best way? This is another area where books always stop at the
> simplest cases... 
> 
> $currentcity = '';
> $counter = 1;
> 
> while ($thisrow = mysql_fetch_array($result))
>        {
>        if ($currentcity <> $thisrow['city'])
>                {
>                if ($counter > 1)
>                        {
>                        echo '</blockquote>';
>                        }
>                echo '<h1>' . $thisrow['city'] . '</h1>';
>                echo '<blockquote>';
>                $currentcity = $thisrow['city'];
>                }
>        echo $thisrow['first'] . ' ' . $thisrow['last'] . '<br />';   
>        $counter++; }

Yep, that's about the size of it.  Looks a lot like many of my output
routines.  I'd start $counter at 0, though, so you can use just if($counter)
as your test; and I'll often tend to use a for loop rather than a while just
to group everything related to loop control in one place:

    for ($currentcity='', $counter=0; $thisrow = mysql_fetch_array($result);
         $counter++)

Or you can do the increment as part of the test:

    if ($counter++) echo '</blockquote>';

It's pretty much down to personal taste whether you use any of these
refinements, but your basic logic is spot on.

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

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

Reply via email to