用while&显示mysql以适当的方式行事

时间:2013-05-26 11:41:07

标签: php mysql foreach while-loop

我试图显示这张表:

enter image description here

(澄清: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 ......而不是上面的例子。 但它应该在几个小时内分离出来。天。

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

2 个答案:

答案 0 :(得分:1)

Oke我看了看,我想我已经得到了你想要的东西。首先,在数据库中创建两个表,如:

每日活动的表格(我认为他们是学校科目的地方。纠正我如果我错了!):

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);

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

Fist制作一个名为'database.class.php'的php文件(Becouse我很好我用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

答案 1 :(得分:0)

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

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