如何在模板中使PHP变量循环

问题描述 投票:-2回答:2

我正在尝试制作一些东西,以便显示数据库结果。问题是,它仅显示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>
php templates template-engine
2个回答
0
投票

我已更改$ activetournaments以使用'。='追加到末尾。

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

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


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

在您的模板/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>
© www.soinside.com 2019 - 2024. All rights reserved.