-1
foreach($search_exploded as $search_each)
{
@$x++;
if($x==1)
@$construct .="keywords LIKE '%$search_each%'";
else
$construct .="AND keywords LIKE '%$search_each%'";

}




i just wont to understand this foreach ($search_exploded as $search_each)
what is the meaning
4
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by Bachu
0

looks like there should be more code for this. All I can tell you about this so far is that is is;
- Looping through an array called $search_exploded placing each value into the variable $search_each
- It then adds 1 to $x (assuming that is was previously defined)
- It then checks if $x is equal to 1 (first time through the loop)
- If it is the first time through the loop it will concatinate "keywords LIKE '%$search_each%'" to the string $construct (also assuming that $construct has previously been defined)
- if not it will concatinate "AND keywords LIKE '%$search_each%'" instead

The code is not written to clearly, although not required is is always best to use {} with if statements.

$x = 0; 
// the $x++ is at the start of the loop so we will need to make its value 0 
// else it will never be evaluated as 1. usually you will put the plus counter 
//at the end of a loop.
$construct = ''; // empty string
foreach($search_exploded as $search_each) {
    $x++;

    if($x==1) {
        $construct .="keywords LIKE '%$search_each%'";
    } else {
        $construct .="AND keywords LIKE '%$search_each%'";
    }
}
0
<?php
// If 
$search_exploded = array('searchValue1','searchValue2','searchValue3'); // this means $search_exploded[0]=searchValue1;$search_exploded[1]=searchValue2;$search_exploded[2]=searchValue3 
$construct="";
foreach($search_exploded as $x=>$search_each)
{
    //$x -> index of an array; Here 0,1,2
    //$search_each -> Value of array ; hear 'searchValue1','searchValue2','searchValue3'
    $x++;
    if($x==1)
    $construct .="keywords LIKE '%$search_each%'";
    else
    $construct .=" AND keywords LIKE '%$search_each%'";
}

echo $construct; // Output is =>keywords LIKE '%searchValue1%' AND keywords LIKE '%searchValue2%' AND keywords LIKE '%searchValue3%'
?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.