Jump to content

PHP gurus, how can I improve my search code ?


2levelsabove

Recommended Posts

Automotive site in which users are searching by make model and zipcode.

 


session_start(); //to store session variables 
require_once('include/_process/application.php');	


$results='';
$data=array();
$matches=array();             

if (isset($_GET['id']))
{
$searchQuery ="Select reg_users_ads.reg_users_ads_id, dealer, reg_users_ads.year, make, model, reg_users_ads.trim, reg_users_ads.price, reg_users_ads.exterior_color, reg_users_ads.transmission, reg_users_ads.mileage FROM reg_users_ads, atrMakes, atrModels WHERE dealer=".$_GET['id']." AND reg_users_ads.atrMakesID = atrMakes.atrMakesID AND atrMakes.atrMakesID = atrModels.atrMakesID AND reg_users_ads.atrModelsID=atrModels.atrModelsID";

$skipDistanceCalc=1;

}
else
{
//////////////////////////////////distance stuff//////////////////////////////////////////
//lets set variables
if (isset($_GET['page']))//we are following page links so we must use the page variables
{
$make=$_POST['makeo'];
$model=$_POST['modelo'];
$zip=$_POST['USZipcodeo'];
$radius=$_POST['radiuso'];


}
else{
$make=$_POST['make'];
$model=$_POST['model'];
$zip= $_POST['USZipcode'];
$radius = $_POST['radius'];

}


$query = "select * from atrUSZipcode where ZIPCode = '$zip' LIMIT 1";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

if(empty($row)){$results = "<tr><td align='center' class='results' style='font-size:16px;font-weight:bold;color:red;font-family:arial;'>Invalid zip code</td></tr>";}

$lat = $row['lat'];
$lon = $row['lon'];


///////////////////////////////////////////////////////////
$coords = array('latitude' => "$row[lat]", 'longitude' => "$row[lon]");
$sql = "SELECT ZIPCode, ( 3959 * acos( cos( radians( {$coords['latitude']} ) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians( {$coords['longitude']} ) ) + sin( radians( {$coords['latitude']} ) ) * sin( radians( lat ) ) ) ) AS distance FROM atrUSZipcode HAVING distance <= {$radius} ORDER BY distance";


$result = mysql_query($sql);
//we got all the zips within however miles of users zipcode
while($row = mysql_fetch_array($result)){
$validZIPS[] = $row['ZIPCode'];
$zipDistance[]= $row['distance'];
}
$comma_separated_zip = implode("','", $validZIPS);




$searchQuery ="Select reg_users_ads.reg_users_ads_id, reg_users_ads.year, make, model, reg_users_ads.trim, reg_users_ads.price, reg_users_ads.exterior_color, reg_users_ads.transmission, reg_users_ads.mileage, zipcode ";

$searchQuery .= "FROM reg_users_ads, atrMakes, atrModels";


if ($model=="0")//all selected in models
{
$searchQuery .= " WHERE reg_users_ads.atrMakesID = '".$make."' AND reg_users_ads.atrMakesID = atrMakes.atrMakesID AND reg_users_ads.atrModelsID = atrModels.atrModelsID AND reg_users_ads.zipcode IN ('".$comma_separated_zip."') AND status = 'active'";
}
else
{
$searchQuery .= " WHERE reg_users_ads.atrMakesID = '".$make."' AND reg_users_ads.atrModelsID = '".$model."' AND reg_users_ads.atrMakesID = atrMakes.atrMakesID AND reg_users_ads.atrModelsID = atrModels.atrModelsID AND reg_users_ads.zipcode IN ('".$comma_separated_zip."') AND status = 'active'";
}

//error_log($searchQuery,1,"scottf@2levelsabove.com");

//////////////////////////////////////////////////////////
//echo $searchQuery;

$searchResult = mysql_query($searchQuery);

$num_rows=mysql_num_rows($searchResult);

if ($num_rows)
{//if results


while($row = mysql_fetch_array($searchResult)){

$alltheresults[] = $row;
}


unset($row);



//lets add the zipcode distance from user with each row
foreach($alltheresults as $row){

if ($skipDistanceCalc==1)
{$data[] = $row;
}
else{

     $found=array_search($row[9],$validZIPS);
     

     
     
    //  error_log("found is============================== ".$found,1,"scottf@2levelsabove.com");
       if ($found!==false)
          {
      //    error_log("found=".$found,1,"scottf@2levelsabove.com");
          array_unshift($row,$zipDistance[$found]);
          $row[0]=round($row[0],2);
          }
$data[] = $row;
}//else

}//for loop





usort($data,compare);


/////////////////////////////Page numbering stuff/////////////////////////////////
$total_results = mysql_num_rows($searchResult);
$limit = "15"; //limit of archived results per page.
$total_pages = ceil($total_results / $limit); //total number of pages


$page=$_GET['page'];
if (empty($page))
{
$page = "1"; //default page if none is selected
}
$offset = ($page - 1) * $limit; //starting number for displaying results out of DB


///////////////////////////////////////////////////////////////////////////////

$data2=array_slice($data,$offset, $limit);



}//if results 




}

 

 

 

Also this may help you also

 

http://www.phpfreaks.com/forums/index.php/topic,224436.0.html

 

 

This code gets caled everytime when a user navigates to a diff page. :(

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.