需要帮助配对数据库值


Need Help Pairing Database Values

我正在开发一个基于Joomla的CMS网站,目前我在弄清楚如何配对数据库值时遇到了一些问题。我昨天发布了这个问题,但删除了它,因为我认为这个问题已经解决了,而且这个问题的结构很糟糕。希望我能更好地表达我正在努力实现的目标。如果有必要的话,我愿意把它发布到开发网站上进行审查(假设这里允许)。

话虽如此,我正在查询四个表。这四个表是项目、附件、标签,然后是另一个将标签ID与相关项目ID配对的表。

您可以在下面看到这些查询:

        // Get all Items associated with this category page
    // ID of current  category
    $current_cat = $this->category->id;
    // Product IDs array
    $product_ids_array = array();
    // Product Names Array
    $item_names_array = array();
    // Product Descriptions Array
    $item_descriptions_array = array();
    $db = JFactory::getDbo();
    $query = $db->getQuery( true );
    $query->select($db->quoteName(array( 'title', 'introtext', 'id' )));
    $query->from( $db->quoteName( '#__k2_items' ) );
    $query->where( $db->quoteName( 'catid' )." = " .$current_cat );
    $db->setQuery( $query );
    $row = $db->loadObjectList();
    // Store Titles, Descriptions and IDs in arrays
    foreach ($row as $value)
    {
        $item_names_array[] = $value->title;
        $item_descriptions_array[] = $value->introtext;
        $product_ids_array[] = $value->id;
    };
// Now we're going to get the IDs of the tags associated with the items
    // Create comma seperated list of product ids
    $product_ids = implode(',', $product_ids_array);
    // Tag IDs Array
    $tag_IDs_array = array();
    $tag_itemIDs_array = array();
    $db = JFactory::getDbo();
    $query = $db->getQuery( true );
    $query->select($db->quoteName(array( 'tagID', 'itemID' )));
    $query->from( $db->quoteName( '#__k2_tags_xref' ) );
    $query->where($db->quoteName('itemID') . ' IN (' . $product_ids . ' )'  );
    $db->setQuery( $query );
    $row = $db->loadObjectList();
    // Store tag IDs and item IDs
    foreach ($row as $value)
    {
        $tag_IDs_array[] = $value->tagID;
        $tag_itemIDs_array[] = $value->itemID;
    };
// Now we're going to get the names of the tags
    // Create comma seperated list of tag ids
    $tag_IDs = implode(',', $tag_IDs_array );
    // Tag Names Array
    $tag_names_array = array();
    $db = JFactory::getDbo();
    $query = $db->getQuery( true );
    $query->select($db->quoteName(array( 'name' )));
    $query->from( $db->quoteName( '#__k2_tags' ) );
    $query->where($db->quoteName('id') . ' IN (' . $tag_IDs . ' )'  );
    $db->setQuery( $query );
    $row = $db->loadObjectList();
    // Store tag names
    foreach ($row as $value)
    {
        $tag_names_array[] = $value->name;
    };
    // Now we're going to get the attachments
    // Attachments Arrays
    $attachment_itemID_array = array();
    $attachment_id_array = array();
    $attachment_filename_array = array();
    $attachment_title_array = array();
    $attachment_title_attr_array = array();
    $db = JFactory::getDbo();
    $query = $db->getQuery( true );
    $query->select($db->quoteName(array( 'id', 'itemID', 'filename', 'title', 'titleAttribute' )));
    $query->from( $db->quoteName( '#__k2_attachments' ) );
    $query->where($db->quoteName('itemID') . ' IN (' . $product_ids . ' )'  );
    $db->setQuery( $query );
    $row = $db->loadObjectList();
    // Store tag names
    foreach ($row as $value)
    {
        $attachment_itemID_array[] = $value->itemID;
        $attachment_id_array[] = $value->id;
        $attachment_filename_array[] = $value->filename;
        $attachment_title_array[] = $value->title;
        $attachment_title_attr_array[] = $value->titleAttribute;
    };
        $db = JFactory::getDbo();
    $query = $db->getQuery( true );
    $query->select($db->quoteName(array( 'title', 'introtext', 'id' )));
    $query->from( $db->quoteName( '#__k2_items' ) );
    $query->where($db->quoteName('alias') . ' LIKE '. $db->quote('acid-%') . ' AND ' . $db->quoteName('catid'). ' = ' . $current_cat . ' AND published = 1'  );
    $db->setQuery( $query );
    $row = $db->loadObjectList();
    foreach ($row as $value) {
        $a = 0;
        $a++;
        echo $value->title . '<br/>';
        echo $value->introtext . '<br/>';
        echo $value->id . '<br/>';
    }

正如您所看到的,最后一块代码是对数据库的最后一个查询,我在其中使用通配符语句执行SQL Select。我希望这是动态的。

使其动态化的想法是,项目具有与其名称前缀匹配的标记。一个例子是,我有一个名为"酸腐蚀抑制剂"的标签,而我有一种别名为"酸-腐蚀抑制剂-ai-136"的产品。别名是我可以从数据库中检索到的东西。

无论如何,问题不在于检索数据。问题是,一旦我有了数据,我怎么才能智能地将数据配对在一起?正如你从我在每个实例中的查询中看到的那样,我提取了itemID,所以我有一些东西可以比较,但从那时起,我就很失落了。

我本质上是在尝试将项目与其关联的标签(每个项目只有一个相应的标签)以及关联的附件进行匹配。

我希望这是清楚的,如果不是,我会回来进一步澄清并回答任何问题。任何帮助都将不胜感激。

谢谢。

更新#1

我想知道in_array()是否是一个好的起点。至少我可以检查itemID是否存在。大致如下:

       if( in_array( $value->id, $attachment_itemID_array ) ) {
            echo 'match';
        }

我不确定这是否是一种确保正确附件与正确物品链接的防弹方式,等等。我想这不是,但希望这是一个开始。

更新#2

我开始认为(在某些情况下)在返回查询值时只使用关联数组会更好。我想这会让确保事情正确匹配变得更容易。

更新#3

希望有人觉得这个有用。我正试图访问这个怪物:

Array ( [0] => stdClass Object ( [id] => 2 [itemID] => 5 [filename] => Flotek-logo_1.png [title] => Flotek-logo_1.png [titleAttribute] => Flotek-logo_1.png ) [1] => stdClass Object ( [id] => 3 [itemID] => 6 [filename] => Logo_Court_Guru_Gray.png [title] => Logo_Court_Guru_Gray.png [titleAttribute] => Logo_Court_Guru_Gray.png ) [2] => stdClass Object ( [id] => 5 [itemID] => 4 [filename] => Icon_Search.png [title] => Icon_Search.png [titleAttribute] => Icon_Search.png ) ) 

因此,我可以使用它来检查当前项ID是否与数组中的项ID匹配,但在使用$array['itemID']行的内容时,我得到了一个未定义索引的错误;很明显,我处理这件事的方式不对。为了找到一些答案,我找到了几个但没有任何答案,这些答案对某些情况下完全不同。

好的,看起来像:

$attachmentRow[1]->itemID

是答案。现在我想知道是否有更好的方法来访问它,因为我无法知道对象的索引。至少,我宁愿只梳理关联数组中的每个对象。

更新#4

了解如何使用json_decode 在一定程度上简化阵列

$attachmentResult = json_decode(json_encode($attachmentRow),true);

导致

Array ( [0] => Array ( [id] => 2 [itemID] => 5 [filename] => Flotek-logo_1.png [title] => Flotek-logo_1.png [titleAttribute] => Flotek-logo_1.png ) [1] => Array ( [id] => 3 [itemID] => 6 [filename] => Logo_Court_Guru_Gray.png [title] => Logo_Court_Guru_Gray.png [titleAttribute] => Logo_Court_Guru_Gray.png ) [2] => Array ( [id] => 5 [itemID] => 4 [filename] => Icon_Search.png [title] => Icon_Search.png [titleAttribute] => Icon_Search.png ) ) 

当使用的长行时仍然得到索引未定义的错误

 echo $attachmentResult['itemID'];

仍在努力正确地访问关联数组。

没关系。这是正确的方式。

echo $attachmentResult[0]['itemID'];

更新#5

好吧,我想我差不多明白了。我在这里找到了一个非常有用的解决方案:如何按关键字搜索=>PHP中多维数组中的值-搜索多维数组非常方便

更新查询以循环通过项目/产品

        $db = JFactory::getDbo();
    $query = $db->getQuery( true );
    $query->select($db->quoteName(array( 'title', 'introtext', 'id' )));
    $query->from( $db->quoteName( '#__k2_items' ) );
    $query->where($db->quoteName('alias') . ' LIKE '. $db->quote('acid-%') . ' AND ' . $db->quoteName('catid'). ' = ' . $current_cat . ' AND published = 1'  );
    $db->setQuery( $query );
    $row = $db->loadObjectList();
    foreach ($row as $value) {
        $itemID = $value->id;
        $search_result = (search($attachmentResult, 'itemID', $itemID));
        if($search_result) {
            $db = JFactory::getDbo();
            $query = $db->getQuery( true );
            $query->select($db->quoteName(array( 'filename' )));
            $query->from( $db->quoteName( '#__k2_attachments' ) );
            $query->where( $db->quoteName( 'itemID' )." = " .$itemID );
            $db->setQuery( $query );
            $attachmentRow = $db->loadObjectList();
             foreach ($attachmentRow as $attachmentValue) {
                 echo $attachmentValue->filename;
             }
        }
        echo $value->introtext . '<br/>';
    }

好的,我开始工作了。我不确定这是否是最优雅的解决方案。如果有人想提出改进意见,我会洗耳恭听的。希望这能在将来帮助到有类似问题的人。

我的最终项目/产品循环查询:

    // Get all Items associated with this category page
    // ID of current  category
    $current_cat = $this->category->id;
    // Product IDs array
    $product_ids_array = array();
    $db = JFactory::getDbo();
    $query = $db->getQuery( true );
    $query->select($db->quoteName(array('id' )));
    $query->from( $db->quoteName( '#__k2_items' ) );
    $query->where( $db->quoteName( 'catid' )." = " .$current_cat . ' AND published = 1' );
    $db->setQuery( $query );
    $row = $db->loadObjectList();
    // Store Titles, Descriptions and IDs in arrays
    foreach ($row as $value)
    {
        $product_ids_array[] = $value->id;
    };
    // Now we're going to get the IDs of the tags associated with the items
    // Create comma seperated list of product ids
    $product_ids = implode(',', $product_ids_array);
    // Tag IDs Array
    $tag_IDs_array = array();
    $db = JFactory::getDbo();
    $query = $db->getQuery( true );
    $query->select($db->quoteName(array( 'tagID', 'itemID' )));
    $query->from( $db->quoteName( '#__k2_tags_xref' ) );
    $query->where($db->quoteName('itemID') . ' IN (' . $product_ids . ' )'  );
    $db->setQuery( $query );
    $row = $db->loadObjectList();
    $tagsRow = $db->loadObjectList();
    $tagsResult = array();
    $tagsResult = json_decode(json_encode($tagsRow),true);
    // Store tag IDs and item IDs
    foreach ($row as $value)
    {
        $tag_IDs_array[] = $value->tagID;     
    };
    // Now we're going to get the names of the tags
    // Create comma seperated list of tag ids
    $tag_IDs = implode(',', $tag_IDs_array );
    // Tag Names Array
    $tag_names_array = array();
    $db = JFactory::getDbo();
    $query = $db->getQuery( true );
    $query->select($db->quoteName(array( 'name' )));
    $query->from( $db->quoteName( '#__k2_tags' ) );
    $query->where($db->quoteName('id') . ' IN (' . $tag_IDs . ' )'  );
    $db->setQuery( $query );
    $row = $db->loadObjectList();
    // Store tag names
    foreach ($row as $value)
    {
        $tag_names_array[] = $value->name;
    };
    // Now we're going to get the attachments
    // Attachments Arrays
    $db = JFactory::getDbo();
    $query = $db->getQuery( true );
    $query->select($db->quoteName(array( 'id', 'itemID', 'filename', 'title', 'titleAttribute' )));
    $query->from( $db->quoteName( '#__k2_attachments' ) );
    $query->where($db->quoteName('itemID') . ' IN (' . $product_ids . ' )'  );
    $db->setQuery( $query );
    $attachmentRow = $db->loadObjectList();
    $attachmentResult = array();
    $attachmentResult = json_decode(json_encode($attachmentRow),true); 
    // Function to search multidimensional arrays
    function search($array, $key, $value)
    {
        $results = array();
        if (is_array($array)) {
            if (isset($array[$key]) && $array[$key] == $value) {
                $results[] = $array;
            }
            foreach ($array as $subarray) {
                $results = array_merge($results, search($subarray, $key, $value));
            }
        }
        return $results;
    }

    // Now we're going to create our product loop

    // Get Tag Names
    foreach( $tag_names_array as $display_tag_name ) {
        // Unformatted Tag Name - this is the one that will be displayed on the front end
        $unformatted_display_tag_name = $display_tag_name;
        // Convert Tag Name White Spaces to Dashes
        $display_tag_name = preg_replace("/['s_]/", "-", $display_tag_name);
        // Lower Case Tag Name
        $display_tag_name = strtolower($display_tag_name);
        switch ( $display_tag_name == $display_tag_name ) {
            case $display_tag_name: 
            $db = JFactory::getDbo();
            $query = $db->getQuery( true );
            $query->select($db->quoteName(array( 'title', 'introtext', 'id' )));
            $query->from( $db->quoteName( '#__k2_items' ) );
            $query->where($db->quoteName('alias') . ' LIKE '. $db->quote($display_tag_name.'-%') . ' AND ' . $db->quoteName('catid'). ' = ' . $current_cat . ' AND published = 1'  );
            $db->setQuery( $query );
            $row = $db->loadObjectList();
            // Start Row
            echo '<div class="row">';
            // Start 12 Column
            echo '<div class="col-lg-12">';
            // Start Row
            echo '<div class="row">';
            // Start Item Container
            echo '<section class="item-container">';
            // Display Tag Name 
            echo '<div class="col-lg-12"><section class="tag-name"><a href="#">' . $unformatted_display_tag_name . '</a></section></div>';

            foreach ($row as $value) {
                // Start Column 6
                echo '<div class="col-lg-6 is-hidden">';
                // Store ID of item
                $itemID = $value->id;
                // Search attachmentResult array
                $attachment_search_result = (search($attachmentResult, 'itemID', $itemID));
                // Check to see if there are any associated attachments - display attachment is present
                if($attachment_search_result) {
                    $db = JFactory::getDbo();
                    $query = $db->getQuery( true );
                    $query->select($db->quoteName(array( 'filename' )));
                    $query->from( $db->quoteName( '#__k2_attachments' ) );
                    $query->where( $db->quoteName( 'itemID' )." = " .$itemID );
                    $db->setQuery( $query );
                    $attachmentRow = $db->loadObjectList();
                     foreach ($attachmentRow as $attachmentValue) {
                         echo $attachmentValue->filename . '<br/>';
                     }
                }
                // Display Item Title
                echo '<h5>' .$value->title. '</h5>';
                // Display Item Text
                echo '<p>' .$value->introtext. '</p>';
                // End Column 6
                echo '</div>';
            }
            // Close Item Container
            echo '</section>';
            // Close Row
            echo '</div>';
            // Close 12 Column
            echo '</div>';
            // Close Row
            echo '</div>';
        }
    }
?>
<!-- /Display Category Items -->