douchuituo3032 2013-12-26 18:29
浏览 50
已采纳

使用PHP按类别排序MYSQL结果

Here's the thing. I'm trying to sort my Comics collection. So far, I have two tables, one for the titles and type of comics (ID, prefix, title, type) and another one with individual information for each issue (ID, title, volume, issue number, value).

First thing I do is associate the title from the issues with the appropriate titles from the title table.

I do something like that:

SELECT * FROM comics, comicstitles WHERE comics.comTitle = comicstitles.titID ORDER BY comicstitles.titTitle ASC

What I get is a neat bunch of rows that I organize, and I get results like:

<table class='simple-list'>
<thead>
    <tr>
        <th>Title</th>
        <th>Volume</th>
        <th>Issue</th>
        <th>Value</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td>Alpha Flight</td>
        <td>1</td>
        <td>1</td>
        <td>$4.00</td>
    </tr>
    <tr>
        <td>Alpha Flight</td>
        <td>1</td>
        <td>2</td>
        <td>$3.00</td>
    </tr>
    <tr>
        <td>Machine Man</td>
        <td>2</td>
        <td>1</td>
        <td>$2.00</td>
    </tr>
    <tr>
        <td>Wolverine</td>
        <td>1</td>
        <td>2</td>
        <td>$35.00</td>
    </tr>
    <tr>
        <td>Wolverine</td>
        <td>1</td>
        <td>1</td>
        <td>$60.00</td>
    </tr>
    <tr>
        <td>The X-Men</td>
        <td>1</td>
        <td>115</td>
        <td>$100.00</td>
    </tr>
    <tr>
        <td>The X-Men</td>
        <td>1</td>
        <td>116</td>
        <td>$100.00</td>
    </tr>
    <tr>
        <td>The X-Men</td>
        <td>1</td>
        <td>111</td>
        <td>$100.00</td>
    </tr>
    <tr>
        <td>The X-Men</td>
        <td>1</td>
        <td>114</td>
        <td>$100.00</td>
    </tr>
    <tr>
        <td>The X-Men</td>
        <td>1</td>
        <td>109</td>
        <td>$160.00</td>
    </tr>
    <tr>
        <td>The X-Men</td>
        <td>1</td>
        <td>112</td>
        <td>$100.00</td>
    </tr>
</tbody>
<tfoot>
    <tr>
        <td colspan='4'><h3>Total: $764.00</h3></td>
    </tr>
</tfoot>

Now, what I would like to do is a foreach, which would give me something like:

For each title, print the title once and all the individual issues underneath.

I would end up with something like:

TITLE
Issue number
Cover
Issue number
Cover

TITLE
Issue number
Cover

TITLE
Issue number
Cover
Issue number
Cover
Issue number
Cover
Issue number
Cover

I tried looking over and over, but I always end up on Wordpress, Joomla or Drupal sites.

And any other results is either WAY too abstract or uses numbers.

  • 写回答

4条回答 默认 最新

  • dowm41315 2013-12-26 18:39
    关注

    You can easily group the items visually by title by keeping track of the title and comparing it to the current one.

    Something like (simple example):

    // before your loop
    $title = '';
    
      // in your loop
      if ($row['title'] === $title) {
        // title already shown, do nothing or echo an &nbsp;
      } else {
        // new title, set and print it
        $title = $row['title'];
        echo htmlspecialchars($title);
      }
    

    You don't mention covers anywhere, so it's impossible to help with that until you've tried it, show the code and what the exact problem is.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊