PHP页面标题标记未正确显示

时间:2014-10-07 00:06:54

标签: php mysql title

我呼吁我的MySQL数据库在band_cat_list.php的title标签中发布一个类别的名称。 这是将其添加到标题中的代码:

BAND_CAT_LIST.PHP
<head>
<?php

    include ('database.php');

    $get_title = "SELECT * FROM `Categories`";

    $run_title = mysql_query($get_title);

    while ($row_title = mysql_fetch_array($run_title)){

        $cat_id   = $row_title['cat_id'];
        $cat_title = $row_title['cat_title'];

        echo "<title>Offstreams - $cat_title</title>";
    }
?>
</head>

在index.php里面,我发布了类别。类别显示没有问题,当我点击它们时,它们会转到相应的页面,即“index.php?band_cat_list&amp; cat = $ category_id”

INDEX.PHP
 <div class="header_links">
    <ul class="header_ul">
        <?php
            include("includes/database.php"); 

            $get_categories = "select * from Categories";

            $run_categories = mysql_query($get_categories);

            if (!$run_categories) { // add this check.
                die('Invalid query: ' . mysql_error());
            }

            while ($category_row=mysql_fetch_array($run_categories)){

                $category_id = $category_row['cat_id'];
                $category_title = $category_row['cat_title'];

                echo "<li class='header_li' align='center'><a href='index.php?band_cat_list&cat=$category_id'>$category_title</a></li>";
                }
        ?>
    </ul>
</div>

我在我的服务器上运行PHP 5.4,所以我知道函数mysql_fetch_array()并没有被弃用。但是我无法弄明白。当我点击任何类别时,它只会在标题标签的列表中发布第一个类别。请帮忙。

2 个答案:

答案 0 :(得分:1)

HTML页面中不能有多个标题标记。

您编写的代码将输出多个标题标签,假设您在类别中有多个记录。如果我理解你要做什么,以下应该有效:

<head>
<?php

include ('database.php');

$id = isset($_GET['cat']) ? $_GET['cat'] : exit("cat id is missing");

$get_title = "SELECT * FROM `Categories` WHERE `cat_id` = '{$id}'";

$run_title = mysql_query($get_title);

while ($row_title = mysql_fetch_assoc($run_title)){

    $cat_id   = $row_title['cat_id'];
    $cat_title = $row_title['cat_title'];

    echo "<title>Offstreams - $cat_title</title>";
}
?>
</head>

PS:你不应该使用mysql扩展,因为它已被弃用且不再维护。尝试使用mysqli

<强>更新

您应该将index.php文件更改为

INDEX.PHP
 <div class="header_links">
    <ul class="header_ul">
        <?php
            include("includes/database.php"); 

            $get_categories = "select * from Categories";

            $run_categories = mysql_query($get_categories);

            if (!$run_categories) { // add this check.
                die('Invalid query: ' . mysql_error());
            }

            while ($category_row=mysql_fetch_array($run_categories)){

                $category_id = $category_row['cat_id'];
                $category_title = $category_row['cat_title'];

                echo "<li class='header_li' align='center'><a href='BAND_CAT_LIST.php?band_cat_list&cat=$category_id'>$category_title</a></li>";
                }
        ?>
    </ul>
</div>

您的BAND_CAT_LIST.php应将标题显示为“Offstreams - $ selected_category”

<head>
<?php

include ('database.php');

$id = isset($_GET['cat']) ? $_GET['cat'] : false;

if($id) {
  $get_title = "SELECT * FROM `Categories` WHERE `cat_id` = '{$id}'";

  $run_title = mysql_query($get_title);

  while ($row_title = mysql_fetch_assoc($run_title)){

      $cat_id   = $row_title['cat_id'];
      $cat_title = $row_title['cat_title'];

      echo "<title>Offstreams - $cat_title</title>";
  }
}
?>
</head>

答案 1 :(得分:0)

echo移到while循环之外。此外,如果$cat_title是一个数组,则需要将其转换为字符串,并可能对其进行格式化。

<强> BAND_CAT_LIST.PHP

<head>
<?php

    include ('database.php');

    $get_title = "SELECT * FROM `Categories`";

    $run_title = mysql_query($get_title);
    $cat_title = array();
    while ($row_title = mysql_fetch_array($run_title)){

        $cat_id   = $row_title['cat_id'];
        $cat_title = $row_title['cat_title'];

    }
    if (is_array($cat_title)) {
        $cat_title = implode(" - ", $cat_title);
    }
    echo "<title>Offstreams - $cat_title['cat_title']</title>";
?>
</head>