Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to show one column value only once but another column value multiple times when column value is the same

idk if it’s possible, but I’m going to ask anyway.
I have a MYSQL database with a column called encounter, mode & encounter_id.

example: if the encounter_id value = 4, the encounter value = "Crota" (exists more than once) & the mode value is both "Normal" & "Master", is it possible to show "Crota" only once, but show both "Normal" & "Master" in an echo?

Query:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$ce = $database->pdo->query("SELECT * FROM `raid_list` WHERE `raid_id` = 13 ORDER BY `encounter_id` ASC")->fetchAll(PDO::FETCH_ASSOC);

PHP:

<?php foreach($ce as $data) {
      echo '<div class="encounter">';
      echo '<p>' . $data['encounter'] . '</p>';
      echo '<p>(' . $data['mode'][0] . ')</p>';
      echo '</div>';
} ?>

If it’s not possible, then that’s okay. I just want to know if it’s possible or not.

EDIT: Here’s the database image
Database Image

>Solution :

Just restructure the array into a format which can be iterated easily. I think you are looking for something like this.

<?php
$results = array_reduce($ce, function($carry, $current) {
  $modes = $carry[$current['encounter']]['modes'] ?? [];
  array_push($modes, $current['mode']);
  $carry[$current['encounter']] = ['modes' => array_unique($modes)];
  return $carry;
});

foreach($results as $encounter => $encounterData) {
  echo '<div class="encounter">';
  echo '<p>' . $encounter . '</p>';
  echo '<p>(' . implode(', ', $encounterData['modes']) . ')</p>';
  echo '</div>';
}
?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading