问题未解决,接着提问!关于查询结果特殊显示的问题!

chemstudy2 2012-02-15 01:08:42
原帖:http://topic.csdn.net/u/20120214/16/171276bf-16a7-45a3-8224-fea338db7ee0.html?1686623126

这个帖子不能再回复了,另开一贴:

数据库结构、字段如下图:


要想达到这样的显示效果:


论坛上 jordan102 朋友帮忙写了个查询过程,PHP文件代码如下:

<?php
mysql_connect('127.0.0.1','root','');
mysql_select_db('jiagedata');



$result=mysql_query("select year(add_date) as y,group_concat(yc_jiage) as str from yc_jiage group by year(add_date);") or die(mysql_error());

echo '<table>';
while($row = mysql_fetch_assoc($result)){

echo '<tr>';
echo "<td>$row[y]</td>";
foreach(explode(',',$row['str']) as $v);
echo "<td>$v</td>";
echo "<tr>";

}
echo "</table>";
?>


运行结果不是我想要的,而是下面的结果:


显然数据没有全部列出,请教大家,到底哪里出错!
jordan102 朋友,还在吗?
...全文
139 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
chemstudy2 2012-02-15
  • 打赏
  • 举报
回复
多谢 jordan102
鼎力相助!!

chemstudy2 2012-02-15
  • 打赏
  • 举报
回复
按月份输出即可
chemstudy2 2012-02-15
  • 打赏
  • 举报
回复
解决了,但必须修改一下

group_concat(day(add_date)) as ss from yc_jiage group by year(add_date)


group_concat(month(add_date)) as ss from yc_jiage group by year(add_date)

一起混吧 2012-02-15
  • 打赏
  • 举报
回复
注意:#4的sql语句变了。
你可以打印数组: print_r($tmp); 看对不对。
chemstudy2 2012-02-15
  • 打赏
  • 举报
回复
修改了PHP。ini 首行也增加了一条语句,运行结果

年\月 1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月
2002
2003

还是没有读出价格来

这条语句有错误 echo "<td>$v[$i]</td>";
一起混吧 2012-02-15
  • 打赏
  • 举报
回复
请参见#4楼主的代码。sql语句改动了。

错误解决方案请参见原帖#18的两种方法。
chemstudy2 2012-02-15
  • 打赏
  • 举报
回复
楼上大哥,好像您写的有些错误,我略做修改如下:

<?php
mysql_connect('127.0.0.1','root','');
mysql_select_db('jiagedata');



$result=mysql_query("select year(add_date) as y,group_concat(yc_jiage) as str from yc_jiage group by year(add_date);") or die(mysql_error());

while($row=mysql_fetch_assoc($result)){
$arr[$row['y']]=$row;
}

foreach($arr as $k=>$v){
$s_arr=explode(',',$v['str']);
$n_arr=explode(',',$v['str']); 《《原来此处是 $n_arr=explode(',',$v[ss]); 提示ss未定义,于是改为 str ,错误消失

foreach($n_arr as $k1=>$n)
$tmp[$k][$n]=$s_arr[$k1];
}
echo "<table>";
echo "<tr>";
echo "<td>年\月</td>";
for($i=1;$i<=12;$i++){ echo "<td>{$i}月</td>";} 《《原来此处好像少了一对 花括号 {}
echo "</tr>";
foreach($tmp as $k2=>$v){
echo "<tr>";
echo '<td>'.$k2.'</td>';
for($i=1;$i<=12;$i++){
echo "<td>$v[$i]</td>";
}
echo '</tr>';
}
echo "</table>";
?>


以上程序运行结果如下:
年\月 1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月
2002

( ! ) Notice: Undefined offset: 1 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 2 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 3 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 4 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 5 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 6 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 7 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 8 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 9 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 10 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 11 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 12 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0
2003
( ! ) Notice: Undefined offset: 1 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 2 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 3 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 4 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 5 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 6 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 7 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 8 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 9 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 10 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 11 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0

( ! ) Notice: Undefined offset: 12 in D:\wamp\www\index.php on line 28
Call Stack
# Time Memory Function Location
1 0.0003 682560 {main}( ) ..\index.php:0


价格数据没有读出来,出错的第28行代码为 echo "<td>$v[$i]</td>";
应该是数组出错。


敬请指教
一起混吧 2012-02-15
  • 打赏
  • 举报
回复
数据格式变一下。试试看行不行。
sql语句改为:
select year(add_date) as y,group_concat(yc_jiage) as str,group_concat(day(add_date)) as ss from yc_jiage group by year(add_date)

while($row=mysql_fetch_assoc($result)){
$arr[$row[y]]=$row;
}

foreach($arr as $k=>$v){
$s_arr=explode(',',$v[str]);$n_arr=explode(',',$v[ss]);
foreach($n_arr as $k1=>$n)
$tmp[$k][$n]=$s_arr[$k1];
}
echo '<table width="800" algn="center" cellspacing=0>';
echo "<tr>";
echo "<td>年\月</td>";
for($i=1;$i<=12;$i++) echo "<td>{$i}月</td>";
echo "</tr>";
foreach($tmp as $k2=>$v){
echo "<tr>";
echo '<td>'.$k2.'</td>';
for($i=1;$i<=12;$i++){
echo "<td>$v[$i]</td>";
}
echo '</tr>';
}
echo '</table>';


chemstudy2 2012-02-15
  • 打赏
  • 举报
回复
预期显示效果应该为:

2002 空格— 12.50 13.00 13.30 11.00 12.00 16.00 14.70 11.90 23.00 15.00 12.00

2003 16.00 21.00 43.00 22.00
chemstudy2 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jordan102 的回复:]
foreach(explode(',',$row['str']) as $v);
echo "<td>$v</td>";

===》

foreach(explode(',',$row['str']) as $v){
echo "<td>$v</td>";
}
[/Quote]

还是不对,显示结果如下:

2002 12.00 15.00 23.00 11.90 14.70 16.00 12.00 11.00 13.30 13.00 12.50

2003 16.00 21.00 43.00 22.00

整个2002年的数据倒叙显示了,没有按照由左至右 1月到12月显示,更重要的,2002年1月份没有价格数据(一楼图片),这儿应该留空缺,预期显示效果应该为:

2002 12.50 13.00 13.30 11.00 12.00 16.00 14.70 11.90 23.00 15.00 12.00

2003 16.00 21.00 43.00 22.00
一起混吧 2012-02-15
  • 打赏
  • 举报
回复
foreach(explode(',',$row['str']) as $v);
echo "<td>$v</td>";

===》

foreach(explode(',',$row['str']) as $v){
echo "<td>$v</td>";
}

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧