原帖: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 朋友,还在吗?

解决方案 »

  1.   

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

  2.   

    数据格式变一下。试试看行不行。
    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>';
      

  3.   

    楼上大哥,好像您写的有些错误,我略做修改如下:<?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>";
    应该是数组出错。
    敬请指教
      

  4.   

    请参见#4楼主的代码。sql语句改动了。错误解决方案请参见原帖#18的两种方法。
      

  5.   

    修改了PHP。ini  首行也增加了一条语句,运行结果年\月 1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月 
    2002             
    2003             还是没有读出价格来 这条语句有错误 echo "<td>$v[$i]</td>";
      

  6.   

    注意:#4的sql语句变了。
    你可以打印数组: print_r($tmp); 看对不对。
      

  7.   

    解决了,但必须修改一下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)
      

  8.   

    多谢 jordan102
     鼎力相助!!