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

时间:2015-03-03 04:39:20

标签: php templates template-engine

我正在尝试制作一些内容,以便显示数据库结果。问题是,它只显示1个结果。我希望它使用快速和脏的模板系统显示多个结果。还有,有办法让我的系统变得更好吗?一类或一个功能?我需要对此有所了解。非常感谢!

cp.php

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

模板/ 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>

2 个答案:

答案 0 :(得分:0)

在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.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?>
</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>

答案 1 :(得分:0)

我已将$ activetournaments更改为使用&#39;。=&#39;追加到最后。

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

目前,对于每条记录,它都会覆盖$ activetournaments。您也可以将$ activetournaments用作数组,然后在模板中执行一段时间/ foreach。