2levelsabove Posted November 6, 2008 Share Posted November 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/131652-php-gurus-how-can-i-improve-my-search-code/ Share on other sites More sharing options...
JonnoTheDev Posted November 6, 2008 Share Posted November 6, 2008 Looks a bit of a cut and paste job! How do you want to improve it? Quote Link to comment https://forums.phpfreaks.com/topic/131652-php-gurus-how-can-i-improve-my-search-code/#findComment-683798 Share on other sites More sharing options...
2levelsabove Posted November 6, 2008 Author Share Posted November 6, 2008 Not sure. Im sure there are things that can be done to optimize it . Quote Link to comment https://forums.phpfreaks.com/topic/131652-php-gurus-how-can-i-improve-my-search-code/#findComment-683830 Share on other sites More sharing options...
DeanWhitehouse Posted November 6, 2008 Share Posted November 6, 2008 One , don't ask for help on code you didn't write and don't understand, and two we are not going to make it better for you, if you want to optimise it go ahead, and if you get stuck then ask for help. Quote Link to comment https://forums.phpfreaks.com/topic/131652-php-gurus-how-can-i-improve-my-search-code/#findComment-683869 Share on other sites More sharing options...
bobbinsbro Posted November 6, 2008 Share Posted November 6, 2008 oh look - it's Mr. grumpypants! lol Quote Link to comment https://forums.phpfreaks.com/topic/131652-php-gurus-how-can-i-improve-my-search-code/#findComment-683874 Share on other sites More sharing options...
DeanWhitehouse Posted November 6, 2008 Share Posted November 6, 2008 Mr.Grumpypants who is right though Quote Link to comment https://forums.phpfreaks.com/topic/131652-php-gurus-how-can-i-improve-my-search-code/#findComment-683878 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.