gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:00:57 25 4
gpt4 key购买 nike

我有一个网站,其中包含各个国家和地区的页面。在主页上,我希望从一个数组中填充一个 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(countryabbreviation 与国家名称在同一个表中)。对此的任何帮助将不胜感激,因为我是 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>

关于php - 用数组填充 HTML 表并创建链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24017540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com