【问题标题】:How to make a PHP variable loop in template如何在模板中制作 PHP 变量循环
【发布时间】:2015-03-03 04:39:20
【问题描述】:

我正在尝试制作一些东西,以便显示数据库结果。问题是,它只显示 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")
?>

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


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      在您的 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 中,您可以在此处添加&lt;/tr&gt; 标签:

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

      【讨论】:

        猜你喜欢
        • 2020-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-24
        • 2021-05-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多