开发者

PHP foreach loop

开发者 https://www.devze.com 2023-01-12 02:45 出处:网络
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:

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 cl开发者_如何学Cose after that point.

How I want it to complete:

olderpeople=1 AND palcare=1 AND lookchild=1


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


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


This is what implode() is for:

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

Live Example


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 ';
    }
}


use the implode function

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

and you wont have to use a for loop :)


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

implode(" AND ", $val);


Try this :

$isFirst = true;
foreach ($val as $r){
    if(!$isFirst){
        echo ' AND ';
    }else{
        $isFirst = false;
    }
    echo $r.'=1';
}


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 http://ca3.php.net/manual/en/function.substr.php, quick and easy


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.


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


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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消