php - 如何使表格将相同的列打印到一行中?

标签 php html mysql sql database

我想做的是表中所示的内容。我只想获取数学科目并将单元从单元 1 (UI) 到数据库中最后一个可用单元进行排序;之后,在每个单元下,显示与每个单元相关的作业。我尝试使用 mysql_fetch_array 将其保存到一个数组中,但它存储涉及一个单元的行。我想要所有的单元,并且只想要下面有作业的单元。 我的数据库位于本文末尾。 希望有人能帮忙。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin título</title>
</head>


<body>


<form action="" method="post" name="form1" id="form1">
<table width="200" border="1">
  <tr>
    <td>Subject:</td>
    <td>Math</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>UI</td>
    <td>UI</td>
    <td>UII</td>
    <td>UIII</td>
    <td>UII</td>
    <td>...</td>
  </tr>
  <tr>
    <td>Home work</td>
    <td>Math work1</td>
    <td>Math work2</td>
    <td>Math work3</td>
    <td>Math work4</td>
    <td>Math work5</td>
    <td>...</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>


</body>
</html>

我只想要学科数学及其单元以及每个单元下的作业。 我的数据库是这样的:

-- phpMyAdmin SQL Dump
-- version 4.1.6
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Jul 25, 2014 at 05:52 AM
-- Server version: 5.6.16
-- PHP Version: 5.5.9

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `school`
--

-- --------------------------------------------------------

--
-- Table structure for table `class`
--

CREATE TABLE IF NOT EXISTS `class` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `subject` varchar(30) COLLATE utf8_spanish_ci NOT NULL,
  `unit` varchar(5) COLLATE utf8_spanish_ci NOT NULL,
  `homework` varchar(50) COLLATE utf8_spanish_ci NOT NULL,
  `dueDate` date NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci AUTO_INCREMENT=11 ;

--
-- Dumping data for table `class`
--

INSERT INTO `class` (`id`, `subject`, `unit`, `homework`, `dueDate`) VALUES
(1, 'Math', 'UI', 'Math work1', '2014-04-04'),
(2, 'Math', 'UI', 'Math work2', '2014-05-12'),
(3, 'Science', 'UI', 'Science work1', '2014-04-09'),
(4, 'Home Economics', 'UII', 'Home economics work1', '2014-04-16'),
(5, 'Math', 'UII', 'Math work 3', '2014-05-12'),
(6, 'Math', 'UIII', 'Math work4', '2014-05-21'),
(7, 'Science', 'UII', 'Science work2', '2014-05-14'),
(8, 'Physics', 'UI', 'Physics work1', '2014-05-06'),
(9, 'Math', 'UIII', 'Math work5', '2014-07-16'),
(10, 'Math', 'UIV', 'Math work6', '2014-07-14');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

最佳答案

您可以使用$query = mysql_query("SELECT * FROM class WHERE subject = 'Math'"); 这将从类(class)表中选择以数学为主题的所有内容,如果这正是您正在寻找的内容,但您需要更多帮助来回显数据,我将提供更多信息。

关于php - 如何使表格将相同的列打印到一行中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24948619/

相关文章:

php - 使用 PHP 基于特定日期显示 MySQL 数据

php - 如何从 php 与 MySQLi 保持连接

css - 隐藏的 Div 未正确显示

html - 按下按钮后如何更改按钮颜色?

python - 如何检查数据库是否存在于 PyMySQL 中

c# - 为什么 ExecuteReader 只返回 1 行数据?

php - Cake Console 无法在 Mac OS X Lion 上运行

php - 如何检查一天是否过去了?

mysql - 查询返回与特定列中的许多值关联的行

php - 简单 HTML DOM 内存泄漏