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

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 朋友,还在吗?
...全文
135 11 打赏 收藏 转发到动态 举报
写回复
用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>";
}
1基础题_2.由计算机生成简单的四则运算题 1.1 需求分析: 本题主要是要求设计一个可以自动生成四则运算的测试器,并且完全由用户决定出加、减、乘、除哪一种运算题,以及出一位数还是两位数的运算题,同时还要对用户给出的答案的对错进行判断。在程序运行过程中,用户可以选择何时结束程序,并在结束程序时给出一个某种形式的成绩。 ///////////////////////////////////////////// 程序执行的结果://///////////////////////////////////////////////// 1.2 概要设计: 在对题目理解的基础上,并针对几个特别的技术环节,我认为程序可分为三个部分: 1) 程序的欢迎界面,主要通过一些特殊制表符来完成。其中运行,退出程序可以通过一个while循环来判定同时还要考虑用户输入信号量的正误; 2) 出题函数,也是本程序最关键的一个函数,通过使用“rand()%10”或“rand()%100”来获得一个0到9的一位整数随机值或得到0到99的两位整数随机值来为用户出题,并判断用户答案的对错; 3) 评分系统,是在用户选择退出后对用户所答题情况给出的成绩评价。 /////////////////////////////////////////////////// 程序流程图: 1.3 详细设计与编码: 为了使程序更加简洁与工整,且容易修改和阅读,我采用头文件的方式将Exam()函数放在了Exam .h中。Exam()函数主要负责程序的出题和结果的判断,其输入接口为运算符号,位数,即只需向其输入四则运算的一种符号和运算的位数,函数便自动生成题目并自动判断结果的正误,结果以1,0返回。而主程序则是完成了程序的开始、结束,用户成绩的判定。 /////////////////////////////////////////////////// 具体源程序如下: ---------------------------------------------------------------------------------------------------------------------- int Exam(int figure, int sign) {//本函数负责给用户出题 if (figure!=1&&figure!=2&&sign<1&&sign>4) return 0; //判断函数的输入是否符合要求 int a, b; if (figure==1) a=rand()%10; b=rand()%10; if (figure==2) a=rand()%100; b=rand()%100; switch(sign) { case(1): { cout<<" "<>r; if(r!=a+b) { cout<<" "<<"╳ 很遗憾,回答错误! X﹏X "<