-2

I have this code

<?php
$query1 = "SELECT users.id,
testtable_result.id as result_id,
testtable_result.user_id,
testtable_result.cad_id,
testtable_result.medication,
testtable_result.color,
testtable_category.category_id,
testtable_category.name
FROM users
INNER JOIN testtable_result
ON testtable_result.user_id=users.id
INNER JOIN testtable_category
ON testtable_result.cad_id=testtable_category.category_id
WHERE users.id = '{$userID}' AND color LIKE '%blue%';";
$result1 = query($query1);
confirm($result1);
if (row_count($result1) > 0) :
while($row1 = fetch_Array($result1)) {
  $medication = $row1['medication'];
  $data[] = $row1;
}
foreach ($data as $value) {
$categoryname = $value['name'];
$medication = $value['medication'];
$arrmaincatname = explode('(', rtrim($categoryname, ')'));
if($categoryname == "Saba"){
$datacat = $arrmaincatname[0];
$medicacat = $medication; ?>
In <?php echo $datacat; ?> category -- [<?php echo $medicacat ?>]
<?php
}} ?>

it shows result like this
In Saba category -- [ProAir RespiClick®] In Saba category -- [Proventil® HFA]

I want to remove all the duplicate results and want to show it like this.
In Saba category -- [ProAir RespiClick®, Proventil® HFA]

Dharman
  • 30,962
  • 25
  • 85
  • 135
Waleed Afridi
  • 107
  • 2
  • 10

1 Answers1

0

You can do using a foreach to group the results, and a second to show print the group as you wish.

<?php
$categories = [];
foreach ($data as $value) {
    $categoryname = $value['name'];
    $medication = $value['medication'];
    $arrmaincatname = explode('(', rtrim($categoryname, ')'));
    if ($categoryname == "Saba") {
        $categories[$arrmaincatname[0]][] = $medication;
    }
}
foreach ($categories as $categoryName => $category) {?>
    In <?php echo $categoryName; ?> category -- [<?php echo implode(', ', $category) ?>]
    <?php
}
Sena
  • 916
  • 5
  • 13