I’m trying to build a table of results from an array. I currently have results outputting like this:
ID VALUE EXTRA
--------------------------------------
| 1 | Value 1 | Extra 1 |
|-----|---------------------|----------|
| 1 | Value 2 | Extra 2 |
|-----|---------------------|----------|
| 2 | Value 3 | Some 1 |
|-----|---------------------|----------|
| 3 | Value 4 | Some 2 |
|-----|---------------------|----------|
| 3 | Value 5 | Nothing |
--------------------------------------
Note the repeating ID value. What I’d like to do is build a loop inside my current loop that will not display duplicate IDs. Something like this:
ID VALUE EXTRA
--------------------------------------
| 1 | Value 1 | Extra 1 |
| |---------------------|----------|
| | Value 2 | Extra 2 |
|-----|---------------------|----------|
| 2 | Value 3 | Some 1 |
|-----|---------------------|----------|
| 3 | Value 4 | Some 2 |
| |---------------------|----------|
| | Value 5 | Nothing |
--------------------------------------
Here’s my current code, simplified:
<?php
$i=0;
while ($i < $mynum) {
$f1=mysql_result($myresult,$i,"tableID");
$f2=mysql_result($myresult,$i,"values");
$f3=mysql_result($myresult,$i,"extra");
?>
<tr>
<td><?php echo $f1; ?></td>
<td><?php echo $f2; ?></td>
<td><?php echo $f3; ?></td>
</tr>
<?php
$i++;
}
?>
Is there a way to build this table dynamically in the way I want? Or should I rethink my strategy?
Here a way to do it from the code you provided
But that is assuming that the
$f1are ordered.