Hi,
I was wondering if someone could explain what is happening here...I have the following PHP code:
$searchword = array('blue','green','yellow','red');
$sql="SELECT * FROM table WHERE item=$item AND (";
for ($i=0; $i<count($searchword); $i++)
{
if ($i=(count($searchword)-1))
{
$sql.="color LIKE $searchword[$i]\"";
}
else
{
$sql.="color LIKE \"$searchword[$i]\" OR ";
}
}
$sql .= ")";
In this case, the final $sql is:
"SELECT * FROM table WHERE item=$item AND (color LIKE 'red')"
However, if I changed the above if-else statement to:
if ($i!=(count($searchword)-1))
{
$sql.="color LIKE $searchword[$i]\" OR ";
}
else
{
$sql.="color LIKE \"$searchword[$i]\"";
}
I get the desired result, that is, $sql is:
"SELECT * FROM table WHERE item=$item AND (color LIKE 'blue' OR color LIKE 'green' OR color LIKE 'yellow' OR color LIKE 'red')"
I would have thought that the first if-else statement would produce the desired result, after all, it is the same condition phrased differently, right? What gives?
Thanks in advance to all of you who answers.
Allen.
P.S. "if ($i<(count($searchword)-1))" works just as well!