I am writing a script that locates stores in a 20 mile radius of a given zip code. That part works fine. I am adding the functionality to also choose a product that those stores carry and display only the stores in a 20 mile radius that sell that product.

Each product column in the database has numerous products in it separated by a ';'. I thought I could use the explode function to create an array, but now i am stumped on how to take the array that explode created and add it to the function that finds if the store is within 20 miles of the zip code.

I have tried every operator i could think of (i.e. &&||, &&, ||, and, or) but nothing works.

// Go through and check all stores
    while ($row = mysql_fetch_assoc ($result)) {

      // Separate closest stores
      $distance = Dist ($row['lat'], $row['lon'], $zip['lat'], $zip['lon']);
	  
	  $preSpecialties = $row['specialties'];
	  $preDoor=explode (';',$preSpecialties);
	  $i=count($preDoor);
	  		for($n=0;$n<$i; $n++){

		

      // Check if store is in radius
      if ($distance <= $r && $p == ($preDoor[1]) or ($preDoor[2]) ) {

        $stores[] = array (
          'name'      => $row['name'],
          'address'   => $row['address'],
          'state'     => $row['state'],
          'town'      => $row['town'],
          'postal'    => $row['postal'],
          'phone'     => $row['phone'],
          'hours'     => $row['hours'],
          'specialties'	=>	$row['specialties']
        );

      }
	  
		}

    }

  } else {
    $Errors = ($Errors) ? $Errors : '<p>Errors were found please try again!</p>';
  }

If i change the Products database entry to only have one product I can get it to work by

if ($distance <= $r && $p == $preSpecialties  )

Thanks for the help.

Recommended Answers

All 12 Replies

any one? I guess a more direct question would be what operators could i use for an and/or statement?

i tried the in_array function,but I am still getting the same results.

With the print_r function I see that $preDoor is creating the array correctly. But I dont know if I am using the in_array function correctly...

if (isset ($_POST['product'])) {
	$p = $_POST['product'];
}else{echo "No Product Selected!";} 

    // Go through and check all stores
    while ($row = mysql_fetch_assoc ($result)) {

      // Separate closest stores
      $distance = Dist ($row['lat'], $row['lon'], $zip['lat'], $zip['lon']);
	  
	  $preSpecialties = $row['specialties'];
	  $preDoor=explode (';',$preSpecialties);
	  if(in_array($p,$preDoor)){
		

      // Check if store is in radius
      if ($distance <= $r) {

        $stores[] = array (
          'name'      => $row['name'],
          'address'   => $row['address'],
          'state'     => $row['state'],
          'town'      => $row['town'],
          'postal'    => $row['postal'],
          'phone'     => $row['phone'],
          'hours'     => $row['hours'],
		  'specialties'	=>	$row['specialties']
        );

      }
	 }else{
		//print "Product Not Selected!";
		print_r ($preDoor);
		
	 } 
	

    }
Member Avatar for diafol
if ($distance <= $r && ($p == $preDoor[1] || $p == $preDoor[2]) ) {

are you sure you've got the predoor index right? [1] and [2] and not [0] and [1] ?

i have tried with the array keys defined. but still nothing. Tried again with the format you used ardav, but still nothing.

Its strange, maybe i am doing something else wrong. It functions correctly when i changed the products for one of the stores with a zip code of 64083(and a product of "AYR Saline Gel"), but when I did it to another (with a zip of 08827 and a product of "Ayr Gel No-Drip Sinus Spray") just now it still won't work, but the previous search still finds a store.

if you want you can see it in action here:

Zip Code Test

Any other ideas?

Here is the last try i attempted...:

// Go through and check all stores
    while ($row = mysql_fetch_assoc ($result)) {

      // Separate closest stores
      $distance = Dist ($row['lat'], $row['lon'], $zip['lat'], $zip['lon']);
	  
	  $preSpecialties = $row['specialties'];
	  $preDoor=explode (';',$preSpecialties);
	 // if(in_array($p,$preDoor)){
		

      // Check if store is in radius
      if ($distance <= $r && ($p == $preDoor[0] || $p == $preDoor[1] || $p == $preDoor[2] || $p == $preDoor[3] || $p == $preDoor[4] || $p == $preDoor[5]
|| $p == $preDoor[6] || $p == $preDoor[7] || $p == $preDoor[8] || $p == $preDoor[9] || $p == $preDoor[10])) {

        $stores[] = array (
          'name'      => $row['name'],
          'address'   => $row['address'],
          'state'     => $row['state'],
          'town'      => $row['town'],
          'postal'    => $row['postal'],
          'phone'     => $row['phone'],
          'hours'     => $row['hours'],
	'specialties'	=>	$row['specialties']
        );

      }
	 //}else{
		//print "Product Not Selected!";
	//	print_r ($preDoor);
		
	 //} 
	echo $preDoor[0];

    }

trim $preDoor[0] and the others, little example:

<?php
$a = 'hello';
$b = 'hello ';
echo ($a == $b) ? 'true':'false';
echo ' ';
echo ($a == trim($b)) ? 'true':'false';
?>

I don't think it is an issue with white space. (I assume that is what you meant by 'trim') here is a sample of what is stored in the products column for one company.:

Ayr Allergy & Sinus Mist;Ayr Gel No-Drip Sinus Spray;Ayr Gel Swabs;Ayr Saline Drops;Ayr Saline Gel;Ayr Saline Mist;Ayr Saline Nasal Neti Rinse Kit;Ayr Saline Nasal Rinse Kit;Ayr Saline Nasal Rinse Kit Refills;Baby Ayr Nose Spray/Drops;Benzedrex Inhaler;

There isn't any white space around the separator value.

But i maybe misunderstanding the use of trim in this situation.

Member Avatar for diafol
if ($distance <= $r && ($p == $preDoor[0] || $p == $preDoor[1] || $p == $preDoor[2] || $p == $preDoor[3] || $p == $preDoor[4] || $p == $preDoor[5]
|| $p == $preDoor[6] || $p == $preDoor[7] || $p == $preDoor[8] || $p == $preDoor[9] || $p == $preDoor[10])) {

no need for that, try:

if ($distance <= $r && in_array($p,$preDoor)) {

im still getting nothing....

maybe if i post the script in its entirety it would be of some help...

<?php





// Create page variables

$r = NULL;

$z = NULL;

$stores = NULL;

$Errors = NULL;



// Establish DB connection

$dbc = mysql_connect ('localhost', 'cowtowntest', 'login2myplesk');

mysql_select_db ('ascherdb', $dbc);



// Declare page functions

function Dist ($lat_A, $long_A, $lat_B, $long_B) {



  $distance = sin(deg2rad($lat_A))

      * sin(deg2rad($lat_B))

      + cos(deg2rad($lat_A))

      * cos(deg2rad($lat_B))

      * cos(deg2rad($long_A - $long_B));



  $distance = (rad2deg(acos($distance))) * 69.09;

  return $distance;



}



### Handle form if submitted

if (isset ($_POST['submitted'])) {



  // Validate Zip code field

  if (!empty ($_POST['zip']) && is_numeric ($_POST['zip'])) {



    $z = (int)$_POST['zip'];



    // Verify zip code exists

    $query = "SELECT lat, lon FROM wp_zip_codes WHERE zip = '$z'";

    $result = mysql_query ($query);



    if (mysql_num_rows ($result) == 1) {

      $zip = mysql_fetch_assoc ($result);

    } else {

      $Errors = '<p>The zip code you entered was not found!</p>';

    }



  }



  // Validate radius field

  //if (isset ($_POST['radius']) && is_numeric ($_POST['radius'])) {

    $r = 20;//(int)$_POST['radius']

  //}

if (isset ($_POST['product'])) {

	$p = $_POST['product'];

}else{echo "No Product Selected!";} 

 // Proceed if no errors were found

   if ($r && $z && $p) {

  

	global $wpdb;

	

	$stores = array();

    // Retrieve coordinates of the stores

    $query = "SELECT name, address, town, state, postal, phone, specialties, lat, lon

    FROM wp_stores

    INNER JOIN wp_zip_codes

    ON wp_stores.postal = wp_zip_codes.zip";

    $result = mysql_query ($query);

	

    // Go through and check all stores

    while ($row = mysql_fetch_assoc ($result)) {



      // Separate closest stores

      $distance = Dist ($row['lat'], $row['lon'], $zip['lat'], $zip['lon']);

	  

	  $preSpecialties = $row['specialties'];

	  $preDoor=explode (';',$preSpecialties);

		



      // Check if store is in radius

      if ($distance <= $r && in_array($p,$preDoor)) {



        $stores[] = array (

          'name'      => $row['name'],

          'address'   => $row['address'],

          'state'     => $row['state'],

          'town'      => $row['town'],

          'postal'    => $row['postal'],

          'phone'     => $row['phone'],

          'hours'     => $row['hours'],

		  'specialties'	=>	$row['specialties']

        );



      }

		 

	echo $preDoor[0];



    }



  } else {

    $Errors = ($Errors) ? $Errors : '<p>Errors were found please try again!</p>';

  }



}



?><html>

<head>

<title>Store Locator</title>

</head>

<body>

  <p>Enter your zip code below to find locations near you.</p>

<form action="" method="post">



<?php echo ($Errors) ? $Errors : ''; ?>

<div id="locator_wrapper">

	<div id="row">

		<div id="zip" style="float:left; margin:0 0px 0 10px;">Zip:  </div>

		<div id="input_text" style="float:left; margin:0px 0px 0px 45px;"><input name="zip" type="text" size="10" /></div>

	</div>

	<div id="row2">

		<div id="product" style="float:left; clear:left; margin:10px;">Product:  </div>

		<div id="select_box" style="float:left; margin:10px;"><select name="product" id="product">

	<?php

	global $wpdb;

	$query = "SELECT * from ". $wpdb->prefix . "ascher_products";

	$post=$wpdb->get_results($query);



foreach($post as $row) {

	$id=$row->Product_id;

	$product=$row->Product;

	echo"<option name='product' value='".$product."'>".$product."</option>";

	}

	?>

	</select></div>

	</div>

	<div id="row3">

		<div id="submit" style="clear:both; margin:10px 10px 10px 78px">

			<input type="hidden" name="submitted" value="submitted" />

			<input type="submit" value="Submit" />

		</div>

	</div>

  </div>



</form>



<?php



if (isset ($stores)) {



  if (!empty ($stores)) {

	echo "<h5>You Searched for: ".$p." around ".$z."</h5>";

    echo '<p><strong>' . count ($stores) . ' results were found.</strong></p>';

    foreach ($stores as $value) {

		  $aDoor = $value['specialties'];

		  $postDoor= explode (';',$aDoor);

		  $cleanedDoor= count($postDoor);

	

      echo '<p><strong>' . $value['name'] . '</strong><br />';

      echo $value['address'] . '<br />';

      echo $value['town'] . ', ' . $value['state'] . ' ' . $value['postal'];

      echo '&nbsp;<a target="_blank" href="http://maps.google.com/maps?q=',

      $value['address'], ' ',

      $value['town'], ', ',

      $value['state'], ' ',

      $value['postal'],

      '"><span style="color:blue;">Map this location</a><br />';

      echo 'Phone: ' . $value['phone'] . '<br />';

      echo 'specialties: ';



	echo "<br />".$postDoor[0]."<br />".$postDoor[2]."<br />".$postDoor[3]."<br />".$postDoor[4]."<br />".$postDoor[5]."<br />".$postDoor[6]."<br />".$postDoor[7]."<br />".$postDoor[8]."<br />".$postDoor[9]."<br />".$postDoor[10];

	 echo '</p>';

		

    }



  } else {

    echo '<p><strong>No results found</strong></p>';

  }



}



?>

</body>

</html>
Member Avatar for diafol

ANy chance you can cut down on the length? E.g. remove all the blank lines? 360-odd lines is too much for me.

Hope this is better. All that white space is so i can decipher what is actually going on...(i get lost sometimes!)

<?php
// Create page variables
$r = NULL;
$z = NULL;
$stores = NULL;
$Errors = NULL;
// Establish DB connection
$dbc = mysql_connect ('localhost', 'xxxxxx', 'xxxxxx');
mysql_select_db ('xxxxxx', $dbc);
// Declare page functions
function Dist ($lat_A, $long_A, $lat_B, $long_B) {
$distance = sin(deg2rad($lat_A))
* sin(deg2rad($lat_B))
+ cos(deg2rad($lat_A))
* cos(deg2rad($lat_B))
* cos(deg2rad($long_A - $long_B));
$distance = (rad2deg(acos($distance))) * 69.09;
return $distance;
}
### Handle form if submitted
if (isset ($_POST['submitted'])) {
// Validate Zip code field
if (!empty ($_POST['zip']) && is_numeric ($_POST['zip'])) {
$z = (int)$_POST['zip'];
// Verify zip code exists
$query = "SELECT lat, lon FROM wp_zip_codes WHERE zip = '$z'";
$result = mysql_query ($query);
if (mysql_num_rows ($result) == 1) {
$zip = mysql_fetch_assoc ($result);
} else {
$Errors = '<p>The zip code you entered was not found!</p>';
}
}
// Validate radius field
//if (isset ($_POST['radius']) && is_numeric ($_POST['radius'])) {
$r = 20;//(int)$_POST['radius']
//}
if (isset ($_POST['product'])) {
$p = $_POST['product'];
}else{echo "No Product Selected!";}
// Proceed if no errors were found
if ($r && $z && $p) {
global $wpdb;
$stores = array();
// Retrieve coordinates of the stores
$query = "SELECT name, address, town, state, postal, phone, specialties, lat, lon
FROM wp_stores
INNER JOIN wp_zip_codes
ON wp_stores.postal = wp_zip_codes.zip";
$result = mysql_query ($query);
// Go through and check all stores
while ($row = mysql_fetch_assoc ($result)) {
// Separate closest stores
$distance = Dist ($row['lat'], $row['lon'], $zip['lat'], $zip['lon']);
$preSpecialties = $row['specialties'];
$preDoor=explode (';',$preSpecialties);
// if(in_array($p,$preDoor)){
// Check if store is in radius
if ($distance <= $r && in_array($p,$preDoor)) {
$stores[] = array (
'name'      => $row['name'],
'address'   => $row['address'],
'state'     => $row['state'],
'town'      => $row['town'],
'postal'    => $row['postal'],
'phone'     => $row['phone'],
'hours'     => $row['hours'],
'specialties'	=>	$row['specialties']
);
}
//}else{
//print "Product Not Selected!";
//	print_r ($preDoor);
//}
echo $preDoor[0];
}
} else {
$Errors = ($Errors) ? $Errors : '<p>Errors were found please try again!</p>';
}
}
?><html>
<head>
<title>Store Locator</title>
</head>
<body>
<p>Enter your zip code below to find locations near you.</p>
<form action="" method="post">
<?php echo ($Errors) ? $Errors : ''; ?>
<div id="locator_wrapper">
<div id="row">
<div id="zip" style="float:left; margin:0 0px 0 10px;">Zip:  </div>
<div id="input_text" style="float:left; margin:0px 0px 0px 45px;"><input name="zip" type="text" size="10" /></div>
</div>
<div id="row2">
<div id="product" style="float:left; clear:left; margin:10px;">Product:  </div>
<div id="select_box" style="float:left; margin:10px;"><select name="product" id="product">
<?php
global $wpdb;
$query = "SELECT * from ". $wpdb->prefix . "ascher_products";
$post=$wpdb->get_results($query);
foreach($post as $row) {
$id=$row->Product_id;
$product=$row->Product;
echo"<option name='product' value='".$product."'>".$product."</option>";
}
?>
</select></div>
</div>
<div id="row3">
<div id="submit" style="clear:both; margin:10px 10px 10px 78px">
<input type="hidden" name="submitted" value="submitted" />
<input type="submit" value="Submit" />
</div>
</div>
</div>
</form>
<?php
if (isset ($stores)) {
if (!empty ($stores)) {
echo "<h5>You Searched for: ".$p." around ".$z."</h5>";
echo '<p><strong>' . count ($stores) . ' results were found.</strong></p>';
foreach ($stores as $value) {
$aDoor = $value['specialties'];
$postDoor= explode (';',$aDoor);
$cleanedDoor= count($postDoor);
echo '<p><strong>' . $value['name'] . '</strong><br />';
echo $value['address'] . '<br />';
echo $value['town'] . ', ' . $value['state'] . ' ' . $value['postal'];
echo '&nbsp;<a target="_blank" href="http://maps.google.com/maps?q=',
$value['address'], ' ',
$value['town'], ', ',
$value['state'], ' ',
$value['postal'],
'"><span style="color:blue;">Map this location</a><br />';
echo 'Phone: ' . $value['phone'] . '<br />';
echo 'specialties: ';
echo "<br />".$postDoor[0]."<br />".$postDoor[2]."<br />".$postDoor[3]."<br />".$postDoor[4]."<br />".$postDoor[5]."<br />".$postDoor[6]."<br />".$postDoor[7]."<br />".$postDoor[8]."<br />".$postDoor[9]."<br />".$postDoor[10];
echo '</p>';
}
} else {
echo '<p><strong>No results found</strong></p>';
}
}
?>
</body>
</html>

Deadline came.
So to make the application function correctly, I changed the way my data in the mysql table reads, and now each company has one product in their row.

After changing that, I noticed that i was never checking $p against $preSpecialties.

If I would have had a little more time, I think that i could have gotten the explode function to work if i had checked it against the $p variable.


The newly working formatted code is below:

while ($row = mysql_fetch_assoc ($result)) {

// Separate closest stores

$distance = Dist ($row['lat'], $row['lon'], $zip['lat'], $zip['lon']);

$preSpecialties = $row['specialties'];


// if(in_array($p,$preDoor)){

// Check if store is in radius

if ($distance <= $r && $p == $preSpecialties) {

$stores[] = array (

'name'      => $row['name'],

'address'   => $row['address'],

'state'     => $row['state'],

'town'      => $row['town'],

'postal'    => $row['postal'],

'phone'     => $row['phone'],

'hours'     => $row['hours'],

'specialties'	=>	$row['specialties']

);

}


}

} else {

$Errors = ($Errors) ? $Errors : '<p>Errors were found please try again!</p>';

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.