Duchy of the Black Rose: Local SCA Group Search
$conn = pg_connect("dbname=sca host=localhost");
$qry = "";
$local_group_id = "";
if (!$conn) {
echo "An error occured during connection to db.
\n";
} else if((integer) $_REQUEST['zip']>0) {
echo "Searching for ".htmlentities($_REQUEST['zip'])."
\n";
$qry = "SELECT * FROM zip INNER JOIN local_group USING (local_group_id) where zip = ".$_REQUEST['zip'].";";
$result = pg_query($conn, $qry);
if (!$result) {
echo "An error occured during select from db.
\n";
echo "$qry
\n";
} else {
if (pg_numrows($result)>0) {
$arr = pg_fetch_array($result,0,PGSQL_ASSOC);
$local_group_id = $arr['local_group_id'];
$loc = htmlentities($arr['loc']);
$desc = htmlentities($arr['description']);
$url = htmlentities($arr['url']);
echo "".htmlentities($arr['name'])."
\n";
if ($loc>'')
echo "Location: ".$loc."
\n";
if ($desc>'')
echo "".nl2br($desc)."
\n";
if ($url>'')
echo "\n";
$qry = "SELECT zip from zip where local_group_id = '".$local_group_id."' order by zip;";
$result = pg_query($conn, $qry);
if (pg_numrows($result)>0) {
echo "\n";
$cols = pg_numrows($result)>=9?9:pg_numrows($result);
$rows = (pg_numrows($result)/$cols)>1?ceil(pg_numrows($result)/$cols):1;
while (pg_numrows($result)-($rows*($cols-1))<1)
$cols-=1;
echo "
Zip Codes:\n";
echo "
\n";
$i=0;
for($c=1;$c<=$cols;$c++) {
echo "\n";
$last=$c<$cols?($rows*$c):pg_numrows($result);
for(;$i<$last;$i++) {
$arr = pg_fetch_array($result,$i,PGSQL_ASSOC);
echo $arr['zip']." \n";
}
echo " | \n";
}
echo "
\n";
echo "
\n";
}
} else {
echo "Not found...try again.
\n";
}
}
}
?>