【问题标题】:How do I use php to put an array index inside html tags如何使用 php 在 html 标签中放置数组索引
【发布时间】:2014-07-10 06:24:26
【问题描述】:

我正在暑期实习,我正在学习 php。我的老板希望我使用 php 在 img alt 标签内插入关键词。由于它是一个包含文件,他只希望它只完成一次,而不是对每个页面进行硬编码。我写了一个包含每个登录页面的数组,但他添加了一些东西并希望我完成它。我不确定如何将它们组合在一起:

<?php

$pages = array("index.php" => "Home Page", "locations.php"=>"Locations","faq.php" => "Frequently Asked Questions",   "contact.php" => "Contact Us", "aboutus.php" => "About Us",);

$real_url_link = $_SERVER['URI'];

parse_url($real_url_link);

echo $pages[$url];

?>

这是带有空 alt 标签的幻灯片:

                <div align="center" id="pics">
                    <img src="../animation-pics/slide1.jpg" alt="" width="1000" height="341" border="0">
                    <img src="../animation-pics/slide2.jpg" alt="" width="1000" height="341" border="0">
                    <img src="../animation-pics/slide3.jpg" alt="" width="1000" height="341" border="0">
                    <img src="../animation-pics/slide4.jpg" alt="" width="1000" height="341" border="0">
                    <img src="../animation-pics/slide5.jpg" alt="" width="1000" height="341" border="0">
                </div>  

            </tr>

'

我知道其中一些东西并没有真正做任何事情,但他说应该只需要几行代码就可以了

它工作的一个例子是: 为索引。 php

            <tr>

                <div align="center" id="pics">
                    <img src="../animation-pics/slide1.jpg" alt="Home Page" width="1000" height="341" border="0">
                    <img src="../animation-pics/slide2.jpg" alt="Home Page" width="1000" height="341" border="0">
                    <img src="../animation-pics/slide3.jpg" alt="Home Page" width="1000" height="341" border="0">
                    <img src="../animation-pics/slide4.jpg" alt="Home Page" width="1000" height="341" border="0">
                    <img src="../animation-pics/slide5.jpg" alt="Home Page" width="1000" height="341" border="0">
                </div>  

            </tr>

'

【问题讨论】:

  • 先修复语法错误
  • 你想在alt标签中添加index.php、locations.php等吗?
  • 我想为 index.php 添加每个页面的关键词,例如“主页”
  • @user3824140 你为什么这么说幻灯片?给出你期望结果的 HTML 部分。
  • 对于 index.php,alt 标签看起来像这样:而不是为空

标签: php html arrays tags


【解决方案1】:

如果我猜对了

<div align="center" id="pics">
    <?php $i = 1; ?>
    <?php foreach($pages as $link => $desc): ?>
        <img src="../animation-pics/slide<?php echo $i++; ?>.jpg" alt="<?php echo $desc; ?>" width="1000" height="341" border="0">
    <?php endforeach; ?>
</div>

【讨论】:

  • 当点击另一个链接时,标签会改变以匹配页面,因为幻灯片中有五个图像,所以会有五个,我可能解释得不好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-22
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 2019-10-21
  • 2015-10-14
  • 1970-01-01
相关资源
最近更新 更多