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

Recommended Answers

All 3 Replies

It means that for each search for love that exploded in our faces, there is love searching for each of us.

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%'";
    }
}
<?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%'
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.