dongyu9894 2011-07-14 13:37
浏览 31
已采纳

用php创建html页眉

Firstly I'm very new to web development so forgive me if this is a stupid or repeated question.

All my web pages have the same header so I simply add the following line to the top of every page:

    <?php require('header.html') ?>

This works fine. But now I'd like to vary the image in the header depending on which page it is on. I'm thinking about using a php function like this:

    function create_header($image){
        //1. echo contents of header.html

        //2. replace default image location with $image if not null
    }

Problem is I don't know how to do steps 1 or 2. Is this possible and/or is there a better way of doing what I want?

  • 写回答

2条回答 默认 最新

  • douxidang9092 2011-07-14 13:49
    关注

    You should use some basic form of templating.

    Let's say your header looks like this:

    <div id="header">
        <img src="images/myimage.jpg" alt="" />
        <h1>Welcome to my site!</h1>
    </div>
    

    If we modify it to turn the image into a variable we get this:

    <div id="header">
        <img src="images/<?php echo $header_image; ?>" alt="" />
        <h1>Welcome to my site!</h1>
    </div>
    

    Now all you have to do is set your variable before you include your header file like so:

    <?php
    $header_image = "image2.jpg";
    include "header.html";
    ?>
    

    This concept of basic templating can be applied to an entire page template and is not limited to header files.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器