For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > January 2007 > Re: [PHP-DB] Filter array results... no copies









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PHP-DB] Filter array results... no copies
Chris

2007-01-12, 3:58 am

Matthew Ferry wrote:
> Hello everyone....
>
> I'm back working on the website again... I'm having lots of fun.
> I have a sql query that looks at one field in a database. (result2 query)
> Then i have mysql_fetch_array statement.


For future reference, if you only have a problem with one particular
part of your page, please only post that part. The rest is unnecessary
in this case.

You can do this in two ways:
- remove the duplicates through changing the query

change:

SELECT adr_one_region FROM egw_addressbook WHERE cat_id='8' ORDER BY
adr_one_region

to

SELECT adr_one_region FROM egw_addressbook WHERE cat_id='8' GROUP BY
adr_one_region ORDER BY adr_one_region


- remove the duplicates in php

$prev_region = '';

while ($area = mysql_fetch_assoc($result2)) {
if ($prev_region == $area['adr_one_region']) {
continue;
}
echo "<a
href='index.php?area=$area[adr_one_region]'>$area[adr_one_region]</a>\n";
echo " - ";
}



--
Postgresql & php tutorials
http://www.designmagick.com/
Chris

2007-01-12, 3:58 am

Chris wrote:
> Matthew Ferry wrote:
>
> For future reference, if you only have a problem with one particular
> part of your page, please only post that part. The rest is unnecessary
> in this case.
>
> You can do this in two ways:
> - remove the duplicates through changing the query
>
> change:
>
> SELECT adr_one_region FROM egw_addressbook WHERE cat_id='8' ORDER BY
> adr_one_region
>
> to
>
> SELECT adr_one_region FROM egw_addressbook WHERE cat_id='8' GROUP BY
> adr_one_region ORDER BY adr_one_region
>
>
> - remove the duplicates in php
>
> $prev_region = '';
>
> while ($area = mysql_fetch_assoc($result2)) {
> if ($prev_region == $area['adr_one_region']) {
> continue;
> }
> echo "<a
> href='index.php?area=$area[adr_one_region]'>$area[adr_one_region]</a>\n";
> echo " - ";
> }


Oops that should be:


$prev_region = '';

while ($area = mysql_fetch_assoc($result2)) {
if ($prev_region == $area['adr_one_region']) {
continue;
}
echo "<a
href='index.php?area=$area[adr_one_region]'>$area[adr_one_region]</a>\n";
echo " - ";
$prev_region = $area['adr_one_region'];
}

(I wasn't resetting 'prev_region' at the end of that loop).

--
Postgresql & php tutorials
http://www.designmagick.com/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com