PHP MySQL Combine Multiple Rows from query into one result with 1 similar column -
so have database used history purposes display businesses start , end dates, affiliated persons, historical notes, , associated newspaper clippings. currently, when user searches, example (see current results), arnold & dunn, 2 results return, 1 affiliated person 'arnold', , other affiliated person 'dunn". group in sql query on returns first affiliated person. wondering how take x number of results names , concat them in php show under 1 company result?
current results:
arnold & dunn bicycle sales
affiliated person: edwin l. arnold
year established: 1886
year closed: 1888
product/service: sporting goods stores
naics: 451110
arnold & dunn bicycle sales
affiliated person: james r. dunn
year established: 1886
year closed: 1888
product/service: sporting goods stores
naics: 451110
expected results:
arnold & dunn bicycle sales
affiliated person: edwin l. arnold, james r. dunn
year established: 1886
year closed: 1888
product/service: sporting goods stores
naics: 451110
php search/results script:
<?php require_once 'php/db.php'; $conn = dbconnect(); $ok = true; if (isset($_get['name'])) { $data = "%".$_get['name']."%"; $sql = 'select * 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 compname ? order 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 $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>"; } }
i guessing in echo "<li>affiliated person: ".$row['apfirstname']." ".$row['aplastname']."</li>";
there have count object in order recieve comma deliniated results (affiliated person: edwin l. arnold, james r. dunn). please let me know if there other required information needed on issue.
one solution create multi-dimensional array persons grouped company.
$companies = array(); foreach ( $rows $row ) { // group persons company name $companies[$row['compname']][$row['compid']] = $row; } foreach ( $companies $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 $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>"; } }
Comments
Post a Comment