dongzhuo1498 2016-05-07 19:19
浏览 21
已采纳

如何用PHP语言处理数组

<?php

$con = mysqli_connect('localhost', 'root', '');
if(!$con)
{
    die("not ok");
}

mysqli_select_db($con,"uoh");  


$q1 = "SELECT * FROM student_record INNER JOIN degree_plan ON
 student_record.course_number = degree_plan.course_number 
 INNER JOIN courses ON student_record.course_number = 
 courses.course_number where student_record.id  = 201102887 AND degree_plan.major='COE'";

$result = mysqli_query($con , $q1 ) ;
$data = array();
while($row = mysqli_fetch_array($result))
{
    $data[$row["term_no"]][] =  array(
        'code' => $row["code"],
        'grade' => $row["grade"]
    );

}


echo '<table width="200" border="1">';
   echo "<tr>";
   echo "<th>courses</th>";
   echo "<th>terms</th>";
   echo "<th>grades</th>";
   echo "</tr>";

foreach($data as $term=>$otherrow) {
    $count = 0;
    foreach ($otherrow as $data) {
        if($count == 0) {
            echo "<tr>";
            echo "<td>" . $data["code"]. "</td>";
            echo '<td rowspan="'.count($otherrow).'">' . $term. '</td>';
            echo "<td>" . $data["grade"]. "</td>";
            echo "</tr>";
            }

        else 

        {
            echo "<tr>";
            echo "<td>" . $data["code"]. "</td>";
            echo "<td>" . $data["grade"]. "</td>";
            echo "</tr>";
        }
        $count++;
    }
}
echo "</table>";

?>

I have this code and it work very well but I faced problem when I want to add more column . I tried to add fourth column(echo "<td>" . $row["crd"]. "</td>"; ) but there is no result .It give me empty cells. how I can do that?

I want add add this echo "<td>" . $row["crd"]. "</td>"; column to my code.

  • 写回答

1条回答 默认 最新

  • duanhuan7750 2016-05-07 19:37
    关注

    As mentioned in the comments, there are two errors that have been noticed.

    1. You are re-declaring $data in your second foreach loop
    2. You don't have $row initiated anywhere, and atempting to echo $row["crd"] will result in an empty cell.

    Proposed Solution:

    Change the name of the $data value in the foreach loop to $row and hence solve both problems at the same time:

    foreach($data as $term=>$otherrow) {
        $count = 0;
        foreach ($otherrow as $row) {
            if($count == 0) {
                echo "<tr>";
                echo "<td>" . $row["code"]. "</td>";
                echo '<td rowspan="'.count($otherrow).'">' . $term. '</td>';
                echo "<td>" . $row["grade"]. "</td>";
                echo "</tr>";
                }
    
            else 
    
            {
                echo "<tr>";
                echo "<td>" . $row["code"]. "</td>";
                echo "<td>" . $row["grade"]. "</td>";
                echo "</tr>";
            }
            $count++;
        }
    } 
    

    And when you add echo "<td>" . $row["crd"]. "</td>"; now it should echo the value stored in the $row array (as long as the value was extracted from the table in the database in the first place of course).

    Let me know if this worked for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 在虚拟机环境下完成以下,要求截图!
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见