您好,欢迎访问一九零五行业门户网

查询数据库,按条件把内容按照数组分组排列显示,怎么弄呢?

因为网站业务原因,订单的内容包含n个不同的姓名 手机 性别 等等信息,上传表单的时候,是按照数组的方式上传的,现在需要显示一个总订单,按照订单号去 分组各个订单,是不是也是得按照数组的方式去查询输出和显示呢?因为本人是个新手,所以在这里请教各位大大!
数据库结构如下:
id     name       shouji        sex       zhuangtai    orderid
      1     张三       138xxx        男            1           1
      2     李四       186xxx        女            1           1
      3     王五       172xxx        男            1           1
      4     小宝       189xxx        男            1           2
      5     邵庄       186xxx        男            1           2
      6     甜甜       133xxx        女            2           3
      7     大宝       131xxx        男            3           4
     ....   ....       .....         ...           ...        ...
大家可以看到,所有的数据是按照这个结构存在数据库中,每个订单号 只有【orderid】这个字段是相同的,其他都是不同的!
现在需要在一个页面上输出显示这些订单,并按照下面的方式去显示,请问怎么办呢?请大家给写个方法和范例,可以吗?先在这里谢谢大家了!
回复讨论(解决方案) 那还不简单
把数据库读出来的数据 放到数组里面去处理
while($arr = mysql_fetch_row($result)){ $row[$arr['orderid']][] = $arr;}
那还不简单
把数据库读出来的数据 放到数组里面去处理
php code?1234while($arr = mysql_fetch_row($result)){ $row[$arr['orderid']][] = $arr;}
你好,非常感谢你的回答,可以稍微具体点吗? 我真的是对php 和数据库一点都不懂啊
大致可以这样写
$r=mysql_query('select orderid,zhuangtai,group_concat(`name`) as name,group_concat(`shouji`) as shouji,group_concat(`sex`) as sex from `order` group by orderid'); echo ; echo 姓名手机性别
;while($row=mysql_fetch_assoc($r)){ $shouji_arr=explode(',',$row['shouji']); $sex_arr=explode(',',$row['sex']); echo 订单号:.$row[orderid]. 状态:.$row['zhuangtai']. ; foreach(explode(',',$row['name']) as $k=>$v){ echo ; echo $v $shouji_arr[$k] $sex_arr[$k] ; echo
; }}echo
;
大致可以这样写
php code?1234567891011121314 $r=mysql_query('select orderid,zhuangtai,group_concat(`name`) as name,group_concat(`shouji`) as shouji,group_concat(`sex`) as sex from `order` group ……
explode() 函数把字符串分割为数组。
大致可以这样写
php code?1234567891011121314 $r=mysql_query('select orderid,zhuangtai,group_concat(`name`) as name,group_concat(`shouji`) as shouji,group_concat(`sex`) as sex from `order` group ……
group_concat(),该函数返回带有来自一个组的连接的非null值的字符串结果。
定义和用法
explode() 函数把字符串分割为数组。
语法
explode(separator,string,limit)
参数描述
separator必需。规定在哪里分割字符串。
string必需。要分割的字符串。
limit可选。规定所返回的数组元素的最大数目。
看来explode是把串起来的列分隔开了~! 对么
大致可以这样写
php code?1234567891011121314 $r=mysql_query('select orderid,zhuangtai,group_concat(`name`) as name,group_concat(`shouji`) as shouji,group_concat(`sex`) as sex from `order` group ……
非常感谢老大,那如果这个表里增加一个时间的字段,排列的顺序要按照时间的先后排列,应该怎么办呢?
大致可以这样写
php code?1234567891011121314 $r=mysql_query('select orderid,zhuangtai,group_concat(`name`) as name,group_concat(`shouji`) as shouji,group_concat(`sex`) as sex from `order` group ……
老大你好,接着上面的问题,如果每个订单(就是orderid一样的行)的时间一致,我可以直接在查询语句中加上order by time 来解决,但是 如果一个订单里,不同的行,时间不一样,按最小的时间来排顺序怎么办呢?  
引用 3 楼 jordan102 的回复:大致可以这样写
php code?1234567891011121314 $r=mysql_query('select orderid,zhuangtai,group_concat(`name`) as name,group_concat(`shouji`) as shouji,group_concat(`sex`) as se……
我用了order by min(time) 去查询 不知道 会不会有问题呢? 如果大数据表里,会不会又效率问题呢?
其它类似信息

推荐信息