dongtuojuan8998 2014-10-07 00:06
浏览 31

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

I call upon my MySQL database to post the name of a category in the title tag in band_cat_list.php. This is the code for adding it into the 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>

Inside index.php, I post the categories. The Categories display no problem and when I click on them they go to the appropriate page which is "index.php?band_cat_list&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>

I am running PHP 5.4 on my server, so I know the function mysql_fetch_array() isn't deprecated.But I can't figure it out. When I click on any of the categories, it only posts the first one on the list in the title tag. Please help.

  • 写回答

2条回答 默认 最新

  • dongwei4096 2014-10-07 00:13
    关注

    You can not have multiple title tags in an HTML page.

    The code you wrote will output multiple title tags, assuming you have multiple records inside Categories. If I understand what you are trying to do the following should work:

    <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: you should not use the mysql extension since it is deprecated and no longer maintained. Try using mysqli instead

    UPDATE:

    You should chnage your index.php file to

    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>
    

    The your BAND_CAT_LIST.php should display the title as the "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>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法