【问题标题】:PHP code to change image source on each page with URL or page Title使用 URL 或页面标题更改每个页面上的图像源的 PHP 代码
【发布时间】:2015-03-16 22:22:43
【问题描述】:

我正在运行网站,该网站在每个页面上加载 main.php 文件,它有数千个页面。每个页面 url 都是不同的,例如,

mywebsite.com/United-Kingdom.php

mywebsite.com/United-Stats-of-america.php.

每个页面都有唯一的页面标题,由 PHP 从数据库中加载, (<h4 style="font-size:20px;color:#148ADB;"><?php echo $cityname['country']; ?>Information</h4>)。

我想用 url 或页面标题自动更改标题 jpg 图像,例如,如果用户转到 United-kingdom url,则图像应该在标题上加载 /images/country/united-kingdom.jpg 并且当用户打开 United Stats 页面时应该在标题上加载图像 /images/country/united-stats.jpg。

我怎样才能做到这一点?

= now() 按票价排序"); $arr1=mysql_fetch_array($sel1); ?>

<div id="content-top-txt" style="height:auto">
<div id="content-top-inner1">

<div id="content-top-txt-inner1" style="height:auto">       

        <div class="content">

        <h4 style="font-size:20px;color:#148ADB;"><?php echo $cityname['country']; ?>specials</h4>

        <p style='text-align:justify; font-size:13px; color:#3c3c3c;'>

        <strong style="text-transform:uppercase;color:#148ADB;"><?php echo $cityname['country']; ?></strong> with choice of range prices think of comfort for lesser cost<strong style="text-transform:uppercase"><?php echo $cityname['country']; ?></strong> starts from <?php

                    $tempcode=$arr1['des_code'];

                    $stemp=mysql_query("SELECT * FROM cities_var WHERE code='$tempcode'");

                    $temp=mysql_fetch_array($stemp);

                    if($temp['code']=='')

                    {                                               

                     ?>

                     <strong><?php echo $arr1['des_name'];?> (<?php echo $arr1['des_code']; ?>) </strong>

                    <?php

                    }

                    else

                    {

                    ?>

                    <a href="<?php echo $temp['url'] ?>" target="_blank"><strong><?php echo $arr1['des_name'];?> (<?php echo $arr1['des_code']; ?>) </strong></a>

                    <?php

                    }

                    ?>

                    @ <strong>&pound;<?php echo $arr1['fare']; ?></strong> with & table below<?php echo $arr1['des_name'];?> & other cities in <strong style="text-transform:uppercase"><?php echo $cityname['country']; ?></strong> About<strong>UK</strong>.General information About <strong style="text-transform:uppercase"><?php echo $cityname['country']; ?></strong>call @ 123456789 or use <a href="../../form.php">Form</a> or email:- <strong>Email@address.com</strong></p>

        <p><?php    echo $cvar['overview2']; ?></p>

        <p>Note:-Prices given are excluding taxes</p>

        </div>

<div id="content-top-inner2">

<div id="content-top-txt-inner2" style="height:auto">      

        <!-- slider start here -->

        <div class="slider-main">



        </div>   <!-- slider end here-->

        </div><!-- Content-top-txt-inner2 end-->

        </div><!-- content-top-inner2 end here-->

</div><!--contaent-top-txt end here -->

</div><!--content-top end here -->



<div id="content-middle">

<div id="content-middle-txt">   

        <?php

        include("../../files/lb1.php");

        ?>

    <div class="content-middle-txt-right">

        <?php       

        include("../../files/d-search-compare.php");            

        ?>

     </div>

     <div style="clear:both"></div>

  <div class="more"><p><span>Note :&nbsp;</span><?php //echo $sql_rec['note']; ?></p><p>paragraph text.</p>

</div> <!--content-middle-txt end -->

</div>  <!--content-middle end -->

<div id="content-bottom">

<div id="content-bottom-txt">                   

    <div id="bottom-all-box">

    <?php                                       

        include("../../files/lb2.php");

    ?>

    <div id="content-bottom-right">

    <?php

        include("../../files/rb1.php");

    ?> 

    </div>

    </div>

    <?php

    /*  

        include("../../files/country-left-box3.php");

        include("../../files/country-right-box1.php");

    */

    ?>     

【问题讨论】:

  • 用 URL 或页面标题更改每个页面上的图像源的 PHP 代码
  • 我试图复制粘贴我的代码,但它不包括 PHP 代码。
  • 将您的代码粘贴到问题中,选择它并按“代码”按钮 - 它会缩进它,这是格式化代码的 Markdown 方式。
  • 亲爱的我已尽力粘贴完整代码,请检查...
  • 请您花点时间以正确的方式放置代码。你的帖子现在真的很乱。

标签: php css jquery-ui


【解决方案1】:

我会将所有图像名称更改为 PHP 文件的确切页面名称。因此,例如您有/united-kingdom.php,您的图像应该命名为united-kingdom.jpg

同样,您可以使用$_SERVER['REQUEST_URI'] 获取您请求的当前页面 URL。

现在您可以使用当前页面名称定义图像。

$url= $_SERVER['REQUEST_URI'];
$imageName = substr($url, 0, strpos($imageName, "."));

变量$imageName 将包含united-kingdom。 所以你可以像这样定义你的图像:

<img src="<?php echo $imageName; ?>.jpg" alt="" />

此代码未经过全面测试,只是为了让您了解如何处理它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-26
    • 2013-09-14
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多