fork download
  1. <?php
  2. $testdata = array();
  3. for ($i=0; $i<10; $i++) {
  4. $testdata[] = array('a' => intval($i/3), 'b' => $i);
  5. }
  6.  
  7. $postArray = array();
  8. $rowspan = 1;
  9. // $post = mysql_fetch_assoc($posts);
  10. $post = array_pop($testdata);
  11. while ($post):
  12. // $pre = mysql_fetch_assoc($posts);
  13. $pre = array_pop($testdata);
  14. if (isset($pre) && $pre['a'] == $post['a']) {
  15. $post['rowspan'] = 0;
  16. $rowspan++;
  17. } else {
  18. $post['rowspan'] = $rowspan;
  19. $rowspan = 1;
  20. }
  21. $postArray[] = $post;
  22. $post = $pre;
  23. endwhile;
  24. ?>
  25. <table>
  26. <thead>
  27. <tr>
  28. <th>名前</th>
  29. <th>内容</th>
  30. </tr>
  31. </thead>
  32. <tbody>
  33. <?php foreach (array_reverse($postArray) as $post) { ?>
  34. <tr>
  35. <?php if ($post['rowspan'] !== 0){ ?>
  36. <?php if ($post['rowspan'] === 1){ ?>
  37. <td>
  38. <?php } else { ?>
  39. <td rowspan="<?php echo $post['rowspan']; ?>">
  40. <?php } ?>
  41. <?php echo $post['a']; ?></td>
  42. <?php } ?>
  43. <td><?php echo $post['b']; ?>
  44. </td>
  45. </tr>
  46. <?php } ?>
  47. </tbody>
  48. </table>
  49.  
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
<table>
<thead>
<tr>
<th>名前</th>
<th>内容</th>
</tr>
</thead>
<tbody>
<tr>
  <td rowspan="3">
  0</td>
<td>0</td>
</tr>
<tr>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
  <td rowspan="3">
  1</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
</tr>
<tr>
<td>5</td>
</tr>
<tr>
  <td rowspan="3">
  2</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
</tr>
<tr>
<td>8</td>
</tr>
<tr>
  <td>
  3</td>
<td>9</td>
</tr>
</tbody>
</table>