1

I have a database table (built from a query) that lists member (called Troop), events, start date, and end date. A member might be listed more than once in the table as they are associated with more than one event.

I want to build an HTML table that lists the member's name followed by each event they are associated with before going on to the next member, which may be a few items down in the array.

I was thinking about using aforeach() statement but could not get it to work. Any pointers on how I might accomplish this? Thanks!

while(list($Event, $Troop, $StartDate, $EndDate) = mysqli_fetch_array($result)) {
  $return=$return . "<table>";
  $return=$return . "<tr>";
  $return=$return . "<th colspan=3>" . $Troop . "</th>";
  $return=$return . "</tr>";
  $return=$return . "<tr>";
    $return=$return . "<th>Event</th>";
    $return=$return . "<th>Start Date</th>";
    $return=$return . "<th>End Date</th>";
  $return=$return . "</tr>";
        //foreach ($Troop as $thisTroop) {
        $return=$return . "<tr>";
        $return=$return . "<td>" . $Event . "</td>";
        $return=$return . "<td>" . $StartDate . "</td>";
        $return=$return . "<td>" . $EndDate . "</td>";
        $return=$return . "</tr>";
        //}
  $return=$return . "</tr>";
  $return=$return . "</table>";
}
3
  • What error are you getting? Commented Jul 26, 2014 at 23:53
  • Would it work if I built two lists, one as list_a($Troop) and the other list_b($Event, $StartDate, $EndDate)? Commented Jul 26, 2014 at 23:54
  • No error. That code works as it is, however, it lists the Troop each time instead of listing the Troop once followed by their events. Commented Jul 26, 2014 at 23:55

2 Answers 2

1

Replace your code with this. It's necesary that the sql result is sorted by Troop.

$return = '';
$currentTroop = '';

while(list($Event, $Troop, $StartDate, $EndDate) = mysqli_fetch_array($result)) {

    if ($currentTroop != $Troop) {
        $return .= "<table>"
        ."<tr>";
        ."<th colspan=3>" . $Troop . "</th>"
        ."</tr>"
        ."<tr>"
        ."<th>Event</th>"
        ."<th>Start Date</th>"
        ."<th>End Date</th>"
        ."</tr>";
    }

    $return .= "<tr>"
    ."<td>".$Event."</td>"
    ."<td>".$StartDate."</td>"
    ."<td>".$EndDate."</td>"
    ."</tr>";

    if ($currentTroop != $Troop) {
        $return .= "</tr>"
        ."</table>";
        $currentTroop = $Troop;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nice! I started experimenting with exactly that, but you solution is much more clean than mine. Thanks!
0

Create a multi-dimensional array with keys of Troops and group every records with the same Troop. And then use foreach() to loop the array and print out the information.

Group array by subarray values

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.