duanpa1980 2011-02-21 14:41
浏览 21
已采纳

PHP - 这段代码到底在做什么?

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 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.

  • 写回答

1条回答 默认 最新

  • douweng1904 2011-02-21 14:53
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用