How can I use the PHP radius function with multiple variables?
<p>I have been reading up on radius and mysql and I am very confused on how to exactly implement the code with multiple search variables, so could someone break it down for me. </p>

<p>My database has the following tables:</p>

<blockquote>
  <p><strong>idx</strong> | <strong>type</strong> | <strong>price</strong> | <strong>item_desc</strong> | <strong>s_lat</strong> | <strong>s_long</strong> | <strong>created date</strong></p>
</blockquote>

<p>Here is my current code.</p>

<pre><code>$search_origin_radius = "200";
$search_dest_radius = "100";
$search_origin_lat = "37.2629742";
$search_origin_long = "-98.286158";
$search_dest_lat = "37.2629742";
$search_dest_long = "-98.286158";
$type = "consumers";
$price = "100";

$sql = "SELECT * FROM products WHERE `price` = '$price'";

if($type &amp;&amp; !empty($type))
{
    $sql .= " AND `type` = '$type'";    
}
</code></pre>

<p>Everything I have found so far says to use :</p>

<pre><code>SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( latitude ) ) 
    * cos( radians( longitude ) - radians(-122) ) + sin( radians(37) ) * sin(radians(lat)) ) ) AS distance 
FROM myTable
HAVING distance &lt; 50
ORDER BY distance 
</code></pre>

<p>But I am super confused on how I implement it for <code>$search_origin_radius</code> and <code>$search_dest_radius</code>. Basically what I am trying to do is find everything that is for sale within the price range and radius around the two cities.</p>

<p>Example I want to find everything priced for $100 around Oklahoma City, OK within 200 miles and Kansas City, MO within 100 miles.</p>

<p>EDIT** As suggested added this as a stored function.</p>

<pre><code>function Calc_Distance($latitudeFrom, $longitudeFrom, $latitudeTo,         $longitudeTo, $earthRadius = 3959)
{
  // convert from degrees to radians
  $latFrom = deg2rad($latitudeFrom);
  $lonFrom = deg2rad($longitudeFrom);
  $latTo = deg2rad($latitudeTo);
  $lonTo = deg2rad($longitudeTo);

  $latDelta = $latTo - $latFrom;
  $lonDelta = $lonTo - $lonFrom;

  $angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
    cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
  return $angle * $earthRadius;
}
</code></pre>

<p>Using new query but still nothing shows up:</p>

<pre><code>$sql = "
SELECT *
FROM products
WHERE Calc_Distance(s_lat, s_long, $search_origin_lat, $search_origin_long) &lt; 200
  AND Calc_Distance(s_lat, s_long, $search_dest_lat,   $search_dest_long)   &lt; 100
  AND price = $price
";
</code></pre>