Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP生成的表中显示MySQL用户数据_Php_Html_Mysql - Fatal编程技术网

在PHP生成的表中显示MySQL用户数据

在PHP生成的表中显示MySQL用户数据,php,html,mysql,Php,Html,Mysql,我想用PHP和MySQL制作一个缺勤系统。每次缺勤都有一个休假类型。我想在生成的HTML表中显示此类型(例如sick)。左侧的第一个字段显示用户的姓名,其他字段为31个字段(如一个月) 我可以在表格中输入类型,但是我不知道如何只在正确的表格中显示它们。例如,“Max”的主键为0,缺勤类型为“Emergency”和“ASDF”,主键为2和“Compensation”,但这两种类型都在两个表中 Array ( [0] => Array ( [start] => 0 [end]

我想用PHP和MySQL制作一个缺勤系统。每次缺勤都有一个休假类型。我想在生成的HTML表中显示此类型(例如sick)。左侧的第一个字段显示用户的姓名,其他字段为31个字段(如一个月)

我可以在表格中输入类型,但是我不知道如何只在正确的表格中显示它们。例如,“Max”的主键为0,缺勤类型为“Emergency”和“ASDF”,主键为2和“Compensation”,但这两种类型都在两个表中

Array ( 
 [0] => 
   Array ( [start] => 0 [end] => 27 [type_FK] => Compensation [employee_FK] => 0 ) 
 [1] => 
   Array ( [start] => 1 [end] => 3 [type_FK] => Emergency [employee_FK] => 2 ) 
) 
如何在该表中仅显示正确用户的休假类型

<html>
        <head>
            <meta charset="utf-8">
            <link rel="stylesheet" type="text/css" href="css/style.css">
            <title>Absence System</title>
        </head>

        <body>
            <div id="container">
                <?php
                    $con = mysql_connect("localhost", "root", "");
                    if (!$con) {
                        die('Could not connect: ' . mysql_error());
                    }
                    mysql_select_db("absence_system", $con);

                    $result = mysql_query("select count(1) FROM employee");
                    $row    = mysql_fetch_array($result);

                    $count_user = $row[0];


                    $result2 = mysql_query("select start, end, type_FK, employee_FK FROM absences");
                    while ($row2 = mysql_fetch_assoc($result2)) {
                        $array_absences[] = $row2;
                    }


                    $count_absences = count($array_absences);


                    $result = mysql_query("select name FROM employee");
                    while ($row = mysql_fetch_assoc($result)) {
                        $array_user[] = $row;
                    }





                    $result = mysql_query("select surename FROM employee");
                    while ($row2 = mysql_fetch_assoc($result)) {
                        $new_array2[] = $row2;
                    }



                    for ($i = 0; $i < $count_absences; $i++) {
                        $array_absences[$i]['start'] = substr($array_absences[$i]['start'], -2);
                        $array_absences[$i]['end']   = substr($array_absences[$i]['end'], -2);

                        $array_absences[$i]['start'] = ereg_replace("^0", "", $array_absences[$i]['start']);
                        $array_absences[$i]['end']   = ereg_replace("^0", "", $array_absences[$i]['end']);

                        $array_absences[$i]['start'] = $array_absences[$i]['start'] - 1;

                        echo $array_absences[$i]['start'], "<br>";
                    }

                    print_r($array_absences);

                    echo "<table border='1'><br />";

                    echo "<tr>";
                    for ($i = 0; $i < 32; $i++) {
                        if ($i == 0) {
                            echo "<td>", "Name", "</td>";
                        } else {
                            echo "<td>", $i, "</td>";
                        }
                    }
                    echo "</tr>";
                    echo "</table>";



                    for ($row = 0; $row < $count_user; $row++) {

                        echo "<table border='1'><br />";
                        echo "<tr>";
                        //Tabelle mit 31 Tagen generieren
                        for ($col = 0; $col < 32; $col++) {
                            $true = 0;
                            if ($col == 0) {
                                //Name in die ersten Felder schreiben
                                echo "<td>", $array_user[$col]['name'], " ", $new_array2[$col]['surename'], "</td>";
                            }



                            for ($i = 0; $i < $count_absences; $i++) {
                                if ($col == $array_absences[$i]['start']) {
                                    echo "<td>", $array_absences[$i]['type_FK'], "</td>";
                                    $true = 1;
                                }


                            }

                            //Normale Felder
                            if ($true == 0) {
                                echo "<td>", $col, "</td>";
                            }


                        }

                        echo "</tr>";
                    }

                    echo "</table>";
                ?>  
            </div>
        </body>
    </html>




-- phpMyAdmin SQL Dump
-- version 4.2.11
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Erstellungszeit: 13. Feb 2015 um 16:07
-- Server Version: 5.6.21
-- PHP-Version: 5.5.19

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 */;

--
-- Datenbank: `absence_system`
--

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

--
-- Tabellenstruktur für Tabelle `absences`
--

CREATE TABLE IF NOT EXISTS `absences` (
  `absences_ID` int(11) NOT NULL,
  `employee_FK` int(11) NOT NULL,
  `start` date NOT NULL,
  `end` date NOT NULL,
  `approved` tinyint(1) NOT NULL,
  `comment` varchar(45) NOT NULL,
  `type_FK` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

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

--
-- Tabellenstruktur für Tabelle `employee`
--

CREATE TABLE IF NOT EXISTS `employee` (
  `employee_ID` int(11) NOT NULL,
  `name` varchar(45) NOT NULL,
  `surename` varchar(45) NOT NULL,
  `on_offshore_FK` int(11) NOT NULL,
  `location_FK` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

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

--
-- Tabellenstruktur für Tabelle `location`
--

CREATE TABLE IF NOT EXISTS `location` (
  `location_ID` int(7) NOT NULL,
  `country` varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

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

--
-- Tabellenstruktur für Tabelle `on_offshore`
--

CREATE TABLE IF NOT EXISTS `on_offshore` (
  `on_offshore_ID` int(11) NOT NULL,
  `on_off` varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

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

--
-- Tabellenstruktur für Tabelle `type`
--

CREATE TABLE IF NOT EXISTS `type` (
  `type` varchar(50) NOT NULL DEFAULT ''
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Indizes der exportierten Tabellen
--

--
-- Indizes für die Tabelle `absences`
--
ALTER TABLE `absences`
 ADD PRIMARY KEY (`absences_ID`), ADD KEY `employee_FK` (`employee_FK`), ADD KEY `type_FK` (`type_FK`);

--
-- Indizes für die Tabelle `employee`
--
ALTER TABLE `employee`
 ADD PRIMARY KEY (`employee_ID`), ADD KEY `on_offshore_FK` (`on_offshore_FK`), ADD KEY `location_FK` (`location_FK`);

--
-- Indizes für die Tabelle `location`
--
ALTER TABLE `location`
 ADD PRIMARY KEY (`location_ID`);

--
-- Indizes für die Tabelle `on_offshore`
--
ALTER TABLE `on_offshore`
 ADD PRIMARY KEY (`on_offshore_ID`);

--
-- Indizes für die Tabelle `type`
--
ALTER TABLE `type`
 ADD PRIMARY KEY (`type`);

--
-- Constraints der exportierten Tabellen
--

--
-- Constraints der Tabelle `absences`
--
ALTER TABLE `absences`
ADD CONSTRAINT `absences_ibfk_2` FOREIGN KEY (`employee_FK`) REFERENCES `employee` (`employee_ID`),
ADD CONSTRAINT `absences_ibfk_3` FOREIGN KEY (`type_FK`) REFERENCES `type` (`type`);

--
-- Constraints der Tabelle `employee`
--
ALTER TABLE `employee`
ADD CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`on_offshore_FK`) REFERENCES `on_offshore` (`on_offshore_ID`),
ADD CONSTRAINT `employee_ibfk_2` FOREIGN KEY (`location_FK`) REFERENCES `location` (`location_ID`);

/*!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 */;

缺勤制度
--phpMyAdmin SQL转储
--版本4.2.11
-- http://www.phpmyadmin.net
--
--主持人:127.0.0.1
--Erstellungszeit:13。2015年2月um 16:07
--服务器版本:5.6.21
--PHP版本:5.5.19
设置SQL\u MODE=“零上无自动值”;
设置时区=“+00:00”;
/*!40101 SET@OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT*/;
/*!40101 SET@OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS*/;
/*!40101设置@OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION*/;
/*!40101集合名称utf8*/;
--
--Datenbank:`缺勤制度`
--
-- --------------------------------------------------------
--
--Tabellenstruktur für Tabelle `缺席`
--
如果不存在“缺席”,则创建表(
`缺勤ID`int(11)不为空,
`员工输入(11)不为空,
`开始日期不为空,
`结束日期不为空,
`已批准的'tinyint(1)不为空,
`注释'varchar(45)不为空,
`键入_FK`varchar(50)不为空
)ENGINE=InnoDB默认字符集=1;
-- --------------------------------------------------------
--
--Tabellenstruktur für Tabelle'employee`
--
如果“employee”不存在,则创建表(
`员工ID`int(11)不为空,
`name`varchar(45)不为空,
`surename`varchar(45)不为空,
`在海上_FK`int(11)上不为空,
`位置_FK`int(11)不为空
)ENGINE=InnoDB默认字符集=1;
-- --------------------------------------------------------
--
--Tabellenstruktur für Tabelle`位置`
--
如果不存在“位置”,则创建表(
`位置_ID`int(7)不为空,
`country`varchar(45)不为空
)ENGINE=InnoDB默认字符集=1;
-- --------------------------------------------------------
--
--塔贝伦斯特鲁克图尔·弗尔·塔贝勒在海上`
--
创建表(如果不存在)`on_offshore`(
`on_offshore_ID`int(11)不为空,
`on_off`varchar(45)不为空
)ENGINE=InnoDB默认字符集=1;
-- --------------------------------------------------------
--
--Tabellenstruktur für Tabelle`型`
--
创建表(如果不存在)`type`(
`键入“varchar(50)非空默认值”
)ENGINE=InnoDB默认字符集=1;
--
--泰伯伦出口公司
--
--
--蒂贝尔缺席独立`
--
更改表格“缺勤”`
添加主键(`absences\u ID`),添加键`employee\u FK`(`employee\u FK`),添加键`type\u FK`(`type\u FK`);
--
--独立于塔贝尔的雇员`
--
ALTER TABLE`employee`
添加主键(`employee\u ID`),添加键`on\u offshore\u FK`(`on\u offshore\u FK`),添加键`location\u FK`(`location\u FK`);
--
--Indizes für die Tabelle的位置`
--
改变表格的位置`
添加主键(`location\u ID`);
--
--在海上的塔贝尔岛`
--
改变表`在海上`
添加主键(`on_offshore_ID`);
--
--印度教风格`
--
ALTER TABLE`类型`
添加主键(`type`);
--
--泰伯伦出口公司
--
--
--Tabelle缺席的限制条件`
--
更改表格“缺勤”`
添加约束'absences\u ibfk\u 2'外键('employee\u FK`)引用'employee`('employee\u ID`),
添加约束'absences\u ibfk\u 3'外键('type_FK`)引用'type`('type`);
--
--Tabelle'employee的约束条件`
--
ALTER TABLE`employee`
添加约束“employee\u ibfk\u 1”外键(`on\u offshore\u FK`)引用“on\u offshore`”(`on\u offshore\u ID`),
添加约束'employee_ibfk_2'外键('location_FK')引用'location'('location_ID');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT*/;
/*!40101集字符集结果=@旧字符集结果*/;
/*!40101设置排序规则\连接=@OLD\ u排序规则\连接*/;

尝试以下方法:

获取一个月的缺勤列表:
从缺勤表中选择*

获取员工列表:
从缺勤表中选择*

创建一个空的
$employees
数组。循环查看缺勤结果,并针对每次缺勤运行如下操作:

  for($i=0;$i<count($absences_array);$i++){
     if(!$employees[$absences_array[$i]['employee_FK']]['dates']){
        $employees[$absences_array[$i]['employee_FK']]['dates'] = array();
     }

     $employees[$absences_array[$i]['employee_FK']]['dates'][$absences_array[$i]['employee_FK']['start']];
     for( $j=$absences_array[$j]['employee_FK']['start'];$j<$absences_array[$j]['employee_FK']['end']; $j++){
        $employees[$absences_array[$i]['employee_FK']]['dates'][$j];
     }
}

用于($i=0;$Isence您刚刚开始这个项目,最好不要使用mysql扩展,因为它已被破坏,并且存在大量的安全问题。请使用mysqli或PDO。您能否更具体地说明它现在是什么样子以及您希望它是什么样子?我添加了一张图片。正如您所看到的,这两个表中都有缺漏项。您所说的['dates'是什么意思?我该如何更改输出?我基本上只需要循环的第二个条件:($col==$array_缺席[$I]['start'])。我指的是员工缺席的“日期”——无论您从“Select*from缺席表”中收到什么