php - 从循环、超文本 url 中获取结果

标签 php mysql url while-loop

我正在呼应数据库中涵盖的区域列表。该列表包含从数据库中获取的标题和子标题,

$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/

相关文章:

php - 使用注释的安全方法

php - 使用正则表达式计算具有特定内容的 php 脚本

mysql比较列和子查询

使用servlet请求转发后URL不显示正确的地址

php - CakePHP:直接使用验证函数

javascript - 将 ajax 值传递到 Laravel Controller

mysql - 将 AS table.column 添加到 LEFT JOIN 查询

mysql - 根据另一个表中的不相关字段从一个表中选择字段(具有 id 值的桥接表将它们链接起来)

java - 使用 Https 协议(protocol)从 Flickr 下载数据

java - 在 tomcat 6 中部署的应用程序的短 url 或别名