简体   繁体   English

基于元素在PHP中组合SQL查询数组

[英]Combining SQL query Array in PHP based on element

I have made a favorites function on my site that someone can choose different items that go together and save it as a favorite package in their account. 我在网站上设置了“收藏夹”功能,使某人可以选择不同的物品,并将其另存为收藏夹。 All items are saved in a favorite table with a favorite ID number. 所有项目均保存在具有收藏夹ID号的收藏夹表中。 Anything that has the same ID number go together. 具有相同ID号的任何东西都一起去。 This will save under the customer that is logged in. 这将保存在已登录的客户下。

I am trying to import this array into a PHP page and display the items graphicly in a lightbox type look, but have been running into a lot of trouble combining them together to display them properly. 我正在尝试将此数组导入PHP页面并以灯箱类型外观以图形方式显示项目,但将它们组合在一起以正确显示它们时遇到了很多麻烦。

The array and php code are below. 数组和php代码如下。 The array is generated from a SQL query. 该数组是从SQL查询生成的。 The items of array [0] and [1] need to be combined together in such a way that I can show the images from each one together (the images actually go on top of each other to make it look like a package. 数组[0]和[1]的各项需要组合在一起,以便我可以一起显示彼此的图像(图像实际上彼此叠放在一起,使其看起来像一个包装。

Here is a link to the page so you get an idea: https://www.viva-cards.com/products/package-designer you will not be able to see the favorite function unless you log-in though 这是页面的链接,因此您有个好主意: https : //www.viva-cards.com/products/package-designer您将无法看到喜欢的功能,除非您登录

I basically need to combine the same fav_seq ids together, but still be able to differentiate itm_id, offer_id, inv_type_id, , inv_thumbnail_url and offer_description, as they have different images and such that go with them. 我基本上需要将相同的fav_seq id组合在一起,但仍然能够区分itm_id,offer_id,inv_type_id,inv_thumbnail_url和offer_description,因为它们具有不同的图像,并且它们具有相似的含义。 I have been hitting my head to a brick wall on this. 我一直在撞墙。 Is there a way to do this on the php page, or do I have to do it at the SQL query stage? 有没有办法在php页面上执行此操作,还是必须在SQL查询阶段执行此操作? What am I doing wrong? 我究竟做错了什么?

The following is the code of the array: 以下是数组的代码:

[0] => Array
    (
        [entity_id] => 6
        [itm_id] => 81
        [fav_seq] => 1
        [offer_id] => 1
        [inv_css_tag] => 
        [inv_type_id] => TYPE1
        [inv_thumbnail_url] => image1-thumb.png
        [offer_description] => Offer1
    )

[1] => Array
    (
        [entity_id] => 6
        [itm_id] => 198
        [fav_seq] => 1
        [offer_id] => 1
        [inv_css_tag] => csstag
        [inv_type_id] => TYPE2
        [inv_thumbnail_url] => image2-thumb.jpg
        [offer_description] => Offer2
    )

[2] => Array
    (
        [entity_id] => 6
        [itm_id] => 810
        [fav_seq] => 2
        [offer_id] => 4
        [inv_css_tag] => 
        [inv_type_id] => TYPE1
        [inv_thumbnail_url] => image3-thumb.png
        [offer_description] => Offer4
    )

[3] => Array
    (
        [entity_id] => 6
        [itm_id] => 98
        [fav_seq] => 2
        [offer_id] => 4
        [inv_css_tag] => csstag
        [inv_type_id] => TYPE2
        [inv_thumbnail_url] => image4-thumb.jpg
        [offer_description] => Offer4
    )

This is the code I am using on the php page to generate the display: 这是我在php页面上用来生成显示的代码:

<div>
    <h2>Favorites Saved</h2>

    <?php 
    foreach($this->favorite_seq as $favorites){
        if(empty($favorites['fav_seq'])){ 
     // Nothing
    ?>
<?php }else {?>                 
        <div id="favoritesThumbnailWrapper"> <!-- For TYPE1 -->
     <div id="favoritesReferralThumb" class="cardPositionMiddle1">
         <img src="<?= $this->inventory_url . $favorites['inv_thumbnail_url'];?>">
     </div>

     <div id="favoritesPresentationThumb"> <!-- For TYPE2 -->
         <img src="<?= $this->inventory_url . $favorites['inv_thumbnail_url'];?>">
     </div>

     <div id="favoritesOffer"> <!-- For OFFER DESCRIPTION -->
         <span><?= $favorites['offer_description'];?></span>
     </div>

</div>
<?php
    }
}
?>

This may help: 这可能会有所帮助:

$favArray = array();
foreach($this->favorite_seq as $favorites){
    if(!empty($favorites['fav_seq'])){
        $favArray[$favorites['fav_seq']][] = $favorites;
    }
}
print_r($favArray);

Itterate on $favArray to display records then. 迭代$favArray以显示记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM