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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
glennnphp
Forum Newbie
Posts: 16
Joined: Thu Aug 28, 2008 10:13 pm

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

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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)";
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
glennnphp
Forum Newbie
Posts: 16
Joined: Thu Aug 28, 2008 10:13 pm

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

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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().
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
glennnphp
Forum Newbie
Posts: 16
Joined: Thu Aug 28, 2008 10:13 pm

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

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
glennnphp
Forum Newbie
Posts: 16
Joined: Thu Aug 28, 2008 10:13 pm

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

Post 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...
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
glennnphp
Forum Newbie
Posts: 16
Joined: Thu Aug 28, 2008 10:13 pm

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

Post by glennnphp »

yeah, that's what i did, only i have a few variables to check for...

thanks a lot. that all helped...
Post Reply