473,480 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

implode an array for query condition

8 New Member
Hi All,

I'm newish to php & have an issue on a project I'm working on...

I've an array containing zip codes which displays if I echo it like
Expand|Select|Wrap|Line Numbers
  1. echo "$key";
it returns 926299262492693926779267592673926079267492672

I need to implode/explode with commas so it passes like 92629,92624,92693,92677,92675,92673,92607,92674,92 672 so I can use it as a WHERE IN condition.

What's the best way to do this?

Thanks
Karl
Feb 28 '07 #1
6 1876
ronverdonk
4,258 Recognized Expert Specialist
Hi All,

I'm newish to php & have an issue on a project I'm working on...

I've an array containing zip codes which displays if I echo it like
Expand|Select|Wrap|Line Numbers
  1. echo "$key";
it returns 926299262492693926779267592673926079267492672

I need to implode/explode with commas so it passes like 92629,92624,92693,92677,92675,92673,92607,92674,92 672 so I can use it as a WHERE IN condition.

What's the best way to do this?

Thanks
Karl
$key is not an array! The value you show via the "echo $key;" here is a normal string value. So you cannot plode it. Unless you have made a mistake

Ronald :cool:
Feb 28 '07 #2
Karl Engstrom
8 New Member
Kinda new/part-time with php, learning every time I sit down with it, please excuse my ignorance.
OK, I guess I'm on the wrong path. I'll detail things a little...
I'm using a class for querying a single table zip code db. I'm adding a table of companies and making a company locator based on range from originating zip code.
I have
Expand|Select|Wrap|Line Numbers
  1. $z = new zipcode_class;
  2. $zips = $z->get_zips_in_range($_POST["zip1"], $_POST["radius"], _ZIPS_SORT_BY_DISTANCE_ASC, true);
  3. if ($zips === false) echo 'Error: '.$z->last_error;
  4. else {
  5.  
  6.    foreach ($zips as $key => $value) {
  7.      $query_rsTech = "SELECT * FROM tech_info WHERE tech_zip_code = '$key,'";
  8.      $rsTech = mysql_query($query_rsTech) or die(mysql_error());
  9.      $row_rsTech = mysql_fetch_assoc($rsTech);
  10.    }
  11. }
Should I replace the foreach to $zips as $array then check with an is_array?

Thanks, learning as I go...
Karl
Mar 1 '07 #3
Karl Engstrom
8 New Member
Can I make the string ($key) an array to use in a query, maybe?

Karl
Mar 2 '07 #4
vssp
268 Contributor
Count the total number and split the string 5 and the store the valus on single array I hope thsi idea is not bad.
Mar 2 '07 #5
Karl Engstrom
8 New Member
I'm gonna try that... After iterating through the first array with
Expand|Select|Wrap|Line Numbers
  1. foreach ($zips as $key => value)
I need to split a string that looks like
926299262492693926779267592673926079267492672
into one that looks like
92629,92624,92693,92677,92675,92673,92607,92674,92 672
so it can be used in a WHERE clause.

I've been reading & trying diffrent things, no success yet.
Any suggestions?
Mar 3 '07 #6
Karl Engstrom
8 New Member
Can we bounce this one? I re-started it under a more descriptive title.

Thanks,
Karl
Mar 3 '07 #7

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

Similar topics

5
3188
by: Andreas Paasch | last post by:
I'm having trouble with the implode function and need some help here. I've been reading at php.net but that didn't solve my problem. I'm having the below code: $query = "SELECT top1, top2,...
5
3900
by: Geoff Berrow | last post by:
I don't particularly need to do this but I've just found out that print implode("<br>", $_SERVER); Gives the warning : Notice: Array to string conversion in c:\phpdev\www... Doesn't do it...
2
1647
by: DJ Craig | last post by:
I have 2-dimensional array with numeric indices for both dimensions, called $results, which contains the search results from a database. The first dimension contains the records, and the second...
8
8039
by: ben3003 | last post by:
I am trying to put a date in my database in the form dd-mm-yyy, as for my PHP page is returns 3 variables $date, $month, $year. is ther a way to put these together? i have tried imploding them but...
11
9419
by: Maksim Kasimov | last post by:
in php-scripts, to insert data to database, i'm doing like this: ... $query_param = array( 'field0' => 1, 'field1' => 3, 'field2' => $var2, 'field3' => $var3, ); ...
18
5876
by: NoWhereMan | last post by:
Maybe a stupid question. ------------------------- 1 ------------------------- $str = ''; for($i=0; $i<10; $i++) $str .= $i;
1
2342
by: scatfly | last post by:
I have a set of values that were put into a form and am now using this to put into a database. this is my error: "Warning: implode() : Bad arguments. in /home/xxxxxx/public_html/login.php on line...
4
5134
by: atyndall | last post by:
OK, this is the relevant portion script: <?php $username = '__'; // MySQL Database Username. $password = '__'; // MySQL Database Password. $server = '__'; // MySQL Database server (most...
3
3149
by: BryanA | last post by:
I'm not quite sure which function to use when I want to add a comma to the end of every line and a semi colon to the last one. Because it generates everything on the fly I can't count the rows from...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7081
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...
0
6921
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5336
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4776
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4481
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.