使用表时PHP和HTML格式问题

时间:2013-04-03 11:30:30

标签: php html foreach html-table

我一直在学习这个,因此我遇到了一个令人讨厌的问题,我似乎无法修复。

当内容回显到表而不是创建一个包含内容的表时,它会创建多个表。

我得到了:

表1:

  • header1 -------- header2
  • 成就1 - 成就 时间

表2:

  • header1 -------- header2
  • 成就2 - 成就时间

我期待:

表1

  • header1 -------- header2
  • 成就1 - 成就时间
  • 成就2 - 成就时间

我已经尝试过array_merge,我已经尝试将achievement-ID设置为键,我甚至尝试了多个foreach语句,但这似乎可以通过重复以及创建其他表来实现。 如果有人不介意告诉我我做错了什么,我会非常感激。

<?php
// Define cache file
$cache_file = "cache.txt";

// Cache file is less than thirty minutes old.
if (file_exists($cache_file) && (filemtime($cache_file) > (time() - 60 * 30  ))) {

// Don't bother refreshing, just use the file as-is.
$file = file_get_contents($cache_file);

// Our cache is out-of-date, so load the data from our remote server,
// and also save it over our cache for next time.
} else {
$file = file_get_contents('http://eu.battle.net/api/wow/guild/chamber-of-aspects/requiem%20paradisum?fields=achievements');
file_put_contents($cache_file, $file, LOCK_EX);
}

// Decode cache file
$achie = json_decode($file);

// Seperate achievements and timestamps as variables
$achiachi = ($achie->achievements->achievementsCompleted);
$achitime = ($achie->achievements->achievementsCompletedTimestamp);
foreach(array_combine($achiachi, $achitime) as $f => $n){

// Combine achievement number into Link for wowhead script
$achilink = ("http://www.wowhead.com/achievement=". $f);

// Sort out time formatting
$unix = ($n);
$not_unix = $unix / 1000;
$date = date("F d Y @ h:i A", $not_unix);

//Print into table
echo "
<table align=center class=mytable cellpadding=8 style=font-family:verdana;font-size:8pt;color:white;>
<tr>
        <th>Achievement</th><th>Timestamp</th>
<tr>
      <td><a href=\"$achilink\" rel=\"item=$achiachi\"></a></td>
      <td>$date</td>
</tr>
</table>
";
}
?>

2 个答案:

答案 0 :(得分:0)

为什么不将表格和标签放在循环之外?:

echo "<table align=center class=mytable cellpadding=8 style=font-family:verdana;font-size:8pt;color:white;>";
echo "<tr><th>Achievement</th><th>Timestamp</th></tr>";
foreach(array_combine($achiachi, $achitime) as $f => $n){

// Combine achievement number into Link for wowhead script
$achilink = ("http://www.wowhead.com/achievement=". $f);

// Sort out time formatting
$unix = ($n);
$not_unix = $unix / 1000;
$date = date("F d Y @ h:i A", $not_unix);

//Print into table
echo "<tr>
      <td><a href=\"$achilink\" rel=\"item=$achiachi\"></a></td>
      <td>$date</td>
</tr>";
}
echo "</table>";

答案 1 :(得分:0)

我认为你必须在for循环之外回显第一个和第一个标题