parsing an array
Moderator: General Moderators
parsing an array
hello,
I am getting a string array from a bunch of textfields with the $_Post
function. what i want to do is put that into a sql query, but if they dont fill out all the text fields there is blank values.
for example
('duncan','victoria','vancouver','sooke','naniamo','ladysmith','','','','','','','','','','','','','','','','','','','','','','','','','','','')
Im new to php so i'm not usre how to do it but is there a way to go through the array so I only have the cities and not the blank fields?
Thanks
I am getting a string array from a bunch of textfields with the $_Post
function. what i want to do is put that into a sql query, but if they dont fill out all the text fields there is blank values.
for example
('duncan','victoria','vancouver','sooke','naniamo','ladysmith','','','','','','','','','','','','','','','','','','','','','','','','','','','')
Im new to php so i'm not usre how to do it but is there a way to go through the array so I only have the cities and not the blank fields?
Thanks
Sorry, here is how I am getting this:
SELECT a.AreaServedID FROM Areas a WHERE a.Area LIKE ('duncan','victoria','vancouver','sooke','naniamo','ladysmith','','','','','','','','','','','','','','','','','','','','','','','','','','',''
this is the sql string
LOL , dont laugh i'm a noob, what i want (I think i want anyways) is to
get this
SELECT a.AreaServedID FROM Areas a WHERE a.Area LIKE
('duncan','victoria','vancouver','sooke','naniamo','ladysmith')
SELECT a.AreaServedID FROM Areas a WHERE a.Area LIKE ('duncan','victoria','vancouver','sooke','naniamo','ladysmith','','','','','','','','','','','','','','','','','','','','','','','','','','',''
this is the sql string
Code: Select all
$countArea = count($areaServed);
$sqlSelect = "SELECT a.AreaServedID
FROM Areas a
WHERE a.Area LIKE (";
for($k = 0; $k < $countArea; $k++){
if('$areaServed[$k]' eq "''") {
$sqlSelect.= ")";
} else {
$sqlSelect.= "'$areaServed[$k]'";
if(($k + 1) < $countArea) {
$sqlSelect.= ",";
}
}
}
echo "$sqlSelect";get this
SELECT a.AreaServedID FROM Areas a WHERE a.Area LIKE
('duncan','victoria','vancouver','sooke','naniamo','ladysmith')
Basically, I think you need a "a.Area LIKE" for each city you want to check. So something like "SELECT a.AreaServedID FROM Areas a WHERE (a.Area LIKE 'duncan') OR (a.Area LIKE 'victoria')" etc.
Here's how I'd do it. I'm just writing this here, so I haven't tested it. I'm also not entirely sure how you are getting your data from the $_POST array, so I'm just going to assume that you want everything from the post array that is not blank.
If you don't want all of the $_POST array, we can help you there too, but we'd need more info on how you are getting the data (checkboxes or what?), like m3rajk asked.
Here's how I'd do it. I'm just writing this here, so I haven't tested it. I'm also not entirely sure how you are getting your data from the $_POST array, so I'm just going to assume that you want everything from the post array that is not blank.
Code: Select all
$cities = array();
foreach($_POST as $v) {
if (strlen($v) > 0) $cities[] = "(a.Area LIKE '$v')";
}
// $cities now contains all of the where clauses for each city
// implode them into one string, this makes figuring out the OR's, etc easier
$str = implode(' OR ', $cities);
// $str is now something like "(a.Area LIKE 'duncan') OR (a.Area LIKE 'victoria')"
$query = "SELECT a.AreaServedID FROM Areas a WHERE $str";
// Run query here, etcgallahad, reason i'm asking is because an array in php wont have blank spaces unless it was checkboxes and set up something like:
<input type="check" name="cities[1]" value="whatever">
if it were just "cities" he wouldn't have the empty spots that he mentioned, which is why i wanna knwo HOW he's collecting the information before i say anything
<input type="check" name="cities[1]" value="whatever">
if it were just "cities" he wouldn't have the empty spots that he mentioned, which is why i wanna knwo HOW he's collecting the information before i say anything
ok.
well seeing as how i've been through this, you will only get what's selected, so make your options set up something like....
and now insert $areaoptions into your heredoc that makes the page.
when you get it back, decide the character to use for a seperator when you put it itnto the text feild of your db, and then....
$chosenAreas=$_POST['area'];
$area_list_for_db=implode(';', $chosenAreas);
well seeing as how i've been through this, you will only get what's selected, so make your options set up something like....
Code: Select all
/*this is an example meant so that you can also do error checking with repopulation -- assumes post */
$areapotions='';
$areas=array('duncan','victoria','vancouver','sooke','naniamo','ladysmith');
foreach($areas as $area){
if(contains($area, $_POST['area'])){ (nothing will match idf it's not set and no error gets returned)
$areaoptions.="<option value="$area" selected>$area</option>";
}else{
$areaoptions.="<option value="$area">$area</option>";
}
}when you get it back, decide the character to use for a seperator when you put it itnto the text feild of your db, and then....
$chosenAreas=$_POST['area'];
$area_list_for_db=implode(';', $chosenAreas);
contains is a function i made for myself to check if something is in an array
Code: Select all
function contains($check, $array){ # see if something is in an array
foreach($array as $value){ # for every item in the array
if($check==$value){ # if an item matches what i have stored
return TRUE; } } # return true and exit the function
return FALSE; # if it's not found, exit as false
}DougieC, do you want people to be able to enter custom cities, or do you want to limit what they can choose to just certain values? What is this for, is it a search engine type thing or sort of a lookup information page?
m3rajk, look at the php manual section on Array Functions, especially in_array.
m3rajk, look at the php manual section on Array Functions, especially in_array.