使用数组填充HTML表并创建链接

时间:2014-06-03 14:16:18

标签: php html mysql

我的网站上有各个国家和地区的网页。在主页面上,我希望从数组中填充4列×12行(48个结果)的HTML表。我目前有一个循环工作,将每个国家放入它自己的段落,但我迷失了如何将它们放入表中并让它们以我上面描述的4x12方式设置。

以下是查询:

$country_sql = "SELECT * FROM country";
$country_query = mysql_query($country_sql) or die(mysql_error());
$rsCountry = mysql_fetch_assoc($country_query);

然后在页面中,我现在可以将每个国家/地区插入一个新段落:

<?php do {?>
<p><?php echo $rsCountry['countryname'];?></p>
<?php } while ($rsCountry = mysql_fetch_assoc($country_query)) ?>

首先,我需要将此地点数据分成单独的td而不是新的段落。一旦我将这些数据放在表格中,我就需要国家名称链接到每个国家/地区的相应页面,该页面设置为mydomain.com/country/countryabbreviation(countrybbreviation与countryname在同一个表中)。任何对此的帮助都会非常感激,因为我对php很新。谢谢!

----完整代码---

<?php
///CONNECTION INFORMATION REMOVED FOR PRIVACY//////////


$country_sql = "SELECT * FROM country";
$country_query = mysql_query($country_sql) or die(mysql_error());
$rsCountry = mysql_fetch_assoc($country_query);

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" content="text/html">
    <title>TEST PAGE </title> 
</head>

<body>

<h1>List of Countries</h1>
<?php do {?>
<p><?php echo $rsCountry['countryname'];?></a></p>
<?php } while ($rsCountry = mysql_fetch_assoc($country_query)) ?>


</body>
</html>

输出: http://postimg.org/image/f2muo627d/

2 个答案:

答案 0 :(得分:0)

这是你想要的吗?我没有测试它是否编译

   $country_sql = "SELECT * FROM country";
   $country_query = mysql_query($country_sql) or die(mysql_error());
   $rsCountry = mysql_fetch_array($country_query)
   echo "<table><th>";
   $firstrow ="";
   foreach( $rsCountry as $name => $cell ){
       echo "<td>$name</td>";
       $firstrow = $firstrow + "<td>$cell</td>";
   }
   echo "</th>";
   echo "<tr>$firsrow</tr>";
   while( $rsCountry = mysql_fetch_array($country_query) ){
       echo "<tr>";
       foreach( $rsCountry as $cell ){
           echo "<td>$cell</td>";
       }
       echo "</tr>";
   }
   echo "</table>";

答案 1 :(得分:0)

这应该可以解决问题:

<?php

// Rewrote the query to only get 48 items.
$country_sql = "SELECT * FROM country LIMIT 48";
$country_query = mysql_query($country_sql) or die(mysql_error());

// Initialize an empty array.
$rsCountry = array();

// Put all our countries in there.
while ($row = mysql_fetch_assoc($country_query)) {
  $item = array();
  $item['href'] = '/country/' . $row['countryabbreviation'];
  $item['title'] = $row['countryname'];
  $rsCountry[] = $item;
}

// Chop up our massive array in rows of 4, should give us 12 rows.
$rsCountry = array_chunk($rsCountry, 4);

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" content="text/html">
    <title>TEST PAGE</title> 
</head>

<body>

  <table>
    <thead>
      <tr>
        <td colspan="4">
          <h1>List of Countries</h1>    
        </td>
      </tr>
    </thead>
    <tbody><?php
      foreach ($rsCountry as $row => $countries): ?>
      <tr><?php
        foreach ($countries as $country): ?>
        <td><a href="<?= $country['href'] ?>"><?= echo $country['title'] ?></a></td><?php
        endforeach; ?>
      </tr><?php
      endforeach; ?>
    </tbody>
  </table>

</body>
</html>