R
E
S
O
U
R
C
E
S
       Home      Products & Services      Contact Us      Links


WebHatchers will design & develop your site for you.
_______________________

Website Menu Heaven: menus, buttons, etc.
_______________________

Send us your questions.
_______________________

site search by freefind
_______________________

HOME
SEO, Google, Privacy
   and Anonymity
Browser Insanity
JavaScript
Popups and Tooltips
Free Website Search
HTML Form Creator
Animation
Buttons and Menus
Counters
Captchas
Image Uploading
CSS and HTML
PHP
AJAX
XPATH
Website Poll
IM and Texting
Databases—MySQL
   or Not MySQL
Personal Status Boards
Content Management
   Systems
Article Content
   Management Systems
Website Directory
   CMS Systems
Photo Gallery CMS
Forum CMS
Blog CMS
Customer Records
   Management CMS
Address Book CMS
Private Messaging CMS
Chat Room CMS
JavaScript Charts
   and Graphs




Free Personal Status Boards (PSB™)

Free Standard Free PSB

Free PSB Pro Version

Free Social PSB

Free Social PSB Plus (with Email)

Free Business PSB

Free Business PSB Plus (with Email)

PSB demo

Social PSB demo

Business PSB demo

So what's all this PSB stuff about?

Chart comparing business status boards

PSB hosting diagram

PSB Licence Agreement



Copyright © 2002 -
MCS Investments, Inc. sitemap

PSBs, social networking, social evolution, microcommunities, personal status boards
PSBs, social networking, business personal status boards
website design, ecommerce solutions
website menus, buttons, image rotators
Ez-Architect, home design software
the magic carpet and the cement wall, children's adventure book
the squirrel valley railroad, model railroad videos, model train dvds
the deep rock railroad, model railroad videos, model train dvds

MC (Microcommunity) Search and Match — PHP

These script blocks are from the PHP script match.php

The first code block has been discussed in the Security page which we call MC (Microcommunity) Search and Match — Security. If you need to see the whole matching script in one script, use this: MC (Microcommunity) Search and Match.


<?php
//copyright (c) 2011 by MCS Investments, Inc. all rights reserved

include_once"checkid.php";
$U=$_SESSION['username'];
define('_NODIRECTACCESS', TRUE);
include_once"includes/config.php";
if (!isset($_SESSION['groupname']) || !isset($_SESSION['userid']) || !isset($_SESSION['username']) || $_SESSION
['username']<>$U || !isset($U) || $U=="" || strlen($U)<6 || !isset($_SESSION['sessionid'])){echo '<script language="javascript">alert("Please login."); window.location = "login-to-mc.php";</script>';}
?>

The following code block is about getting the distances between 2 zipcodes. There are plenty of distance calculation formulas on the Internet—we chose the simpest and most effective. The function milesapart() uses the latitudes and longitudes from the MySQL database table which correspond to the zipcodes in the table (which are in the same record). The PHP functions milesapart() and coords() are distance scripts that need to compare the zip code of the user with the zip codes of the rest of the users in the database to determine which are within a certain distance (these distances are in the $dist[] array) which has been selected by the user by him checking ONE of the checkboxes in the Willing_to_relocate section of the search form.

The zipcode database was free. Thanks, guys. It contains latitudes and longitudes and zips (and other stuff) which is enough info to enable us to calculate distances between zipcodes as long as they are in the USA and not military. We stuck all 42000+ records into a MySQL database table that is nearly 3MB in size. Yes, there are better zipcode databases that are more up to date and have the military zipcodes as well, but they cost a bit and we didn't feel like paying. To get the records into the db, click here.

The coords() function gets 2 zipcodes sent to it as parameters. Then the MySQL db is queried and the zipcodes are looked up and the longitudes and latitudes are found and stuck in the parameters for calling the milesapart() function. The class is checked, as well, and if it is MILITARY, the function returns a value of 9999 miles. This means it will be too great a distance to ever get found in searches, whose biggest comparison criteria is 1000 miles or less. People in the armed services will not be in MCs. But the deciding factor is that the free zipcode database has empty strings for longitudes and latitudes for all records of class MILITARY.

If the zipcode is nonmilitary and USA, the coords() function consults the milesapart function for the distance apart the two zipcodes are. A zipcode area can be fairly large, so the map coordinates represent the center of each area only, not precise values for the distance apart of two MC groups. If the center of an area and an adjacent area are 20 miles apart, and your neighbor lives in one area but you are in the other area, the script will see your groups as 20 miles apart, so a search for groups within 2 miles or even 10 miles will fail to find this nearby group. The vast majority of searches will not involve this situation, of course. But it's a theoretically possible scenario.

<?php

$dist=array(0,0,1000,500,200,100,50,10,2,0);

function milesapart($lat1,$long1,$lat2,$long2){
$miles = sin(deg2rad($lat1))
* sin(deg2rad($lat2))
+ cos(deg2rad($lat1))
* cos(deg2rad($lat2))
* cos(deg2rad($long1-$long2));
$miles = (rad2deg(acos($miles))) * 69.09;
return $miles;}

function coords($z1,$z2){
$res=mysql_query("SELECT latitude,longitude,class FROM zipcodes WHERE zipcode = '$z1'") or die(mysql_error());
$row = mysql_fetch_array($res);$lat1=$row['latitude'];$long1=$row['longitude'];$c1=$row['class'];
$res=mysql_query("SELECT latitude,longitude,class FROM zipcodes WHERE zipcode = '$z2'") or die(mysql_error());
$row = mysql_fetch_array($res);$lat2=$row['latitude'];$long2=$row['longitude'];$c2=$row['class'];
if($c1<>"MILITARY" && $c2<>"MILITARY" && $lat1<>0 && $long1<>0 && $lat2<>0 && $long2<>0){
$zz=milesapart($lat1,$long1,$lat2,$long2);
}else{$zz=9999;}
return $zz;}

The restorem() function loops through the $first[] array setting scores to 1 in the mc_members table where registered groups reside for each of the ID numbers stored in this array. This is how we handle searches that are of the Search Your Results type. The mc_members table has a score field that is used for 3 purposes. The first is to keep track of the results of a search, and score=score+1 is what we do with the db table record whose data satisfies a search criteria. Since a record (representing all the data on one group) may satisfy more than one criteria, the score can be as high as 10 (since searches are confined to 10 criteria), if all criteria are satisfied in a 10-criteria search. Only the distance aspect of the records has a comparative method. Other criteria, like city, are pass or fail. A search for Chicago will or won't find it in any one record—it may not be in the city field of any group's record. A search for "Have screeching birds" (checked in the appropriate checkbox of the Pets section) will either find a value of 6 (representing "Have screeching birds") in the Pets field of any group record or not. Similarly, if the checkbox in the input tag named "availablechildcare" is checked (next to the sentence "There are people in your group that are available to do childcare."), it will look for all records with a value in that field greater than zero. This represents a searcher looking for groups available to do childcare.

The restorem() function gets records with the IDs stored in $first[] and gives them a score of 1 to represent that these are the only records to search in a Search Your Results type search. The 1 will be looked for in these types of searches and records without this will be ignored. At the beginning of all searches, all records' scores are zeroed. But if the script finds the user doing a Search Your Results type search, the 1 will go into each record's score field that was successfully found as a search result in the last search.

Just before each Search Your Results type search is completed, the script will find all records with scores>0 and subtract 1 from them. What this does is keeps the LAST search result from contaminating THIS search. Even though the group records searched are ONLY the ones found in the LAST search result, we do not want to keep finding these records, regardless. We want to find only those who are part of the CURRENT search—the NEW search results. So we temporarily increment the scores and then decrement them after the search. This leaves the records with scores GREATER THAN 1 as the NEW search results that get displayed on the screen, since the ones that were found LAST time but not THIS time were 1 but are now 0!

How did the $first[] array get the IDs of search result records in the first place? Just before the new search begins and all records are zeroed, if it is a Search Your Results type search they chose, the script looks for all records with scores>0 and uses the array_push() function to store these records in the array. Even though the user is starting a new search, the database table still has the results from the last search in the form of nonzero scores field values, so these record IDs are snuck into the array just prior to zeroing all records' score field values. To find WHERE, in this long script, something happens such as filling an array called $first, simply search for $first and look for array_push near it, or, in general, search with Ctrl F for the relevant keyword.

function restorem(){
global $first;
for ($i=0;$i<count($first);$i++) {
$sql="UPDATE mc_members SET score=1 WHERE id='$first[$i]'";
$result=mysql_query($sql);}}

The opt() function is for data display purposes. Check this out:

$a=htmlentities(stripslashes($row['Pets']), ENT_QUOTES);$e=$Pets;$x=opt($a,$e);
echo "<td colspan='2' style='text-align:left'>Pets: ".$x."</td></tr><br>";

As you can see, the opt() function requires 2 parameters, $a and $e. The $a variable gets the value of the Pets field in the db table, while $e gets the name of the PHP array we will reference. You can see those arrays in their entirety below. So the opt() function now uses the PHP substr() function to strip the comma from the beginning and end of the $a value gotten from the db.

Why do all the table fields in the records that refer to the Preferences section of the search form have commas fore and aft? Because when the searches happen, a search for all 2 values would also find 12, 22, 32, 42, and 52 as field values, (e.g., 'Smoking not an issue' in the Smoking_Drinking options of the form is immune since it has only 5 elements, but what about the Religion options in the form, with 61 elements?). Looking for ,2 or 2, is no good since the 2 could be at the front or end of the string, and, as we showed, looking for 2 is no good either. But looking for ,2, works fine, as long as the data follows the rules.

The rules are that if a record has ,3,7,8, as its field value for example for Religion, signifying that the group has at least 1 member of the Assemblies of God, Buddhist, and Catholic religions, a search for ,3, or ,7, or ,8, would do fine, and if their field has ,2,6,11,22,48,52, searching in the field for the criterion ,2, should succeed at getting ,2, only but not ,22, or ,52, . This type of field configuration works fine. At first one would imagine that an array of numbers in string form would look like 2,6,11,22,48,52 since in the opt() function we explode it into an array and ,2,6,11,22,48,52, will mess up. But $a=substr($a,1,strlen($a)-2) takes care of the problem since searches need ,2,6,11,22,48,52, but data display needs these numbers as individual array elements.

So we explode the string (e.g., ,2,6,11,22,48,52,) after getting the fore and aft commas dumped, into the $c[] array. Since $e holds the name of the PHP array we need, $e[$c[$i]] gets the array element value where $c[$i] is the element number corresponding to the 2nd, 6th, 11th, 22nd, 48th, and 52nd array element as $i increments in the loop. $x gets the values (which are strings found in the PHP arrays below, not numbers) all concatenated together with commas and spaces where appropriate. The final substr() gets the ", " dumped off the end of $x—there are no more array values to display.

Check out these PHP arrays, below.

function opt($a,$e){
global $a,$e,$x;
$a=substr($a,1,strlen($a)-2);
$c=explode(",",$a);$x="";
for ($i=0;$i<count($c);$i++) {$x=$x.$e[$c[$i]].", ";}
return substr($x,0,-2);}

function get_record($id,$score){
global $a,$e,$x;

$Smoking_Drinking=array('No preference','Prefer nonsmokers','Smoking not an issue','Prefer nondrinkers','Drinking not an issue');

$Preferred_Housing_Type=array('No preference','Row houses','Apartments','Mobile homes','Manufactured homes','Condominium','Co-op','Hotel','Motel','Farm','Rural','City','Suburbs','Retirement community','Gated community','Pocket neighborhoods');

$Willing_to_relocate=array('No preference','Yes, anywhere','Yes, up to 1000 miles','Yes, up to 500 miles','Yes, up to 200 miles','Yes, up to 100 miles','Yes, up to 50 miles','Yes, up to 10 miles','Yes, up to 2 miles','No');

$MC_type_sought=array('No preference','With children','Without children','Children not an issue','Singles MC','Married MC','Mixed MC','Marital status not an issue');

$Open_to_be_in_mixed_race_MC=array('No preference','Prefer to be in MC of my own race','Prefer to be in MC with racial diversity','Race not an issue');

$Sexuality=array('No preference','Prefer not to answer','Open to be in mixed sexual preference MC','Prefer to be in MC of my own sexual preference','Prefer to be in MC with sexual preference diversity','Sexual preference not an issue');

$Religious_Openness=array('No preference','Open to be in mixed religion MC','Prefer to be in MC of my own religion','Prefer to be in MC with religious diversity','Religion not an issue','Prefer not to answer');

$Religion=array('No preference','Agnostic','Apostolic or New Apostolic','Assemblies of God','Atheist','Baha\'I','Baptist','Buddhist','Catholic','Christian Reform','Christian Science','Christian no denomination supplied','Church of God','Church of the Brethren','Church of the Nazarene','Churches of Christ','Congregational or United Church of Christ','Deity','Disciples of Christ','Druid','Eckankar','Episcopalian or Anglican','Ethical Culture','Evangelical or Born Again','Foursquare Gospel','Full Gospel','Fundamentalist','Hindu','Holiness or Holy','Humanist','Independent Christian Church','Jehovah\'s Witness','Jewish','Lutheran','Mennonite','Methodist or Wesleyan','Mormon or Latter Day Saints','Muslim','Native American','New Age','No religion','Nondenominational','Orthodox Eastern','Other unclassified','Pagan','Pentecostal or Charismatic','Presbyterian','Protestant no denomination supplied','Quaker','Rastafarian','Reformed or Dutch Reform','Salvation Army','Santeria','Scientologist','Secular','Seventh Day Adventist','Sikh','Spiritualist','Taoist','Unitarian or Universalist','Wiccan');

$Childrearing_Style_and_Preferences=array('No preference','Liberal or permissive','Authoritative','Authoritarian','Liberal and authoritative mix','Liberal and authoritarian mix','Authoritative and authoritarian mix');

$Pets=array('No preference','Have dogs','Have cats','Have birds','Have other pets','Have barking dogs','Have screeching birds','Tolerant of neighbors\' pet sounds','Not tolerant of neighbors\' pet sounds');

$Personal_Entertainment=array('No preference','Tolerant of neighbors\' loud music or TV','Not tolerant of neighbors\' loud music or TV');

$Housing_Interested_in_Common_Shared_Space_for=array('No preference','Cooking','Laundry','Exercise','Childcare','Eldercare','Home schooling','Meetings','Entertainment','Not interested in common space');

$Employment=array('No preference','Management, business, and financial','Professional and related','Office and administrative support','Service','Installation, maintenance, and repair','Transportation and material moving','Construction and extraction','Production','Sales and related','Farming, fishing, and forestry','Armed forces','Part-time','Temporary','Retired','Unemployed','Independently wealthy');

The get_record($id,$score) function is for displaying results. The records with results get their $id and $score sent to the function, the former so it knows which record to show and the latter so it can show the user how many search criteria were satisfied. Security measures are in place—we use htmlentities() and stripslashes() functions to make sure the data from the MySQL database table is safely displayed, even though the data was thoroughly sanitized on the way into the db. Note the style applied to the columns: 'width:143px;max-width:143px;min-width:143px;word-wrap:break-word;'. This forces decent data display even if the data contains long words wider than their <TD> container.

We've already described the opt() function's use for data display purposes. The Preferences part of record display uses this.

$result = mysql_query("SELECT * FROM mc_members WHERE id = '$id'") or die(mysql_error());
$num=mysql_num_rows($result);
if($num>0){
$row = mysql_fetch_array($result);
echo "<center><div id='t'><table border='2' id='j'>";
echo "<tr><td colspan='5' style='text-align:center;color:red'>(click link to send message) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b><span style='color:black'>Group Name:</span> <a HREF='send-message-form.php?gr=".htmlentities(stripslashes($row['groupname']), ENT_QUOTES)."'>".htmlentities(stripslashes($row['groupname']), ENT_QUOTES)."</a></b> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(fills ".$score." of your criteria)</td></tr><br>";
echo "<tr><td colspan='5' style='text-align:center'><b>Location: ".htmlentities(stripslashes($row['city']), ENT_QUOTES).", ";
echo htmlentities(stripslashes($row['state']), ENT_QUOTES)." ";
echo htmlentities(stripslashes($row['zip']), ENT_QUOTES)."</b></td></tr><br>";

echo "<tr><td style='text-align:center;width:147px;max-width:147px;min-width:147px;word-wrap:break-word;'><b>Males</b></td><td style='text-align:center;width:147px;max-width:147px;min-width:147px;word-wrap:break-word;'><b>Females</b></td><td style='width:143px;max-width:143px;min-width:143px;word-wrap:break-word;'>Childless: ".htmlentities(stripslashes($row['childless']), ENT_QUOTES)."</td><td style='width:143px;max-width:143px;min-width:143px;word-wrap:break-word;'>White: ".htmlentities(stripslashes($row['white']), ENT_QUOTES)."</td><td style='width:230px;max-width:230px;min-width:230px;word-wrap:break-word;'>Allergies: ".htmlentities(stripslashes($row['allergies']), ENT_QUOTES)."</td></tr><BR>";

echo "<tr><td>0-3 yr: ".htmlentities(stripslashes($row['males03']), ENT_QUOTES)."</td><td>0-3 years old: ".htmlentities(stripslashes($row['females03']), ENT_QUOTES)."</td><td>Empty Nesters: ".htmlentities(stripslashes($row['emptynesters']), ENT_QUOTES)."</td><td>Black: ".htmlentities(stripslashes($row['black']), ENT_QUOTES)."</td><td>Chronic: ".htmlentities(stripslashes($row['chronic']), ENT_QUOTES)."</td></tr><br>";


echo "<tr><td>4-7 years old: ".htmlentities(stripslashes($row['males47']), ENT_QUOTES)."</td><td>4-7 years old: ".htmlentities(stripslashes($row['females47']), ENT_QUOTES)."</td><td>Widows: ".htmlentities(stripslashes($row['widows']), ENT_QUOTES)."</td><td>Asian: ".htmlentities(stripslashes($row['asian']), ENT_QUOTES)."</td><td>Mental: ".htmlentities(stripslashes($row['mental']), ENT_QUOTES)."</td></tr><br>";


echo "<tr><td>8-11 years old: ".htmlentities(stripslashes($row['males811']), ENT_QUOTES)."</td><td>8-11 years old: ".htmlentities(stripslashes($row['females811']), ENT_QUOTES)."</td><td>Widowers: ".htmlentities(stripslashes($row['widowers']), ENT_QUOTES)."</td><td>Hispanic: ".htmlentities(stripslashes($row['hispanic']), ENT_QUOTES)."</td><td>Disabilities: ".htmlentities(stripslashes($row['disabilities']), ENT_QUOTES)."</td></tr><br>";


echo "<tr><td>12-14 years old: ".htmlentities(stripslashes($row['males1214']), ENT_QUOTES)."</td><td>12-14 years old: ".htmlentities(stripslashes($row['females1214']), ENT_QUOTES)."</td><td>Married: ".htmlentities(stripslashes($row['married']), ENT_QUOTES)."</td><td>Hawaiian: ".htmlentities(stripslashes($row['hawaiian']), ENT_QUOTES)."</td><td>Need Ramps: ".htmlentities(stripslashes($row['ramps']), ENT_QUOTES)."</td></tr><br>";


echo "<tr><td>15-17 years old: ".htmlentities(stripslashes($row['males1517']), ENT_QUOTES)."</td><td>15-17 years old: ".htmlentities(stripslashes($row['females1517']), ENT_QUOTES)."</td><td>Living Together: ".htmlentities(stripslashes($row['livingtogether']), ENT_QUOTES)."</td><td>Mixed: ".htmlentities(stripslashes($row['mixed']), ENT_QUOTES)."</td><td>Willing to do Eldercare: ".htmlentities(stripslashes($row['willingeldercare']), ENT_QUOTES)."</td></tr><br>";


echo "<tr><td>18-23 years old: ".htmlentities(stripslashes($row['males1823']), ENT_QUOTES)."</td><td>18-23 years old: ".htmlentities(stripslashes($row['females1823']), ENT_QUOTES)."</td><td>Separated: ".htmlentities(stripslashes($row['separated']), ENT_QUOTES)."</td><td>Criminal: ".htmlentities(stripslashes($row['criminal']), ENT_QUOTES)."</td><td>Available to do Eldercare: ".htmlentities(stripslashes($row['availableeldercare']), ENT_QUOTES)."</td></tr><br>";


echo "<tr><td>24-34 years old: ".htmlentities(stripslashes($row['males2434']), ENT_QUOTES)."</td><td>24-34 years old: ".htmlentities(stripslashes($row['females2434']), ENT_QUOTES)."</td><td>Divorced: ".htmlentities(stripslashes($row['divorced']), ENT_QUOTES)."</td><td>Home Schooled: ".htmlentities(stripslashes($row['homeschooled']), ENT_QUOTES)."</td><td>Need Eldercare: ".htmlentities(stripslashes($row['needeldercare']), ENT_QUOTES)."</td></tr><br>";


echo "<tr><td>35-49 years old: ".htmlentities(stripslashes($row['males3549']), ENT_QUOTES)."</td><td>35-49 years old: ".htmlentities(stripslashes($row['females3549']), ENT_QUOTES)."</td><td>Engaged: ".htmlentities(stripslashes($row['engaged']), ENT_QUOTES)."</td><td>Public Schools: ".htmlentities(stripslashes($row['publicschools']), ENT_QUOTES)."</td><td>Willing to do Childcare: ".htmlentities(stripslashes($row['willingchildcare']), ENT_QUOTES)."</td></tr><br>";


echo "<tr><td>50-65 years old: ".htmlentities(stripslashes($row['males5065']), ENT_QUOTES)."</td><td>50-65 years old: ".htmlentities(stripslashes($row['females5065']), ENT_QUOTES)."</td><td>Single: ".htmlentities(stripslashes($row['single']), ENT_QUOTES)."</td><td>Private Schools: ".htmlentities(stripslashes($row['privateschools']), ENT_QUOTES)."</td><td>Available to do Childcare: ".htmlentities(stripslashes($row['availablechildcare']), ENT_QUOTES)."</td></tr><br>";


echo "<tr><td>66-79 years old: ".htmlentities(stripslashes($row['males6679']), ENT_QUOTES)."</td><td>66-79 years old: ".htmlentities(stripslashes($row['females6679']), ENT_QUOTES)."</td><td>Gay Males: ".htmlentities(stripslashes($row['gaymales']), ENT_QUOTES)."</td><td>Religious Schools: ".htmlentities(stripslashes($row['religiousschools']), ENT_QUOTES)."</td><td>Need Childcare: ".htmlentities(stripslashes($row['needchildcare']), ENT_QUOTES)."</td></tr><br>";


echo "<tr><td>80+ years old: ".htmlentities(stripslashes($row['males80']), ENT_QUOTES)."</td><td>80+ years old: ".htmlentities(stripslashes($row['females80']), ENT_QUOTES)."</td><td>Lesbian: ".htmlentities(stripslashes($row['lesbian']), ENT_QUOTES)."</td><td>Special Schools: ".htmlentities(stripslashes($row['specialschools']), ENT_QUOTES)."</td><td>&nbsp;</td></tr><br>";


echo "<tr><th style='text-align:center' colspan='5'><b>Preferences</b></th></tr><BR>";

$a=htmlentities(stripslashes($row['Smoking_Drinking']), ENT_QUOTES);$e=$Smoking_Drinking;$x=opt($a,$e);
echo "<tr><td colspan='3' style='text-align:left'>Smoking and Drinking: ".$x."</td>";

$a=htmlentities(stripslashes($row['Preferred_Housing_Type']), ENT_QUOTES);$e=$Preferred_Housing_Type;$x=opt($a,$e);
echo "<td colspan='2' style='text-align:left'>Preferred Housing Type: ".$x."</td></tr><br>";

$a=htmlentities(stripslashes($row['Willing_to_relocate']), ENT_QUOTES);$e=$Willing_to_relocate;$x=opt($a,$e);
echo "<tr><td colspan='3' style='text-align:left'>Willing to Relocate: ".$x."</td>";

$a=htmlentities(stripslashes($row['MC_type_sought']), ENT_QUOTES);$e=$MC_type_sought;$x=opt($a,$e);
echo "<td colspan='2' style='text-align:left'>MC Type Sought: ".$x."</td></tr><br>";

$a=htmlentities(stripslashes($row['Open_to_be_in_mixed_race_MC']), ENT_QUOTES);$e=$Open_to_be_in_mixed_race_MC;$x=opt($a,$e);
echo "<tr><td colspan='3' style='text-align:left'>Open to be in Mixed Race MC: ".$x."</td>";

$a=htmlentities(stripslashes($row['Sexuality']), ENT_QUOTES);$e=$Sexuality;$x=opt($a,$e);
echo "<td colspan='2' style='text-align:left'>Sexuality: ".$x."</td></tr><br>";

$a=htmlentities(stripslashes($row['Religious_Openness']), ENT_QUOTES);$e=$Religious_Openness;$x=opt($a,$e);
echo "<tr><td colspan='3' style='text-align:left'>Religious Openness: ".$x."</td>";

$a=htmlentities(stripslashes($row['Religion']), ENT_QUOTES);$e=$Religion;$x=opt($a,$e);
echo "<td colspan='2' style='text-align:left'>Religion: ".$x."</td></tr><br>";

$a=htmlentities(stripslashes($row['Childrearing_Style_and_Preferences']), ENT_QUOTES);$e=$Childrearing_Style_and_Preferences;$x=opt($a,$e);
echo "<tr><td colspan='3' style='text-align:left'>Childrearing Style and Preferences: ".$x."</td>";

$a=htmlentities(stripslashes($row['Pets']), ENT_QUOTES);$e=$Pets;$x=opt($a,$e);
echo "<td colspan='2' style='text-align:left'>Pets: ".$x."</td></tr><br>";

$a=htmlentities(stripslashes($row['Personal_Entertainment']), ENT_QUOTES);$e=$Personal_Entertainment;$x=opt($a,$e);
echo "<tr><td colspan='3' style='text-align:left'>Personal Entertainment: ".$x."</td>";

$a=htmlentities(stripslashes($row['Housing_Interested_in_Common_Shared_Space_for']), ENT_QUOTES);$e=$Housing_Interested_in_Common_Shared_Space_for;$x=opt($a,$e);
echo "<td colspan='2' style='text-align:left'>Housing—Interested in Common Shared Space for: ".$x."</td></tr><br>";

$a=htmlentities(stripslashes($row['Employment']), ENT_QUOTES);$e=$Employment;$x=opt($a,$e);
echo "<tr><td colspan='5' style='text-align:left'>Employment: ".$x."</td></tr><br></table></div>";
}}

Now we get some of the POSTed search criteria input and sanitize it, after dealing with the possible situation of no search type yet selected, in which case the default is "all". Note that $fieldname and $fieldvalue come in as strings to be exploded into arrays $fieldnames[] and $fieldvalues[].

$R=$_GET['searchtype'];
if($R<>"all" && $R<>"first"){$R="all";}

$Entry=$_POST['entry'];
if($Entry==1){
$replacement = '';
$fieldname = $_POST['fieldname'];
$fieldvalue = $_POST['fieldvalue'];
$relocate=0;$nolocate=0;$gotmatchrelocate=0;$gotmatch=0;

if(strlen($fieldvalue)<>0){
$pattern7 = '/[^a-zA-Z0-9\\_\\,]/i';
$pattern8 = '/[^0-9\\-\\,]/i';
$fieldname=strip_tags($fieldname);
$fieldname=preg_replace($pattern7, $replacement, $fieldname);
$fieldvalue=strip_tags($fieldvalue);
$fieldvalue=preg_replace($pattern8, $replacement, $fieldvalue);
$fieldnames = explode(',',$fieldname);
$fieldvalues = explode(',',$fieldvalue);

We need to process the arrays. This happens as we ensure no value is over 2 characters long. If a field name in the $fieldnames array is "Willing_to_relocate", we need to fill the $relocate variable with a value from the $dist[] array that corresponds to the parallel $fieldvalues[] array being used as the element number in $dist[]. As soon as we have filled the $relocate variable with a $dist[] value, we unset() the $fieldvalue and $fieldname variables if the array lengths are exactly 1. I.e., if you search only for the distance criteria, dump these variables as they will be used as flags later. Additionally, we unset the array elements corresponding to the "Willing_to_relocate" name and its value. Next we use the PHP function array_values() to renumber the other array elements one lower so there is no gap.

for($i=0;$i<count($fieldvalues);$i++){$fieldvalues[$i]=substr($fieldvalues[$i],0,2);
if($fieldnames[$i]=="Willing_to_relocate"){$ii=$i;$relocate=$dist[$fieldvalues[$i]];
}}
if($relocate>0 && count($fieldvalues)==1){unset($fieldvalue);unset($fieldname);}
if($relocate>0){unset($fieldvalues[$ii]);unset($fieldnames[$ii]);$fieldvalues=array_values($fieldvalues);$fieldnames=array_values($fieldnames);}}

Next we get the user's zipcode into $z1. Then—if the search type flag $R is "first"—we push the IDs of the records with nonzero scores into the $first array. Then we deal with any use of groupname, city, state, or zip as search criteria. We get the POSTs and process them, using the substr() function to ensure they are not too long. We filter them using the strip_tags() function to dump tags and the preg_replace() function to dump unacceptable characters. If their search strings are too long, they get alerts and must start the search over, but the type of search is maintained by putting the flag into a query string.

$r=mysql_query("SELECT zip FROM mc_members WHERE username='$U'");
$row = mysql_fetch_array($r);
$z1=$row['zip'];

if($R=="first"){
$first=array();
$r=mysql_query("SELECT id FROM mc_members WHERE score>0 ORDER BY score DESC LIMIT 30");
while($row = mysql_fetch_array($r)){
array_push ($first, $row[0]);}}

$G=$_POST['groupname'];
$C=$_POST['city'];
$S=$_POST['state'];
$Z=$_POST['zip'];

$G=substr($G,0,20);
$C=substr($C,0,33);
$S=substr($S,0,2);
$Z=substr($Z,0,5);

$pattern1 = '/[^a-zA-Z\\-\\s]/i';
$pattern3 = '/[^a-zA-Z0-9\\_]/i';
$pattern5 = '/[^0-9]/';
$pattern6 = '/[^A-Z]/';
$G=strip_tags($G);
$C=strip_tags($C);
$S=strip_tags($S);
$Z=strip_tags($Z);
$C=preg_replace($pattern1, $replacement, $C);
$Z=preg_replace($pattern5, $replacement, $Z);
$G=preg_replace($pattern3, $replacement, $G);
$S=preg_replace($pattern6, $replacement, $S);

if (strlen($G)<6&&strlen($G)>0) {restorem();echo '<script language="javascript">alert("Please enter 6 to 20 characters for group name."); window.location = "match.php?searchtype='.$R.'"; </script>';
}else{
if (strlen($C)<2&&strlen($C)>0) {restorem();echo '<script language="javascript">alert("Please enter 2 to 33 characters for city."); window.location = "match.php?searchtype='.$R.'"; </script>';
}else{
if ((strlen($S)<2 || strlen($S)>2)&&strlen($S)>0) {restorem();echo '<script language="javascript">alert("Please use dropdown list for state."); window.location = "match.php?searchtype='.$R.'"; </script>';
}else{
if (strlen($Z)<5&&strlen($Z)>0) {restorem();echo '<script language="javascript">alert("Please enter 5 characters for zip code."); window.location = "match.php?searchtype='.$R.'"; </script>';
}else{

Now the searching begins. We zero the field called score. We search for any of these: groupname, city, state, and zip. We use the flag $gotmatch to either signal we tried to find one of these but failed (-1) or we got a match (1). We increment the score of records with found search criteria. Next we use if(strlen($fieldvalue)<>0){$gotmatch=-1; to decide if we will search for any of the fields that are not groupname, city, state, and zip. If the distance search from the "Willing_to_relocate" input is used alone as a criteria, the $fieldvalue variable will have been unset so we skip this section. Again we use the $gotmatch=-1 as a tried but failed flag until it changes to 1 because a criteria is found. We look at the values in fields whose name is the one stored in the $fieldname[] array element value, in a loop. If they are greater than 0 and the $fieldvalue[] array element value is -1, $gotmatch gets a 1. But if the $fieldvalue[] array element value is greater than -1, we search for the field value (by putting commas on either side of the number for reasons already discussed), using the MySQL LIKE operator, which lets the field be searched in a substring style, since the search may have to find ,9, in ,8,4,2,9,0,1, for example. The mysql_num_rows() function will work to get the number of rows in the result. The variable $gotmatch gets a 1 if the result set is not 0. We increment the score of records with positive results.

If the search type flag ($R) is "first"—meaning we are doing Search Your Results—we use the IDs of the LAST search's records with positive results by looping through the $first[] array and putting a 1 in those records' score fields. Now, only records with nonzero score fields will get searched. We again search for groupname, city, state, and zip and search for any of the fields that are not groupname, city, state, and zip if the $fieldvalue variable is not 0. If $gotmatch is 1, we now decrement the score field of records with scores>0. As explained before, this will cause the search results to contain only the records whose score field had been GREATER THAN 1 before we decremented the score field.

If there are no results to display, $gotmatch is less than 1 && $relocate is 0, and we display a message and the user sees "No results were found. If you want to Search Your Results, click OK. If you want to Search All, click Cancel." A JavaScript confirm function allows the choice. Note that the restorem() function is run and the score fields are incremented from the $first[] array. There is one other variable combination that will lead to the "no results" message: $relocate>0 && $gotmatchrelocate<1 OR $relocate==0 && $nolocate==0. This would mean that the distance option ($relocate>0) was used in the search but no results ($gotmatchrelocate<1) were found OR that the distance option was not searched for and no non-distance results were found. Again the restorem() function is run and the score fields are incremented from the $first[] array.

Note that in the 2 "successful search" code blocks, we use $w=-1;if($R=="first"){$w=0;}, then when we UPDATE the db table the score>'$w' has the effect of updating only if the score is greater than 0 already if we are in Search Your Results mode, but updating only if the score is greater than -1 (which it always is) if we are in Search All mode. Before finishing this code area, if we are in Search Your Results mode we decrement the score field so only records whose score field is been GREATER THAN 1 will be in the displayed results.

The query "SELECT id,score FROM mc_members WHERE score>0 ORDER BY score DESC LIMIT 30" is run to grab the search results from the db table. Note the limit 30, which prevents any more than 30 results from displaying. Note also the order by score desc, which makes sure that the results with the highest scores are displayed first. Next we run the get_record() function—the main display script. Finally we show the user how many results there were.

//SEARCH HERE
$gotmatch=0;
$sql="UPDATE mc_members SET score=0";
$result=mysql_query($sql);

if($R=="all"){$gotmatch=-1;

if(strlen($G)>0){
$r=mysql_query("SELECT id FROM mc_members WHERE groupname='$G'");
$num_rows = mysql_num_rows($r);
if($num_rows>0){$gotmatch=1;
while($row = mysql_fetch_array($r)){
$ID=$row['id'];$sql="UPDATE mc_members SET score=score+1 WHERE id='$ID'";$result=mysql_query($sql);}}}

if(strlen($C)>0){
$r=mysql_query("SELECT id FROM mc_members WHERE city='$C'");
$num_rows = mysql_num_rows($r);
if($num_rows>0){$gotmatch=1;
while($row = mysql_fetch_array($r)){
$ID=$row['id'];$sql="UPDATE mc_members SET score=score+1 WHERE id='$ID'";$result=mysql_query($sql);}}}

if(strlen($S)>0){
$r=mysql_query("SELECT id FROM mc_members WHERE state='$S'");
$num_rows = mysql_num_rows($r);
if($num_rows>0){$gotmatch=1;
while($row = mysql_fetch_array($r)){
$ID=$row['id'];$sql="UPDATE mc_members SET score=score+1 WHERE id='$ID'";$result=mysql_query($sql);}}}

if(strlen($Z)>0){
$r=mysql_query("SELECT id FROM mc_members WHERE zip='$Z'");
$num_rows = mysql_num_rows($r);
if($num_rows>0){$gotmatch=1;
while($row = mysql_fetch_array($r)){
$ID=$row['id'];$sql="UPDATE mc_members SET score=score+1 WHERE id='$ID'";$result=mysql_query($sql);}}}

if(strlen($fieldvalue)<>0){$gotmatch=-1;

for($i=0;$i<count($fieldvalues);$i++){

if($fieldvalues[$i]=="-1"){
$r=mysql_query("SELECT id FROM mc_members WHERE $fieldnames[$i]>0");
}else{

$v=",".$fieldvalues[$i].",";
$r=mysql_query("SELECT id FROM mc_members WHERE $fieldnames[$i] LIKE '%".$v."%'");
}
$num_rows = mysql_num_rows($r);
if($num_rows>0){$gotmatch=1;}

while($row = mysql_fetch_array($r)){
$ID=$row['id'];$sql="UPDATE mc_members SET score=score+1 WHERE id='$ID'";$result=mysql_query($sql);}}}

}else{$gotmatch=-1; //the else is if $R=="first" -- so below is if it's "first"

for($i=0;$i<count($first);$i++){
$sql="UPDATE mc_members SET score=1 WHERE id='$first[$i]'";
$result=mysql_query($sql);}

if(strlen($G)>0){
$r=mysql_query("SELECT id FROM mc_members WHERE groupname='$G' AND score>0");
$num_rows = mysql_num_rows($r);
if($num_rows>0){$gotmatch=1;
while($row = mysql_fetch_array($r)){
$ID=$row['id'];$sql="UPDATE mc_members SET score=score+1 WHERE id='$ID'";$result=mysql_query($sql);}}}

if(strlen($C)>0){
$r=mysql_query("SELECT id FROM mc_members WHERE city='$C' AND score>0");
$num_rows = mysql_num_rows($r);
if($num_rows>0){$gotmatch=1;
while($row = mysql_fetch_array($r)){
$ID=$row['id'];$sql="UPDATE mc_members SET score=score+1 WHERE id='$ID'";$result=mysql_query($sql);}}}

if(strlen($S)>0){
$r=mysql_query("SELECT id FROM mc_members WHERE state='$S' AND score>0");
$num_rows = mysql_num_rows($r);
if($num_rows>0){$gotmatch=1;
while($row = mysql_fetch_array($r)){
$ID=$row['id'];$sql="UPDATE mc_members SET score=score+1 WHERE id='$ID'";$result=mysql_query($sql);}}}

if(strlen($Z)>0){
$r=mysql_query("SELECT id FROM mc_members WHERE zip='$Z' AND score>0");
$num_rows = mysql_num_rows($r);
if($num_rows>0){$gotmatch=1;
while($row = mysql_fetch_array($r)){
$ID=$row['id'];$sql="UPDATE mc_members SET score=score+1 WHERE id='$ID'";$result=mysql_query($sql);}}}

if(strlen($fieldvalue)<>0){$gotmatch=-1;

for($i=0;$i<count($fieldvalues);$i++){

if($fieldvalues[$i]=="-1"){
$r=mysql_query("SELECT id FROM mc_members WHERE $fieldnames[$i]>0 AND score>0");
}else{

$v=",".$fieldvalues[$i].",";
$r=mysql_query("SELECT id FROM mc_members WHERE $fieldnames[$i] LIKE '%".$v."%' AND score>0");
}
$num_rows = mysql_num_rows($r);
if($num_rows>0){$gotmatch=1;}

while($row = mysql_fetch_array($r)){
$ID=$row['id'];$sql="UPDATE mc_members SET score=score+1 WHERE id='$ID' AND score>0";$result=mysql_query($sql);}}}

if($gotmatch==1){$sql="UPDATE mc_members SET score=score-1 WHERE score>0";$result=mysql_query($sql);}

}//the end of the else that is if $R=="first" (the stuff below is for either "first" or "all")

if ($gotmatch<1 && $relocate==0){restorem();echo '<script language="javascript">var answer = confirm ("No results were found. If you want to Search Your Results, click OK. If you want to Search All, click Cancel.");if (answer){window.location = "match.php?searchtype='.$R.'";}else{window.location = "match.php";} </script>';
}else{

if($gotmatch==1 && $relocate==0){$nolocate=1;}
$gotmatchrelocate=0;

if($gotmatch==1 && $relocate>0){
$nolocate==0;$w=-1;if($R=="first"){$w=0;};
$r=mysql_query("SELECT id,zip FROM mc_members WHERE score>'$w'");
while($row = mysql_fetch_array($r)){
$id=$row['id'];$z2=$row['zip'];$zz=coords($z1,$z2);
if($relocate>=$zz){$sql="UPDATE mc_members SET score=score+1 WHERE id='$id' AND score>'$w'";$result=mysql_query($sql);$gotmatchrelocate=mysql_affected_rows();
}else{

$sql="UPDATE mc_members SET score=0 WHERE id='$id'";$result=mysql_query($sql);
}}
if($w==0){$sql="UPDATE mc_members SET score=score-1 WHERE score>0";$result=mysql_query($sql);}}

if($gotmatch<1 && $relocate>0){
$nolocate==0;$w=-1;if($R=="first"){$w=0;};
$r=mysql_query("SELECT id,zip FROM mc_members WHERE score>'$w'");
while($row = mysql_fetch_array($r)){
$id=$row['id'];$z2=$row['zip'];$zz=coords($z1,$z2);
if($relocate>=$zz){$sql="UPDATE mc_members SET score=score+1 WHERE id='$id' AND score>'$w'";$result=mysql_query($sql);$gm=mysql_affected_rows();if($gm==1){$gotmatchrelocate=1;}
}else{

$sql="UPDATE mc_members SET score=0 WHERE id='$id'";$result=mysql_query($sql);
}}
if($w==0){$sql="UPDATE mc_members SET score=score-1 WHERE score>0";$result=mysql_query($sql);}}

if(($relocate>0 && $gotmatchrelocate<1) || ($relocate==0 && $nolocate==0)){restorem();echo '<script language="javascript">var answer = confirm ("No results were found. If you want to Search Your Results, click OK. If you want to Search All, click Cancel.");if (answer){window.location = "match.php?searchtype='.$R.'";}else{window.location = "match.php";} </script>';
}else{

echo "<BR><BR><BR><BR>";
$r=mysql_query("SELECT id,score FROM mc_members WHERE score>0 ORDER BY score DESC LIMIT 30");
while($row = mysql_fetch_array($r)){
$id=$row['id'];
$score=$row['score'];
get_record($id,$score);}
echo "You got ".mysql_num_rows($r)." results";}

mysql_close();

echo '<center><div class="j"><center><h1>MC Search and Match Results</h1><br>
<a HREF="http://www.thebiganswer.info/">Home</a>&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;<a HREF="register-for-mc.php">Account Management</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a HREF="match.php?searchtype=all">Search All</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a HREF="match.php?searchtype=first">Search These Results</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a HREF="#" onclick="saveresults()">Save These Results</a></center></div></center>';

}}}}}}

if($Entry==0){ ?>

(THE HTML SEARCH CRITERIA SELECTION FORM IS HERE)

<?php
mysql_close();
}
?>