如何在PHP中将多维数组显示到表中?

时间:2015-09-30 23:55:32

标签: php mysql arrays multidimensional-array pdo

我试图处理一个项目,需要从mysql中从多个表中提取一些数据并将其显示到php中。这是代码:

 <?php   $db = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME . ';charset=utf8', DB_USER, DB_PASS);

           if(!$db) echo "fail";
          // else
          // echo "success";


$sth = $db->prepare("SELECT e.COURSE_NAME 'CourseCatalog', e.COURSE_CATEGORY 'CourseCatalog', d.OFFERING_START_DATE 'CourseOffered', d.OFFERING_END_DATE 'CourseOffered', d.OFFERING_CLASS_DAY 'CourseOffered' FROM PortalAccount a, Student_has_PortalAccount b, StudentEnrollment c, CourseOffered d, CourseCatalog e WHERE d.COURSE_ID = e.COURSE_ID and c.OFFERING_ID = d.OFFERING_ID and b.Student_Student_ID = c.STUDENT_ID and a.user_id = b.PortalAccount_PORTAL_ACCOUNT_ID and a.user_id = :user_id");
$sth->bindValue(':user_id', $_SESSION['user_id'], PDO::PARAM_STR);

$sth->execute();
$row = $sth->fetchAll();

var_dump($row); 
?>

结果是这样的:

array (size=2)
  0 => 
    array (size=7)
      'CourseCatalog' => string 'Video Game Design' (length=17)
      0 => string 'Video Game Design Minecraft Modding' (length=35)
      1 => string 'Video Game Design' (length=17)
      'CourseOffered' => string 'SATURDAY' (length=8)
  2 => string '2015-09-12' (length=10)
  3 => string '2015-11-14' (length=10)
  4 => string 'SATURDAY' (length=8)
  1 => 
  array (size=7)
  'CourseCatalog' => string 'Video Game Design' (length=17)
  0 => string 'Programming is Fun Using MIT Scratch' (length=36)
  1 => string 'Video Game Design' (length=17)
  'CourseOffered' => string 'SATURDAY' (length=8)
  2 => string '2015-09-12' (length=10)
  3 => string '2015-10-14' (length=10)
  4 => string 'SATURDAY' (length=8)

问题是如何让它像php中的这个表一样显示:

课程名称类型明细

视频游戏设计Minecraft Modding视频游戏设计09/12 - 11/14星期六

使用MIT Scratch Video Game Design编程很有趣09/12 - 11/14周六

谢谢!

0 个答案:

没有答案