Page 1 of 1

couple of problems with two dif set of arrays...

Posted: Thu Sep 11, 2008 9:40 am
by glennnphp
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


i have a large form - someone in here kindly showed me how to concantenate this array into a query:

Code: Select all

 
$twofour = $_POST['twofour'];
if ($twofour != '') {
$twofour_sql = implode(',',$twofour);
} else {
if ($twofour == '') {
$twofour_sql = '';
    }
}
if ($twofour_sql != '') {
$where[] = "prg_2_year IN ($twofour_sql)";
} 
 
 
this works with a set of integers, but i have arrays that are abbreviations, AL, AK, AR etc, with which i'm getting errors, and i'm assuming i have to get them in quotes, but i don't know how (if that's the problem). believe me, i've tried, and i have no idea how to find something like this in documentation at mysql.com or php.net or anywhere...

//*******************//

i have an array where i need to use BETWEEN like this: ...from table WHERE enrollment BETWEEN 1 and 200 OR BETWEEN 201 and 500...

i made the values of the checkboxes "1 and 200" and "201 and 500", but i still can't configure the Form/function correctly.

Code: Select all

 
 
$enrollment = $_POST['enrollment'];
if ($enrollment != '') {
$enrollment = implode(',',$enrollment);
} else {
if ($enrollment == '') {
$enrollment = "";
    }
}
if ($enrollment != '') {
$where[] = "enrollment BETWEEN $enrollment";
} 
 
 
i really appreciate any help that someone can offer. when i see code, i can see how something's done much more easily than reading or hearing "how" to do it...

Thanks again,
GN


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: couple of problems with two dif set of arrays...

Posted: Thu Sep 11, 2008 9:50 am
by pickle
Probably the easiest would be to run the array through a for loop:

Code: Select all

foreach($_POST['twofour'] as $current)
{
   $clause .= "'".trim($current,"'")."',";
}
$clause = rtrim($clause,',');
$where[] = "prg_2_year IN ($clause)";

Re: couple of problems with two dif set of arrays...

Posted: Thu Sep 11, 2008 10:04 am
by glennnphp
awesome, that worked -

sorry about the

Code: Select all

- 

any idea how to get "WHERE enrollment BETWEEN '1 and 200' OR enrollment BETWEEN '201 and 500' OR  enrollment BETWEEN '501 and 1000'", etc. from that array i have?

thanks again

Re: couple of problems with two dif set of arrays...

Posted: Thu Sep 11, 2008 11:45 am
by pickle
Just do a check if the "1 and 200" checkbox is checked - if so, add it to the query followed by 'OR'. Then check if the "201 and 500" is checked and tack it on the end. After all is said & done, remove the trailing OR with rtrim().

Re: couple of problems with two dif set of arrays...

Posted: Thu Sep 11, 2008 12:57 pm
by glennnphp
i've been playing with what you gave me for the other, and came up with this:

Code: Select all

 
if (isset($_POST['submit'])) {
foreach($_POST['enrollment'] as $enroll)
{
   $clause .= " OR enrollment BETWEEN ".$enroll;
}
}   
$clause = ltrim($clause,' OR enrollment ');
$clause = "enrollment ".$clause;
 
 
$query = "SELECT fice FROM characteristics WHERE $clause";
 
 
any problem with it? works with the one variable alone, but i have to add another couple of variables, like:

$query = "SELECT fice FROM characteristics WHERE $where AND $clause";

...where "AND" is conditional on whether $clause contains a value or now... can PHP be ran inline with a query like it can be within a form field?

Re: couple of problems with two dif set of arrays...

Posted: Thu Sep 11, 2008 2:25 pm
by pickle
glennnphp wrote:can PHP be ran inline with a query like it can be within a form field?
Not sure what you mean by this.

Re: couple of problems with two dif set of arrays...

Posted: Thu Sep 11, 2008 2:45 pm
by glennnphp
i wanted to put an if statement in the query like you can in a form field:

"SELECT fice FROM characteristics WHERE $where ". if($enroll != '') {." AND $enroll_clause ."else {". ?>"; ...

i know i'm showing my ignorance... had to ask...

Re: couple of problems with two dif set of arrays...

Posted: Thu Sep 11, 2008 2:53 pm
by pickle
Ah - no I don't think you can do it that way. It's pretty hard to read that anyway - I'd have that if...else clause run & set a variable that is then injected into the query.

Re: couple of problems with two dif set of arrays...

Posted: Thu Sep 11, 2008 2:59 pm
by glennnphp
yeah, that's what i did, only i have a few variables to check for...

thanks a lot. that all helped...