PHP - What's this code doing exactly?
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:
Other Question
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 u开发者_StackOverflow社区nderstanding 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:
- it first tries to see if the provided attribute list is the same length as the one coming from the database - if not, it just goes to the next attribute list in the for loop
- if the length of the lists is the same, then it will go through each attribute in the attribute list and see if there's something different amongst any of the elements.
The function isn't very optimal, btw.
精彩评论