PHP MySQL将查询中的多行组合成一个带有1个相似列

时间:2015-08-19 15:24:50

标签: php mysql

因此,我们有一个用于历史记录的数据库,用于显示企业的开始和结束日期,关联人员,历史记录和相关的剪报。目前,当用户搜索时,例如(参见当前结果),Arnold& Dunn,2个结果返回,一个与附属人员为'Arnold',另一个与附属人员为'Dunn'。在Sql Query中的组返回第一个附属人员。我想知道你如何取x个名称的结果和基本上Concat在PHP中显示在一个公司结果下?

当前结果:

Arnold& Dunn Bicycle销售

附属人员:Edwin L. Arnold

成立年份:1886

年度休息日:1888年

产品/服务:体育用品商店

NAICS:451110

Arnold& Dunn Bicycle销售

附属人士:James R. Dunn

成立年份:1886

年度休息日:1888年

产品/服务:体育用品商店

NAICS:451110

预期结果:

Arnold& Dunn Bicycle销售

附属人员:Edwin L. Arnold,James R. Dunn

成立年份:1886

年度休息日:1888年

产品/服务:体育用品商店

NAICS:451110

PHP搜索/结果脚本:

<?php
require_once 'php/db.php';
$conn = dbConnect();
$OK = true;

if (isset($_GET['name'])) {
    $data = "%".$_GET['name']."%";
    $sql = 'SELECT *
                    FROM company
                    LEFT JOIN xref_ap_comp
                    ON company.CompId=xref_ap_comp.CompID
                    LEFT JOIN persons
                    ON xref_ap_comp.APID=persons.APID
                    LEFT JOIN xref_comp_prod2
                    ON company.CompId=xref_comp_prod2.CompID
                    LEFT JOIN product
                    ON xref_comp_prod2.NAICS=product.NAICS
                    LEFT JOIN product2
                    ON product.NAICSThreeDigitCode=product2.NAICSThreeDigitCode
                    LEFT JOIN product3
                    ON product2.NAICSRootCode=product3.NAICSRootCode
                    WHERE CompName like ? 
                    ORDER BY company.CompName';
    $stmt = $conn->prepare($sql);
    $results = $stmt->execute(array($data));
    $rows = $stmt->fetchAll();
    $error = $stmt->errorInfo();
}
if(empty($rows)) {
    echo "<h3>No Records Found</h3>";
}
else {  foreach ($rows as $row) {
        echo "<h3 class=\"ui-accordion-header ui-state-default ui-corner-all ui-accordion-icons\" role=\"tab\" id=\"ui-id-1\" aria-selected=\"false\" aria-expanded=\"false\" tabindex=\"0\">".$row['CompName']."<span class=\"ui-accordion-header-icon ui-icon ui-icon-triangle-1-e\"></span></h3>";
                    echo "<div>";
                    echo "<p>".$row['HistoricalNotes']."</p>";
                    echo "<ul>";
                    echo "<li>Affiliated Person: ".$row['APFirstName']." ".$row['APLastName']."</li>";
                    echo "<li>Year Established: ".$row['StartDate']."</li>";
                    echo "<li>Year Closed: ".$row['EndDate']."</li>";
                    echo "<li>Product/Service: ".$row['ProductCategory']."</li>";
                    echo "<li>NAICS: ".$row['NAICS']."</li>";
                    echo "</ul>";
                    echo "</div>";

    }
}

我猜测echo "<li>Affiliated Person: ".$row['APFirstName']." ".$row['APLastName']."</li>";中必须有一些计数对象才能收到逗号分隔的结果(附属人:Edwin L. Arnold,James R. Dunn)。如果有任何其他所需信息需要帮助解决此问题,请与我们联系。

1 个答案:

答案 0 :(得分:0)

一种解决方案是创建一个多维数组,其中人员按公司分组。

$companies = array();
foreach ( $rows as $row ) {
    // Group persons by company name
    $companies[$row['CompName']][$row['CompId']] = $row;    
}
foreach ( $companies as $company => $persons ) {
    echo "<h3 class=\"ui-accordion-header ui-state-default ui-corner-all ui-accordion-icons\" role=\"tab\" id=\"ui-id-1\" aria-selected=\"false\" aria-expanded=\"false\" tabindex=\"0\">".$company."<span class=\"ui-accordion-header-icon ui-icon ui-icon-triangle-1-e\"></span></h3>";   
    foreach ( $persons as $person ) {
        echo "<div>";
        echo "<p>".$person['HistoricalNotes']."</p>";
        echo "<ul>";
        echo "<li>Affiliated Person: ".$person['APFirstName']." ".$person['APLastName']."</li>";
        echo "<li>Year Established: ".$person['StartDate']."</li>";
        echo "<li>Year Closed: ".$person['EndDate']."</li>";
        echo "<li>Product/Service: ".$person['ProductCategory']."</li>";
        echo "<li>NAICS: ".$person['NAICS']."</li>";
        echo "</ul>";
        echo "</div>";
    }
}