如何使表打印出一行中相同的列?

时间:2014-07-25 05:26:00

标签: 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 */;

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您想要的查询将是这样的

mysql_query("SELECT subject,unit,homework from class where subject='Math' order by unit")