gpt4 book ai didi

php - 以正确的方式使用 while 和 foreach 显示 mysql

转载 作者:行者123 更新时间:2023-11-29 13:50:25 26 4
gpt4 key购买 nike

我正在尝试显示此表格:

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 小时。现在我得到了这个代码:

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

    $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...而不是上面的例子。但它应该在几小时和几天内分开。

我不知道如何解决这个问题。所有帮助均适用..

最佳答案

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

每日事件表(我假设它们是学校科目。如果我错了,请纠正我!):

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,我们称之为 View 。

首先创建一个名为“database.class.php”的 php 文件(因为我太好了,我使用 PDO = php 数据对象为您创建了一个简单的数据库类。这比使用旧函数更好,因为它们是已弃用):

<?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

关于php - 以正确的方式使用 while 和 foreach 显示 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16758867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com