简体   繁体   English

将多条记录合并到一个输出

[英]Merge multiple records to one output

I got a plugin that for some reason saves the data in really annoying places (3 different tables under the same names etc).我得到了一个插件,它出于某种原因将数据保存在非常烦人的地方(3 个不同的同名表等)。

I added 4 fields to the cms which I want to echo in an element.我向要在元素中回显的 cms 添加了 4 个字段。 Function, name, text and an image.功能、名称、文本和图像。 The problem is all 4 are stored under the same name in the database data .问题是所有4个列在数据库中的同名存储data The only difference between them is their field_id .它们之间的唯一区别是它们的field_id

在此处输入图片说明

So I got the correct query which gets all the data I need, my query is the following:所以我得到了正确的查询,它获取了我需要的所有数据,我的查询如下:

SELECT ct . * , fe . * , cn . * , dt . * 
FROM web_content ct
INNER JOIN web_fieldsandfilters_elements fe ON fe.item_id = ct.id
INNER JOIN web_fieldsandfilters_connections cn ON cn.element_id = fe.id
INNER JOIN web_fieldsandfilters_data dt ON dt.element_id = fe.id
WHERE ct.id
IN (
'46'
)

The above query returns the same row multiple times only with data being different.上面的查询多次返回同一行,只是数据不同。 So when I loop the output, it shows the element multiple times instead of just one time.因此,当我循环输出时,它会多次显示该元素,而不仅仅是一次。

How can I merge the output together?如何将输出合并在一起?

My entire loop with the query as I have it now:我现在拥有的整个查询循环:

<?
//Query om referentie op de dienstenpage te laten zien
$referentie                         = "
SELECT ct.*, fe.*, cn.*, dt.*
FROM web_content ct
INNER JOIN web_fieldsandfilters_elements fe on fe.item_id = ct.id
INNER JOIN web_fieldsandfilters_connections cn on cn.element_id = fe.id
INNER JOIN web_fieldsandfilters_data dt on dt.element_id = fe.id
WHERE ct.id IN ('".$contentcr[0]['id']."')";
$referentiecon                  = $conn->query($referentie);
$referentiecr                   = array();
while ($referentiecr[]  = $referentiecon->fetch_array());
?>
<div class="container">
    <div class="row">
        <div class="col-md-12 wow fadeInUp animated animated">
            <?
            foreach($referentiecr as $referentietext){
                if($referentietext['field_id'] != ''){
                    if($referentietext['field_id'] == '8'){
                        $referentie_images = $referentietext['data'];
                        $ref_pictures = json_decode($referentie_images);

                        if($ref_pictures->{'image'} != ''){
                            $image = 'cms/'.$ref_pictures->{'image'};
                        }else{
                            $image .= '';
                        }
                    }
                    if($referentietext['field_id'] == '5'){
                        $naam = $referentietext['data'];
                    }

                    if($referentietext['field_id'] == '6'){
                        $text = $referentietext['data'];
                    }

                    if($referentietext['field_id'] == '7'){
                        $functie = $referentietext['data'];
                    }
                    $refoverzicht .= '
                    <div class="col-md-4">
                        <img src="'.$image.'" style="max-width:100%;">
                    </div>
                    <div class="col-md-8">
                        <blockquote>
                          <p style="font-size: 14px;">
                            '.$text.'
                            <br>
                            <em>– '.$naam.' – '.$functie.'</em>
                          </p>
                        </blockquote>
                    </div>';
                    }
                }
    echo $refoverzicht;
?>

Resulting in the following:结果如下:

在此处输入图片说明

I cannot think of a way at the query level unless you specifically select the columns and give them a alias.除非您专门选择列并给它们一个别名,否则我想不出查询级别的方法。 Eg select ct.field_id as ct_field_id ....例如选择 ct.field_id 作为 ct_field_id ....

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

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