我有一个网站,将有不同国家和地区的页面。在一个主页上,我希望填充一个HTML表,这是4列12行(48个结果)从一个数组。我目前有一个循环工作,将每个国家放入它自己的段落中,但我不知道如何将它们放入表中,并以我上面描述的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's而不是新的段落。一旦我在表中有了这些数据,我就需要将国家名称链接到每个国家各自的页面,该页面设置为mydomain.com/country/countryabbreviation (countryacronym与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/
这应该能奏效:
<?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>
这是你想要的吗?我没有测试它是否编译
$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>";