【问题标题】:PHP and HTML formatting issue when using tables使用表格时的 PHP 和 HTML 格式问题
【发布时间】:2017-10-05 01:30:18
【问题描述】:

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

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

我明白了:

表1:

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

表2:

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

我期待:

表1

  • header1 -------- header2
  • achievement1 - 成就时间
  • achievement2 - 成就时间

我已经尝试过 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>
";
}
?>

【问题讨论】:

    标签: php html foreach html-table


    【解决方案1】:

    为什么不把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>";
    

    【讨论】:

    • 其实我只是跳了一小会儿。你是明星。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      • 2010-09-26
      相关资源
      最近更新 更多