3

I have the array example below that I am using to dynamically create an SQL query based on the options ticked in a form. The code below tests whether there is a value, if so, append it to the array:

if ($lookchild) { $val[]='lookchild'; }
if ($mentalcap) { $val[]='mentalcap'; }
if ($mentalheal) { $val[]='mentalheal'; }
if ($olderpeople) { $val[]='olderpeople'; }
if ($palcare) { $val[]='palcare'; }

I am then looping through the array and adding the rest of the SQL statement:

foreach ($val as $r){
 echo $r.'=1 AND ';
} 

This produces:

olderpeople=1 AND palcare=1 AND lookchild=1 AND

When the loop reaches the last entry, I don't want it to append the AND to it as the SQL statement needs to close after that point.

How I want it to complete:

olderpeople=1 AND palcare=1 AND lookchild=1
asked Aug 19, 2010 at 20:41

11 Answers 11

10

Implode

In these situations you can use implode

It 'glues' an array together.

implode ( string $glue , array $pieces )

Example:

echo implode('=1 AND ', $val);
echo '=1';
answered Aug 19, 2010 at 20:44
Sign up to request clarification or add additional context in comments.

2 Comments

You're missing the close quote on the last echo, and you're missing a space after AND.
Thanks Michael. Works as requested!
3

A common trick is to use 'WHERE 1=1' then you can append ' AND foo = bar' without a syntax error.

WHERE 1=1 AND olderpeople=1 AND palcare=1 AND lookchild=1
answered Aug 19, 2010 at 20:44

Comments

1

This is what implode() is for:

$result = array();
foreach ($val as $r){
 $result[] = "$r=1";
}
$result = implode($result, ' AND ');

Live Example

answered Aug 19, 2010 at 20:44

Comments

1

Just don't print the AND for the last value of the foreach loop. Here is the code to use:

foreach ($val as $r){
 echo $r.'=1';
 if (next($val)) {
 echo ' AND ';
 }
}
answered Aug 19, 2010 at 20:47

Comments

1

use the implode function

$sql = implode("=1 AND ", $array)."=1";

and you wont have to use a for loop :)

answered Aug 19, 2010 at 20:44

1 Comment

What about the very last item? blah=1 AND orange. (it won't get the necessary =1)
1

Instead on assigning palcare to $val[], assign $val[] = "palcare = 1" etc. Them

implode(" AND ", $val);
answered Aug 19, 2010 at 20:48

Comments

0

Try this :

$isFirst = true;
foreach ($val as $r){
 if(!$isFirst){
 echo ' AND ';
 }else{
 $isFirst = false;
 }
 echo $r.'=1';
}
answered Aug 19, 2010 at 20:44

Comments

0

I would remove the last 4 characters of the string with:

$r = '';
foreach ($val as $r){
 $r.'=1 AND ';
}
$r = substr($r, 0, -4);
echo $r;

Checkout https://www.php.net/manual/en/function.substr.php, quick and easy

answered Aug 19, 2010 at 20:48

Comments

0

If you have to do it with a foreach (and for some reason you cant use implode, which is a good suggestion) you will need a way to keep track of where you are.

I thought to add the "AND" before anything but the first item, instead of adding it after anything but the last item, something like this:

$sqlwhere = "";
foreach ($val as $r){
 if($sqlwhere ==""){
 $sqlwhere = $r;
 }
 else {
 $sqlwhere .= " AND " . $sqlwhere;
 }
}
echo $sqlwhere;

I used a varable instead of just echoing it out too, which I find useful in complicated sql statements anyway.

answered Aug 19, 2010 at 20:49

Comments

0

Use implode. But if for some reason you need to loop (such as you need to do more logic than just joining the strings), use a separator approach:

$seperator = '';
$result = '';
foreach ($array as $value) {
 // .. Do stuff here
 $result .= $seperator . $value;
 $seperator = ' AND ';
}

The benefit is both brevity and flexibility without checking conditions all the time...

answered Aug 19, 2010 at 20:51

Comments

0

Since you are using an array, you can also use count to figure out how many are in the array and if you are on the last item, don't append the 'AND'.

$result = array();
$totalcount = count($val);
$currentCount = 0;
foreach ($val as $r){
 $currentCount ++; 
 if ($currentCount != $totalcount){$result[] = "$r=1 AND ";}else{$result[] = "$r=1";}
}
answered Aug 19, 2010 at 21:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.