如何在模板中创建PHP变量循环


How to make a PHP variable loop in template

我正在尝试制作一些东西,以便它显示数据库结果。问题是,它只显示了1个结果。我希望它显示多个结果使用一个快速和肮脏的模板系统。还有,有没有办法让我的系统变得更好?也许是一个类或函数?我需要对此有所了解。谢谢大家!

cp.php

<?php
require("db.php");
chdir("../"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
$title = "********";
require("templates/header.php");
if($mybb->user['uid'])
{
    $uid = $mybb->user['uid'];
    // Active Tournaments
    // run queries, do all the brainwork
    // lets get the forum name
    $query = "SELECT tourneys.name, tourneys.date, tourneys.time
            FROM tourneys
            INNER JOIN players ON players.tid = tourneys.id
            WHERE players.forumname = {$uid}";
    $result = mysqli_query($conn, $query);
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $activetournaments = "<td>". $row['name'] ."</td><td>" . $row['date'] . "</td><td>". $row['time'] ."</td>";
        // $team = mysqli_query($conn, "SELECT * FROM tourneys WHERE id=" . $row['tid'] . "");
        // $playing = mysqli_query($conn, "SELECT `` FROM tourneys WHERE id=" . $row['tid'] . "");
    }
}
else
{
        $error = "Only registered members may access this area.";
        include ('templates/error.php');
}
require ("templates/cp.php")
?>

templates/cp.php

<h2>Welcome back <?=$mybb->user['username']; ?>!</h2>
<?=$postrequirement?>
<h3>Active Tournaments</h3>
<table>
    <tr>
    <th>Name</th>
    <th>Date/time</th>
    <th>Team</th>
    <th>Playing as</th>
    <th>Options</th>
</tr>
<tr>
    <?=$activetournaments?>
    </table>
<hr />
<h3>Participated Tournaments</h3>
<table>
    <tr>
    <th>Position</th>
    <th>Name</th>
    <th>Date/time</th>
    <th>Team</th>
    <th>Played as</th>
    <th>Options</th>
</tr>
<tr>
    <?=$participatedtournaments?>
    </table>

我已经将$activechampions更改为使用'.='附加到末尾。

while($row = mysqli_fetch_assoc($result)) {
    $activetournaments .= "<td>". $row['name'] ."</td><td>" . $row['date'] . "</td><td>". $row['time'] ."</td> ";
}

目前,对于每一项记录,它都会覆盖$activechampionships。您也可以使用$activechampions作为数组,然后在模板中执行while/foreach。

在cp.php中,每次执行时都覆盖$activetournaments,将代码更改为:

<?php
require("db.php");
chdir("../"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
$title = "1ShotGG Tournaments | Control Panel";
require("templates/header.php");
if($mybb->user['uid'])
{
    $uid = $mybb->user['uid'];
    // Active Tournaments
    // run queries, do all the brainwork
    // lets get the forum name
    $query = "SELECT tourneys.name, tourneys.date, tourneys.time
            FROM tourneys
            INNER JOIN players ON players.tid = tourneys.id
            WHERE players.forumname = {$uid}";
    $result = mysqli_query($conn, $query);
    // output data of each row
    $activetournaments = '';
    while($row = mysqli_fetch_assoc($result)) {
        $activetournaments .= "<td>". $row['name'] ."</td><td>" . $row['date'] . "</td><td>". $row['time'] ."</td>";
        // $team = mysqli_query($conn, "SELECT * FROM tourneys WHERE id=" . $row['tid'] . "");
        // $playing = mysqli_query($conn, "SELECT `` FROM tourneys WHERE id=" . $row['tid'] . "");
    }
}
else
{
        $error = "Only registered members may access this area.";
        include ('templates/error.php');
}
require ("templates/cp.php")
?>

在templates/cp.php中,您可以在此处添加</tr>标签:

<tr>
    <?=$activetournaments?>
</tr>

您的新模板/cp.hp:

<h2>Welcome back <?=$mybb->user['username']; ?>!</h2>
<?=$postrequirement?>
<h3>Active Tournaments</h3>
<table>
    <tr>
    <th>Name</th>
    <th>Date/time</th>
    <th>Team</th>
    <th>Playing as</th>
    <th>Options</th>
</tr>
<tr>
    <?=$activetournaments?>
</tr>
    </table>
<hr />
<h3>Participated Tournaments</h3>
<table>
    <tr>
    <th>Position</th>
    <th>Name</th>
    <th>Date/time</th>
    <th>Team</th>
    <th>Played as</th>
    <th>Options</th>
</tr>
<tr>
    <?=$participatedtournaments?>
    </table>