-1

我一直在通过这样做来学习这一点,因此我遇到了一个我似乎无法解决的恼人问题。

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

我越来越 :

表格1 :

  • 标头1 --------标头2
  • 成就1 - 成就时间

表2:

  • 标头1 --------标头2
  • 成就2 - 成就时间

我期待着 :

表格1

  • 标头1 --------标头2
  • 成就1 - 成就时间
  • 成就2 - 成就时间

我试过array_merge,我试过将成就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>
";
}
?>
4

2 回答 2

0

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

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>";
于 2013-04-03T11:34:36.897 回答
0

我认为您必须在 for 循环之外回显 and first for 标头

于 2013-04-03T11:37:20.250 回答