I can’t figure out how to log into my account here on Stackoverflow, I find it a little confusing. Anyway, I’ve asked a question here about a problem I’m having:
I’ve since found an open source project that does exactly what I need done but the code is PHP and while I can understand most of it there are bits I don’t get. I’ll post it here with my comments through it and if someone can add extra details that would be appreciated.
public function productAttributeExists($attributesList, $currentProductAttribute = false)
{
$result = Db::getInstance()->ExecuteS('SELECT pac.`id_attribute`, pac.`id_product_attribute`
FROM `'._DB_PREFIX_.'product_attribute` pa
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON (pac.`id_product_attribute` = pa.`id_product_attribute`)
WHERE pa.`id_product` = '.intval($this->id));
if (!$result OR empty($result))
return false;
$productAttributes = array();
foreach ($result AS $productAttribute)
$productAttributes[$productAttribute['id_product_attribute']][] = $productAttribute['id_attribute'];
foreach ($productAttributes AS $key => $productAttribute)
if (sizeof($productAttribute) == sizeof($attributesList))
{
$diff = false;
for ($i = 0; $diff == false AND isset($productAttribute[$i]); $i++)
if (!in_array($productAttribute[$i], $attributesList) OR $key == $currentProductAttribute)
$diff = true;
if (!$diff)
return true;
}
return false;
}
Ok turns out I can’t comment this code in Stackoverflow without it all going to formatting hell. So my understanding is this:
1) Get the Data
2) If the dataset is empty return false
3) New array called productAttributes
4) Loop through the dataset and populate the array
5) Not sure what this last section is doing, the section beginning ‘foreach’ is unclear to me.
Any tips appreciated. Incidentally, C# is my preferred language and the one I understand best.
This function, as it is named, is only supposed to check if a particular attribute list of a product exists and is the same as the one provided in the first parameter.
The last part is confusing, but it’s iterating through all the attributes to see if any of the retrieved attributes are the same as the ones provided.
in a loop iteration:
The function isn’t very optimal, btw.