473,387 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to use associative array in WHERE clause

(I started this under an improper title, will request bounce of the other)
Hello All-
I'm new to php. I'm working on using a zip code class I found in order to create a locater app using 2 tables in a db, one is US zip codes with lat & log info and the other table is company info with a zip code field.
The output I'm working with is an associative array with zip codes and the distance from the originating zip code they are. I have this code grabbing the array contents and showing me what's there
[php]$z = new zipcode_class;
$zips = $z->get_zips_in_range($_POST["zip1"], $_POST["radius"], _ZIPS_SORT_BY_DISTANCE_ASC, true);
if ($zips === false) echo 'Error: '.$z->last_error;

else {foreach ($zips as $zcodes => $dist)
print_r ($zips);
[/php]
(get_zips_in_range is a function and _ZIPS_SORT_BY_DISTANCE_ASC is a switch, both from the class)

Example array contents is
Array ( [92629] => 0 [92624] => 2.14 [92693] => 2.41 [92677] => 2.86 [92675] => 3.3 [92673] => 3.51 [92607] => 3.75 [92674] => 4.9 [92672] => 4.96 ).

I'm hoping to use zcodes as a where clause on the company info table.Echoing zcodes shows a continuous string like
926299262492693926779267592673926079267492672
which I obviously can't use in the clause.

Any help would be great! I'm feeling sooo lost!!

Thanks,
Karl
Mar 3 '07 #1
6 3067
ronverdonk
4,258 Expert 4TB
I'm hoping to use zcodes as a where clause on the company info table.Echoing zcodes shows a continuous string like
926299262492693926779267592673926079267492672
which I obviously can't use in the clause.
Hold a second! From your code I understand that the zcodes are the keys of the array, right? So you can use them individually in your WHERE clause.

Are you, by the way, echoing the zcodes without breaks, so forcing a continuous string? Or am I completely off?

Ronald :cool:
Mar 5 '07 #2
Hold a second! From your code I understand that the zcodes are the keys of the array, right? So you can use them individually in your WHERE clause.

Are you, by the way, echoing the zcodes without breaks, so forcing a continuous string? Or am I completely off?

Ronald :cool:
Hi Ronald-

my echo is just
Expand|Select|Wrap|Line Numbers
  1. echo $zcodes;
for checking the oputput.

Here's what I'm working with as of now:
Expand|Select|Wrap|Line Numbers
  1. {
  2.  $z = new zipcode_class;
  3.  $zips = $z->get_zips_in_range($_POST["zip1"], $_POST["radius"], _ZIPS_SORT_BY_DISTANCE_ASC, true);
  4.  if ($zips === false) echo 'Error: '.$z->last_error;
  5.  else {
  6.      $vzips=0;
  7.      foreach ($zips as $zcodes => $dist)
  8.          {
  9.          if ($vzips++) {$zcodes .= ',' . $azcodes;}
  10.          else {$zcodes = $azcodes;}
  11.          }
  12.      echo $zcodes;
  13.  
  14.      //$query_rsTech = sprintf("SELECT * FROM tech_info WHERE tech_zip_code IN (%s)", mysql_real_escape_string($zcodes));
  15.      $query_rsTech = "SELECT * FROM tech_info WHERE tech_zip_code IN (92629,92624,92693,92677,92675,92673,92607,92674,92672)";
  16.      $rsTech = mysql_query($query_rsTech) or die(mysql_error());
  17.      $row_rsTech = mysql_fetch_assoc($rsTech);
  18.      }
  19.  }
  20.  
  21.  
(The class is by Micah Carrick)

The uncommented query is an example of what I'm working towards...
Am I way lost?still not getting the string I need

Thanks
Karl
Mar 5 '07 #3
ronverdonk
4,258 Expert 4TB
[php]
{
$z = new zipcode_class;
$zips = $z->get_zips_in_range($_POST["zip1"], $_POST["radius"], _ZIPS_SORT_BY_DISTANCE_ASC, true);
if ($zips === false) echo 'Error: '.$z->last_error;
else {
$vzips=0;
foreach ($zips as $zcodes => $dist)
{
if ($vzips++) {$zcodes .= ',' . $azcodes;}
else {$zcodes = $azcodes;}
}
echo $zcodes;

//$query_rsTech = sprintf("SELECT * FROM tech_info WHERE tech_zip_code IN (%s)", mysql_real_escape_string($zcodes));
$query_rsTech = "SELECT * FROM tech_info WHERE tech_zip_code IN (92629,92624,92693,92677,92675,92673,92607,92674,9 2672)";
$rsTech = mysql_query($query_rsTech) or die(mysql_error());
$row_rsTech = mysql_fetch_assoc($rsTech);
}
}
[/php]

That code cannot work! Statement "foreach ($zips as $zcodes => $dist)" assigns $zcodes as the key. Next statement "$zcodes .= ',' . $azcodes;}" overwrites it. In my opinion the code should be:

[php]
{
$z = new zipcode_class;
$zips = $z->get_zips_in_range($_POST["zip1"], $_POST["radius"], _ZIPS_SORT_BY_DISTANCE_ASC, true);
if ($zips === false) echo 'Error: '.$z->last_error;
else {
$vzips=0;
foreach ($zips as $zcodes => $dist)
{
if ($vzips++) {$qzcodes .= ',' . $zcodes;}
else {$qzcodes = $zcodes;}
}
echo $qzcodes;

$query_rsTech = sprintf("SELECT * FROM tech_info WHERE tech_zip_code IN (%s)", mysql_real_escape_string($qzcodes));
//$query_rsTech = "SELECT * FROM tech_info WHERE tech_zip_code IN (92629,92624,92693,92677,92675,92673,92607,92674,9 2672)";
$rsTech = mysql_query($query_rsTech) or die(mysql_error());
$row_rsTech = mysql_fetch_assoc($rsTech);
}
}
[/php]

Test that and see what the echo returns. Should be an array starting with a comma en values separated by comm's.

Ronald :cool:
Mar 5 '07 #4
Thanks Ronald, you nailed it!

Not more than an hour ago I saw the err of my ways (with help from a buddy). Our code looks just like yours with slightly different var names. Now I'm on to the next phase...

I'm learning all the time & really enjoying PHP!

MegaThanks for your help!
Karl
Mar 6 '07 #5
ronverdonk
4,258 Expert 4TB
You are welcome.

But, if you have received this code from someone and tried to use it as-is, the giver cheated because it can never work!

See you again.

Ronald :cool:
Mar 6 '07 #6
cassbiz
202 100+
Hi Karl,

It is funny, your name is the same as one of my college professors who was in San Diego, CA 20+ years ago.
Mar 6 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Stefan Richter | last post by:
Hi, I want to create an associative Array with a PHP variable (article ID) as Key and another associative array as it's value. How do I instanciate it, how can I fill it? I want something...
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
4
by: Kozman | last post by:
I have a problem where I need to use the literal "length" as a subscript in an associative array (I have no control over what is used as a subscript..."length" happens to be one of the uncontrolled...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.