0

我试图显示这个表:

在此处输入图像描述

(澄清一下:Uren = 小时,Maandag = 星期一,Dinsdag = 星期二,Woensdag = 星期三,Donderdag = 星期四,Vrijdag = 星期五。

现在我计划在不同的表格中显示所有这样的数据,在同一页面上的某种“列表”中:

Monday 
Hours: Monday   
1.     Index
2.     Index
3.     Index
4.     Index
5.     Index
6.     Index
7.     Index
8.     Index

  Tuesday
 etc. etc.

其他日子都一样~

一直只有前8个小时。现在我得到了这个代码:

公共函数 notLoggedIn($whichtable, $whichrow, $link, $preference, $dag, $uur) { $this->newfunction = new functions;

    $getvalue = $this->newGet();
    $resultaat = DB_connect()->query("SELECT * FROM $whichtable WHERE $whichrow = '$preference'");

    $days = array("Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag"); // take all dagen op
    $loop = array();

    while($rij = $resultaat->fetch_assoc())
    {
            $loop[] = $rij;
    }

    foreach ($days as $currentDay) 
    {   
    echo "<table style='float:left; width:100%;'>";     
        echo "<th></th><th style='width:100%; left:0; background-color:#9a136e; color:#fff; font-size:20px;'>
        <div style='margin-auto;'>$currentDay</div></th>";  
        foreach($loop as $test) 
        {   
            $day = $test[$currentDay]; 
            $uren = $test['Uren'];

            $getkleur = explode(",", $day); 

                    echo "<tr>"; ?> <th style='width:20%; display:$getkleur[5]; border-right:1px dotted #000;'>
                    <? $this->newfunction->linkToHoure($uren)?></th><?
                    echo "<td style='border-bottom:1px solid #999; display:$getkleur[5]; width:100%; height:40px;'>".$getkleur[0]."
                    <a style='font-size:24px; margin-top:-7px; position:absolute; right:0;' href='#'>></a></td></tr>

                    <tr><td></td><td style='width:100%;'><b><a style='background-color:$getkleur[4]; color:#fff;' href=''>".$getkleur[3]."</b>
                    <b><a style='color:#906;' href=''>". $getkleur[2]."</b></a></td></tr>";             
        } // close foreach
    echo "</table>";
    } 
    }// close notloggedin function

现在它确实在同一页面上显示一切,但像这样:

 1. (Index of monday). Monday
    1. (Index of Tuesday). Tuesday
    1. (Index of Wednesday). Wednesday
    1. (Index of Thursday). Thursday
    1. (Index of Friday). Friday
    2. (Index of Monday). Monday
    2. (Index of Tuesday). Tuesday
    2. (Index of Wednesday). Wednesday
    2. (Index of Thursday). Thursday
    2. (Index of Friday). Friday

直到 8... 而不是上面的例子。但它应该在几小时和几天内分开。

我不知道如何解决这个问题。所有的帮助都是appriciated..

4

2 回答 2

1

好吧,我看了看,我想我已经得到了你想要的。首先,您在数据库中创建两个表,例如:

每日活动表(我假设它们在学校科目中。如果我错了,请纠正我!):

CREATE TABLE IF NOT EXISTS `vakken` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `vaknaam` varchar(30) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

插入一些示例数据:

INSERT INTO `vakken` (`id`, `vaknaam`) VALUES
(1, 'Nederlands'),
(2, 'Engels'),
(3, 'Duits'),
(4, 'Wiskunde'),
(5, 'Rekenen'),
(6, 'programming php'),
(7, 'Biologie'),
(8, 'Grieks'),
(9, 'Geschiedenis'),
(10, 'Scheikunde');

每日概览的最后一张表格:

CREATE TABLE IF NOT EXISTS `lesuren` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `les_id` int(11) NOT NULL,
  `uur` tinyint(2) NOT NULL,
  `dag` tinyint(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=43 ;

还插入一些示例数据:

INSERT INTO `lesuren` (`id`, `les_id`, `uur`, `dag`) VALUES
(3, 4, 1, 1),
(4, 7, 2, 1),
(5, 5, 3, 1),
(6, 1, 4, 1),
(7, 0, 5, 1),
(8, 6, 6, 1),
(9, 3, 7, 1),
(10, 4, 8, 1),
(11, 10, 1, 2),
(12, 8, 2, 2),
(13, 9, 3, 2),
(14, 7, 4, 2),
(15, 6, 5, 2),
(16, 4, 6, 2),
(17, 3, 7, 2),
(18, 1, 8, 2),
(19, 10, 1, 3),
(20, 8, 2, 3),
(21, 9, 3, 3),
(22, 7, 4, 3),
(23, 6, 5, 3),
(24, 4, 6, 3),
(25, 3, 7, 3),
(26, 1, 8, 3),
(27, 10, 1, 4),
(28, 8, 2, 4),
(29, 9, 3, 4),
(30, 7, 4, 4),
(31, 6, 5, 4),
(32, 4, 6, 4),
(33, 3, 7, 4),
(34, 1, 8, 4),
(35, 10, 1, 5),
(36, 8, 2, 5),
(37, 9, 3, 5),
(38, 7, 4, 5),
(39, 6, 5, 5),
(40, 4, 6, 5),
(41, 3, 7, 5),
(42, 1, 8, 5);

如您所见,该表仅包含数字。这就是我们在数据库中保存东西的方式。我们称之为模型。现在我们创建一些我们称之为视图的 html。

拳头制作一个名为“database.class.php”的 php 文件(因为我很好,我用 PDO = php 数据对象为您制作了一个简单的 db 类。这比使用旧函数更好,因为它们已被弃用):

<?php

    //Database class
    class db extends Pdo{

        private $inlog;
        private $pass;
        private $adres;
        private $dbname;

        public function __construct($dbname, $adres, $inlog, $pass){

            parent::__construct('mysql:dbname='.$dbname.';host='.$adres, $inlog, $pass);

            $this->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
            $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }

        public function doPrepare($queryString, $param){

            $query = $this->prepare($queryString);
            $query->execute($param);
            return $query;
        }

        public function doPrepareBind($queryString, $param){

            $query = $this->prepare($queryString);
            foreach($param as $par){
                switch($par[2]){
                    case 'int':
                        $query->bindParam($par[0], $par[1], PDO::PARAM_INT);
                        break;
                    case 'str':
                        $query->bindParam($par[0], $par[1], PDO::PARAM_STR);
                        break;
                    case 'blob':
                        $query->bindParam($par[0], $par[1], PDO::PARAM_LOB);
                        break;
                    default:
                        $query->bindParam($par[0], $par[1], PDO::PARAM_STR);
                        break;
                }
            }
            $query->execute();
            return $query;
        }
    }

?>

现在终于可以做一些PHP了。它实际上很短:

<?php

    require_once('database.class.php');

    // use construct like dbname - dbadres - login - pass
    $db = new db('test', 'localhost:3307', 'root', 'usbw');

    $query = $db->query("
                            SELECT l.`dag` , l.`uur` , v.`vaknaam` 
                            FROM  `lesuren` l
                            INNER JOIN  `vakken` v ON l.`les_id` = v.`id` 
                            ORDER BY l.`dag` , l.`uur` 
    ");

    $dagen = array(1 => 'Maandag',2 => 'Dinsdag',3 => 'Woensdag',4 => 'Donderdag',5 => 'Vrijdag');

    $html = '';
    $olddag = 0;
    while($row = $query->fetch()){

        if($olddag != $row['dag']){
            $html .= $dagen[$row['dag']].' <br>'; 
        }
        $html .= "$row[uur] - $row[vaknaam] <br>"; 
        $olddag = $row['dag'];
    }

    echo $html;

?>

你可以在这里看到一个现场演示(注意我的网络服务器并不总是在线):demo

于 2013-05-27T08:48:56.567 回答
0

首先尝试切换 while 和 foreach 部分。应该首先出现“每天”,然后在选择实际日期的情况下循环遍历表格。

格式化应该是另一部分,但这应该为您提供所需的每天 8 行输出。

于 2013-05-26T18:16:09.207 回答