Jack Jack - 6 months ago 13
PHP Question

php foreach loop only returns first character of each string

I have the below code. It is only returning the first charater of a string.

$conn = Mage::getSingleton('core/resource')->getConnection('connection_write');
$str = 'something to search for';
$fields = 'content_field1, content_field2, content_field3, content_field4';
$idFields = 'id_field1, id_field2, id_field3, id_field4';
$tables = 'table1, table2, table3, table4';
$table = explode(', ', $tables);
$field = explode(', ', $fields);
$rowId = explode(', ', $idFields);

$i=1;
while ($i<4) {
$f = $field[$i];
$id = $rowId[$i];
$sql = $conn->select()->from($table[$i], array($f, $id))->where($f . " LIKE ?", '%' . $str . '%');
$result = $conn->fetchRow($sql);

foreach ($result as $row) {
var_dump($row[$id]);
}

$i++;
}


However, if I use
var_dump($row);
the entire string from both the id fields and the content fields are outputted.

Can anyone explain to me what I am doing wrong?

Thanks in advance.

Answer

However, if I use var_dump($row); the entire string from both the id fields and the content fields are outputted.

well, why do you use var_dump($row[$id]); which exactly equals to "take $id'th char from $row string" then?