Page 1 of 1

parsing an array

Posted: Thu Aug 14, 2003 3:11 pm
by DougieC
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

Posted: Thu Aug 14, 2003 3:18 pm
by m3rajk
ummm...
how are you doing that???
you need to give us an idea of what you're trying to get.

Posted: Thu Aug 14, 2003 3:43 pm
by DougieC
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

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";
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')

Posted: Thu Aug 14, 2003 4:03 pm
by m3rajk
so they select multiple things in a select box or checkbox?

or is it a series of radios?

Posted: Thu Aug 14, 2003 4:10 pm
by Galahad
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.

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, etc
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.

Posted: Thu Aug 14, 2003 4:13 pm
by m3rajk
gallahad, 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

Posted: Thu Aug 14, 2003 4:35 pm
by DougieC
the array is from a series of text fields
the "SELECT blah blah blah" is an echo of my sql query so thats what it looks like after i build it.
Sorry for the confusion
and I need to get the values that match all the cities so i'm not sure the OR is for that?

Posted: Thu Aug 14, 2003 4:54 pm
by m3rajk
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....

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>";
  }
}
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);

Posted: Thu Aug 14, 2003 4:56 pm
by m3rajk
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
}

Posted: Thu Aug 14, 2003 5:19 pm
by Galahad
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.