我正在呼应数据库中涵盖的区域列表。该列表包含从数据库中获取的标题和子标题,
$area_shire = '';
$area_district = '';
$area_name = '';
while($rows = mysql_fetch_array($query)):
if($rows['area_shire'] != $area_shire) {
echo '<h1>'.$rows['area_shire'].'</h1>';
$area_shire = $rows['area_shire'];
}
/* same for district using h2 and name using h3 */
endwhile;
我现在想让每个结果成为一个超文本网址,所以我添加了
$area_shire_url = str_replace(' ', '_', $area_shire);
$area_district_url = str_replace(' ', '_', $area_district);
$area_name_url = str_replace(' ', '_', $area_name);
并将每个回显更改为
echo '<a href=\"Tree_Surgery_'.$area_shire_url.'.php\"><h2>'.$rows['area_shire'].'<br></h2>';
$area_shire = $rows['area_shire'];}
/* same for district using h2 and name using h3 */
这根本不起作用?
最佳答案
我会将片段重写为:
$area_shire = '';
$area_district = '';
$area_name = '';
while($rows = mysql_fetch_assoc($query)) {
if ($rows['area_shire'] != $area_shire) {
$area_shire = $rows['area_shire'];
$area_shire_url = 'Tree_Surgery_'.str_replace(' ', '_', $area_shire).'.php';
echo '<h2><a href="'.$area_shire_url.'">'.$area_shire.'</a></h2><br>';
}
// same for district using h2 and name using h3
}
您的错误似乎是您在单引号字符串中转义了 "
。当使用 '
时,php 将按原样回显所有包含的字符;无需转义。
请注意,我还使用了 mysql_fetch_assoc而不是mysql_fetch_array并重新排列 HTML 标签的顺序以避免嵌套 block-level elements里面inline elements .
我还选择将完整的 url 存储在变量中,而不是仅存储其中的一部分,并将其在 echo 语句中组合到完整的 url 中。在我看来,这更容易阅读。特别是当您稍后想要编辑内容时。
关于php - 从循环、超文本 url 中获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13748716/