草庐IT

php - 显示每种颜色的值

coder 2024-04-20 原文

我在从数据库打印值时遇到问题。

项目表

ITEM | COLOR | MATERIAL | DIMENSIONS | CATEGORY | QUANTITY
- 01    33      05           111         12         1000.00
- 02    33      07           125         18         200.00
- 03    33      11           156         18         254.00
- 04    56      15           25          66         113.00
- 05    66      05           11          33         521.00

我正在尝试打印表格中的值(针对每种颜色打印 Material 尺寸类别) 所以输出将是:

COLOR - > 33
MATERIAL | DIMENSION | CATEGORY  | QUANTITY
05          111          12          1000.00
07          125          18          200.00
11          156          18          254.00

COLOR - > 56
MATERIAL | DIMENSION | CATEGORY  | QUANTITY
15          25           66          113.00

COLOR - > 66
MATERIAL | DIMENSION | CATEGORY  | QUANTITY
05          11           33          521.00

我正在使用查询

$query = "SELECT a.itemnb, b.colorname, c.materialname, d.categoryname, sum(a.quantity) as quantity 
FROM dbo_items a
JOIN dbo_color b
ON a.color=b.colorid
JOIN dbo_material c
on a.material=c.material
JOIN dbo_category
on a.category=d.categoryid
GROUP BY b.colorname, c.materialname, d.categoryname, ";

我正在使用 PDO。

$q=$conn->query($query);

现在我可以获取表中的所有值,但这并不是我真正想要的。

<table class="table table-bordered table-striped">
  <thead>
    <tr class="bg-primary">
      <td data-field="color">COLOR</td>
      <td data-field="material">MATERIAL</td>
      <td data-field="dim">DIMENSIONS</td>
      <td data-field="quantity">QUANTITY</td>
    </tr>
  </thead>
  <tbody>  
  <?php while ($r = $m->fetch()){?> 
    <tr>
      <td><?=$r['colorname']?></td>
      <td><?=$r['materialname']?></td>
      <td><?=$r['categoryname']?></td>
      <td><?=$r['quantity ']?></td>
   <?php } ?>
  </tbody>
</table>

我想打印第一种颜色,然后打印与该颜色相关的所有 Material 。 我在那里遇到了麻烦,感谢任何帮助或建议?

最佳答案

首先在查询的group by子句中去掉colorname字段,添加该列,order by表示添加order by colorname 在查询中。

然后使用以下内容从 HTML 和 php 代码更改:

<table class="table table-bordered table-striped">
  <thead>
    <tr class="bg-primary">
      <td data-field="color">COLOR</td>
      <td data-field="material">MATERIAL</td>
      <td data-field="dim">DIMENSIONS</td>
      <td data-field="quantity">QUANTITY</td>
    </tr>
  </thead>
  <tbody>  
  <?php 
  $tempColor = '';
  while ($r = $m->fetch()){
  if($tempColor != $r['colorname']) {
   ?>
   <tr><td colspan="4">Color Name: <?=$r['colorname']?></td></tr>
  <?php $tempColor = $r['colorname'];
  } else { 
  ?> 
    <tr>
      <td><?=$r['colorname']?></td>
      <td><?=$r['materialname']?></td>
      <td><?=$r['categoryname']?></td>
      <td><?=$r['quantity ']?></td>
   <?php }
   }
    ?>
  </tbody>
</table>

关于php - 显示每种颜色的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29941668/

有关php - 显示每种颜色的值的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  4. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  5. ruby - 如果指定键的值在数组中相同,如何合并哈希 - 2

    我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat

  6. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  7. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

  8. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  9. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

  10. ruby 诅咒颜色 - 2

    如何使用Ruby的默认Curses库获取颜色?所以像这样:puts"\e[0m\e[30;47mtest\e[0m"效果很好。在浅灰色背景上呈现漂亮的黑色。但是这个:#!/usr/bin/envrubyrequire'curses'Curses.noecho#donotshowtypedkeysCurses.init_screenCurses.stdscr.keypad(true)#enablearrowkeys(forpageup/down)Curses.stdscr.nodelay=1Curses.clearCurses.setpos(0,0)Curses.addstr"Hello

随机推荐