2015-03-03 120 views
-2

我試圖讓東西,所以它顯示的數據庫結果。問題是,它只顯示1個結果。我希望它使用快速和髒的模板系統顯示多個結果。另外,有沒有辦法讓我的系統更好?也許是一個班級或功能?我需要對此有所瞭解。謝謝一堆!如何使PHP變量循環模板

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> 

回答

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>