HTML表格将行更改为列For循环

时间:2018-01-27 03:47:15

标签: php html html-table

我需要附上两张桌子。 第一循环是完美的, 但第二个For-loop表进入了Bottom。 我需要将那个底部表附加到前一个表。 任何人都可以解决这个问题吗? 任何解决方案都将不胜感激,所以建议解决这个问题。

<?php
    include 'pappu.php';
    header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    $grab=ngegrab('https://www.cryptopia.co.nz/api/GetMarkets/USDT');
    $json = json_decode($grab, true);
    $usd= $json['Data'][3]['AskPrice'];
    $grabb=ngegrab('https://www.cryptopia.co.nz/api/GetMarkets/USDT');
    $jsonss = json_decode($grabb, true);
    $dogeusd= $jsonss['Data'][8]['AskPrice'];
    $grabsz=ngegrab('https://www.cryptopia.co.nz/api/GetMarkets/DOGE');
    $jsonsz = json_decode($grabsz);
    $grabs=ngegrab('https://www.cryptopia.co.nz/api/GetMarkets/BTC');
    $jsons = json_decode($grabs);
    echo "<table border=1>";
    echo "<th>BTC EXCHANGE</th>";
    if($jsons)
        foreach ($jsons->Data as $sam){
            $market= $sam->Label . "\n";
            $link= $sam->AskPrice . "\n";
            echo "<tr><td>$market</td>";
            $link = number_format($link, 8);
            echo "<td>$link" ;
            echo '($'; 
            echo number_format($link * $usd, 6) ;
            echo ')';
            echo "</td></tr>";
        }
    echo "<th>DOGE EXCHANGE</th>";
    foreach ($jsonsz->Data as $sam){
        $market= $sam->Label . "\n";
        $link= $sam->AskPrice . "\n";
        echo "<tr><td>$market</td>";
        $link = number_format($link, 8);
        echo "<td>$link" ;
        echo '($'  ; 
        echo number_format($link * $dogeusd, 6) ;
        echo ')';
        echo "</td></tr>";
    }
    echo "</table>";
?>

2 个答案:

答案 0 :(得分:1)

首先设置表格标题(<th>),然后设置表格主体,如果您希望所有标题显示为内联。

然后,您可以为两个表的行创建数组,并为每一行输出。

注意:如果您的$btc$doge数组具有不同的行数或者行数不同,则可能需要创建第三个数组结合两者,然后循环。

echo '<table border="1">
      <tr>
          <th colspan="2">BTC EXCHANGE</th>
          <th colspan="2">DOGE EXCHANGE</th>
      </tr>';

// prepare the rows
$btc = array();
$doge = array();

if($jsons)
{
  foreach ($jsons->Data as $sam)
  {
     $number = (float) $sam->AskPrice;
     array_push($btc, array($sam->Label, number_format($number,8) ));
  }

  foreach ($jsonsz->Data as $sam)
  {
     $number = (float) $sam->AskPrice;
     array_push($doge, array($sam->Label, number_format($number,8) ));
  }

  // assuming count($btc) and count($doge) are the same
  for($i=0; $i< count($btc); $i++)
  {
      echo '<tr>
             <td>'.$btc[i][0].'</td>
             <td>'.$btc[i][1].' ($'.number_format($btc[i][1] * $usd, 6).')</td>
             <td>'.$doge[i][0].'</td>
             <td>'.$doge[i][1].' ($'.number_format($doge[i][1] * $usd, 6).')</td> 
          </tr>';
  }
}
echo '</table>';
?>

答案 1 :(得分:0)

您有两个<th>单元格应位于表格行<tr>中:

echo "<th>BTC EXCHANGE</th>";

echo "<th>DOGE EXCHANGE</th>";

将您的代码更改为:

echo "<tr><th>BTC EXCHANGE</th></tr>";

echo "<tr><th>DOGE EXCHANGE</th>";