I want to fetch each element of array in foreach loop.
Here in my code my array is displayed, I want to fetch this array element in if condition.
if (!empty($data[5])) {
$selectsql = "select value_id from catalog_product_entity_media_gallery where entity_id = '".$entity_id."'";
$selectsqlresult = $connection->query($selectsql);
$resultquery = $selectsqlresult->fetchAll();
print_r($resultquery);
if(!empty($resultquery['value_id']))
{
$imgpos = 2;
foreach($resultquery as $value)
{
$updatequery = "update catalog_product_entity_media_gallery_value set position = '".$imgpos."' where value_id = '".$value."' limit 1";
//$connection->query($updatequery);
echo $updatequery . "
";
echo "Images Position - " . $imgpos ."
"; $imgpos++;
}
}
}
I want to increase images position by 1 as per the array element. Image position $imgpos
is used in update query.
Please provide some guidance.
You could also use the key value in the foreach statement to get a incremented value:
if(!empty($resultquery['value_id']))
{
foreach($resultquery as $imgpos => $value)
{
$updatequery = "update catalog_product_entity_media_gallery_value set position = '".$imgpos."' where value_id = '".$value."' limit 1";
//$connection->query($updatequery);
echo $updatequery . "
";
echo "Images Position - " . $imgpos ."
"; $imgpos++;
}
}