Filter array results... no copies

Filter array results... no copies

am 12.01.2007 06:35:31 von Matthew Ferry

------=_NextPart_000_0477_01C735E1.90849430
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

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.

I then use this array to print links on the page.
Works fine except I don't want duplicate links.

It creates links for the states. If we have four customers from =
Pennsylvania. This current process gives me four different "PA" links =
on my page. =20

I need a statement that says...if its already printed...don't print =
it...

Thanks...

Here is my code:






$db =3D mysql_connect("HOST", "USERNAME", "PASSWORD");

mysql_select_db("DATABASE",$db);


if ($_GET[area]=="") {

$master =3D mysql_query("SELECT * FROM egw_addressbook

WHERE cat_id=3D'8' ORDER BY org_name", $db);



} else {

$master =3D mysql_query("SELECT * FROM egw_addressbook

WHERE cat_id=3D'8' and adr_one_region=3D'$_GET[area]' ORDER BY =
org_name", $db);

}



$result2 =3D mysql_query("SELECT adr_one_region FROM egw_addressbook

WHERE cat_id=3D'8' ORDER BY adr_one_region", $db);



if ($area =3D mysql_fetch_array($result2)) {

echo "Sort by State: ";



do {

echo " href=3D'index.php?area=3D$area[adr_one_region]'>$area[adr_on e_region]=
\n";

echo " - ";

} while ($area =3D mysql_fetch_array($result2));

echo "\n";

} else {

echo "ERROR";

}



if ($myrow =3D mysql_fetch_array($master)) {

echo "

\n";

echo "\n";

echo "\n";

echo "
\n";

echo "
\n";



do {

printf(" =
\n", =
$myrow["org_name"], $myrow["fn"]);=20

printf("\n", =
$myrow["adr_one_street"], $myrow["tel_work"]);

printf("\n", =
$myrow["adr_one_locality"], $myrow["adr_one_region"], =
$myrow["adr_one_postalcode"]);



} while ($myrow =3D mysql_fetch_array($master));

echo "
%s%s
%s%s
%s, %s =
%s
\n";

echo "
\n";

} else {

echo "Sorry, no records were found!";=20

}

?>








------=_NextPart_000_0477_01C735E1.90849430--

Re: Filter array results... no copies

am 12.01.2007 07:13:48 von robleyd

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.
>
> I then use this array to print links on the page.
> Works fine except I don't want duplicate links.
>
> It creates links for the states. If we have four customers from
> Pennsylvania. This current process gives me four different "PA" links on
> my page.
>
> I need a statement that says...if its already printed...don't print it...
>
> Thanks...
>
> Here is my code:
>
>
>
>
>
> >
> $db = mysql_connect("HOST", "USERNAME", "PASSWORD");
>
> mysql_select_db("DATABASE",$db);
>
>
> if ($_GET[area]=="") {
>
> $master = mysql_query("SELECT * FROM egw_addressbook
>
> WHERE cat_id='8' ORDER BY org_name", $db);
>
>
>
> } else {
>
> $master = mysql_query("SELECT * FROM egw_addressbook
>
> WHERE cat_id='8' and adr_one_region='$_GET[area]' ORDER BY org_name",
> $db);
>
> }
>
>
>
> $result2 = mysql_query("SELECT adr_one_region FROM egw_addressbook
>
> WHERE cat_id='8' ORDER BY adr_one_region", $db);
>
>
>
> if ($area = mysql_fetch_array($result2)) {
>
> echo "Sort by State: ";
>
>
>
> do {
>
> echo " > href='index.php?area=$area[adr_one_region]'>$area[adr_one_re gion]\n";
>
> echo " - ";
>
> } while ($area = mysql_fetch_array($result2));
>
> echo "\n";
>
> } else {
>
> echo "ERROR";
>
> }
>
>
>
> if ($myrow = mysql_fetch_array($master)) {
>
> echo "

\n";
>
> echo "\n";
>
> echo "\n";
>
> echo "
\n";
>
> echo "
\n";
>
>
>
> do {
>
> printf("
> \n",
> $myrow["org_name"], $myrow["fn"]);
>
> printf("\n",
> $myrow["adr_one_street"], $myrow["tel_work"]);
>
> printf("\n",
> $myrow["adr_one_locality"], $myrow["adr_one_region"],
> $myrow["adr_one_postalcode"]);
>
>
>
> } while ($myrow = mysql_fetch_array($master));
>
> echo "
%s%s
%s%s
%s, %s
> %s
\n";
>
> echo "
\n";
>
> } else {
>
> echo "Sorry, no records were found!";
>
> }
>
> ?>
>
>
>
>

Hm, perhaps if you order by state first, then do something like (pseudocode
only):

set currentstate = ''; # Initialise variable to empty
while (looping through result set)
if currentstate != state_from_dataset
echo $state
endif
display other stuff
do any other stuff in loop
set currentstate = state_from_dataset
endwhile



Cheers
--
David Robley

Auntie Em: Hate you, hate Kansas, taking the dog. -Dorothy
Today is Boomtime, the 12nd day of Chaos in the YOLD 3173.

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

Re: Filter array results... no copies

am 12.01.2007 07:15:22 von dmagick

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 " href='index.php?area=$area[adr_one_region]'>$area[adr_one_re gion]\n";
echo " - ";
}



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

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

Re: Filter array results... no copies

am 12.01.2007 07:16:20 von Trevor Gryffyn

If all you want is a unique list of "adr_one_region" codes, then use the DISTINCT keyword in your SQL:

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


-TG

= = = Original message = = =

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.

I then use this array to print links on the page.
Works fine except I don't want duplicate links.

It creates links for the states. If we have four customers from Pennsylvania. This current process gives me four different "PA" links on my page.

I need a statement that says...if its already printed...don't print it...

Thanks...

Here is my code:






$db = mysql_connect("HOST", "USERNAME", "PASSWORD");

mysql_select_db("DATABASE",$db);


if ($_GET[area]=="")

$master = mysql_query("SELECT * FROM egw_addressbook

WHERE cat_id='8' ORDER BY org_name", $db);



else

$master = mysql_query("SELECT * FROM egw_addressbook

WHERE cat_id='8' and adr_one_region='$_GET[area]' ORDER BY org_name", $db);





$result2 = mysql_query("SELECT adr_one_region FROM egw_addressbook

WHERE cat_id='8' ORDER BY adr_one_region", $db);



if ($area = mysql_fetch_array($result2))

echo "Sort by State: ";



do

echo "\n";

echo " - ";

while ($area = mysql_fetch_array($result2));

echo "\n";

else

echo "ERROR";





if ($myrow = mysql_fetch_array($master))

echo "

\n";

echo "\n";

echo "\n";

echo "
\n";

echo "
\n";



do

printf("\n", $myrow["org_name"], $myrow["fn"]);

printf("\n", $myrow["adr_one_street"], $myrow["tel_work"]);

printf("\n", $myrow["adr_one_locality"], $myrow["adr_one_region"], $myrow["adr_one_postalcode"]);



while ($myrow = mysql_fetch_array($master));

echo "
%s%s
%s%s
%s, %s %s
\n";

echo "
\n";

else

echo "Sorry, no records were found!";



?>






___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

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

Re: Filter array results... no copies

am 12.01.2007 07:16:31 von dmagick

Chris wrote:
> 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 " > href='index.php?area=$area[adr_one_region]'>$area[adr_one_re gion]\n";
> echo " - ";
> }

Oops that should be:


$prev_region = '';

while ($area = mysql_fetch_assoc($result2)) {
if ($prev_region == $area['adr_one_region']) {
continue;
}
echo " href='index.php?area=$area[adr_one_region]'>$area[adr_one_re gion]\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/

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