array_unique not always unique
| From: | kris at grinz dot com | Date: | Fri, 15 Dec 2000 21:42:22 +0000 |
| Subject: | array_unique not always unique | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-30590@lists.php.net to get a copy of this message | ||
Hi, I have a function I wrote recently which builds a pulldown menu.
Sometimes it builds a pulldown showing only unique values (which
is what I want), and sometimes it builds a pulldown with duplicate
values listed. Here is a sample data list (hand written based on sql
data) and which pulldowns are rendered properly. Also attached is
the script. Can someone tell me how to make this work all the
time? Currently, it only works about half the time. Its probably
something simple but not much docs on this that I could find. I
found one thing about this at
http://www.php.net/manual/function.array-unique.php
by
jllamas@bal.com.mx but I dont understand it enough to use the
info to solve my problem. Thanks, Kris (everything u need is
below)
data map (comma seperated "simulated sql output"):
type=price data =$100.00, $200.00, $100.00, $100.00
this renders properly with my function, showing unique values only.
type=size data=19,19,19,19
this renders improperly, showing 19 twice in the pulldown.
type=bottommat data=blue,orange,red,blue
this renders properly, showing unique values only.
type=topmat data=green,green,red,red
this renders improperly, showing each unique twice.
type=frametype data=cherry,cherry,black,black
this renders improperly, showing each unique twice.
// BEGIN CODE
$psqlhost="what.ever.com"; $psqldb="whatever";
function GetPullDown($type){
global $id, $name, $descript, $price, $size, $image,
$bottommat, $topmat, $frametype, $startid, $psqldb, $psqlhost,
$progname, $itemcheck;
$call="select $type from products";
$db_connect=pg_connect("host=$psqlhost dbname=$psqldb");
$result=pg_exec($db_connect, "$call");
$rows=pg_numrows($result);
echo"<select name=\"$type\" size=1>";
$x=0;
$sortmp=array();
while($x<$rows){
$arra=pg_fetch_array($result, $x);
$sortmp[$x]=$arra[0];
$x++;
}
$final=array_unique($sortmp);
$finalcount=count($final);
$y=0;
while($y<$finalcount){
if($final[$y]==""){ echo" "; }
else{
if($final[$y]==${$type}){
echo"<option selected>$final[$y]\n";
}
else{ echo"<option>$final[$y]\n"; }
}
$y++;
}
echo"<option>Other\n";
echo"</select>";
}
echo"<table><tr><td
align=\"right\">Price:</td><td>";
GetPullDown(price);
/* pricetag used if Other is selected on pulldown */
echo"<input type=\"text\" name=\"pricetag\" size=\"8\">
</td></tr></table>";
// END CODE