简体   繁体   English

使用 PHP 从列表中随机显示 HTML 片段

[英]Randomly Show an HTML Snippet From List With PHP

As the title implies, I have a few different lines of HTML code (Example Below), and I would like the server to randomly select one and show it when the page loads.正如标题所暗示的,我有几行不同的 HTML 代码(下面的示例),我希望服务器随机选择一个并在页面加载时显示它。

I have these three HTML snippets, and I want one to randomly show when the page is loaded.我有这三个 HTML 片段,我想要一个在页面加载时随机显示。

<img src=“image1.png” alt=“image1”>
<p class=“class1”>Image Caption<a href=“link1”> And Link</a></p>
<img src=“image2.png” alt=“image2”>
<p class=“class2”>Image Caption<a href=“link2”> And Link</a></p>
<img src=“image3.png” alt=“image3”>
<p class=“class3”>Image Caption<a href=“link3”> And Link</a></p>

I found this example using the PHP array function, but I don't know how to put the array in a separate PHP file, or if I can change the image links to full HTML code.我发现这个例子使用 PHP 数组函数,但我不知道如何将数组放在单独的 PHP 文件中,或者我是否可以将图像链接更改为完整的 HTML 代码。

The example I found:我发现的例子:

<?php
    $images = array('img1.jpg', 'img2.jpg', 'img3.jpg');
?>

Then, you just get a random element from the array然后,您只需从数组中获取一个随机元素

<?php
    $random_image = array_rand($images);
?>

Then you can go right away displaying the image:然后您可以立即显示图像:

<img src="<?php echo $images[$random_image]; ?>" />

How can I make this so the HTML snippets above are stored in the array, and the array is in a different PHP file (I can use the “require” function for that, right?).我如何才能使上面的 HTML 片段存储在数组中,并且数组位于不同的 PHP 文件中(我可以为此使用“require”函数,对吗?)。

You can echo any amount of html code just like you would echo the value of a property.您可以回显任意数量的 html 代码,就像回显属性值一样。

You're right that you can use require to access an array defined in another file.您可以使用require访问另一个文件中定义的数组,这是对的。

<?php
    $images = require 'images_array.php';
    echo $images[array_rand($images)];
?>

images_array.php: images_array.php:

array(
    '<img src=“image1.png” alt=“image1”><p class=“class1”>Image Caption<a href=“link1”> And Link</a></p>',
    '<img src=“image2.png” alt=“image2”><p class=“class2”>Image Caption<a href=“link2”> And Link</a></p>',
    '<img src=“image3.png” alt=“image3”><p class=“class3”>Image Caption<a href=“link3”> And Link</a></p>',
);

I recommend that you avoid bloating your file with redundant html markup.我建议您避免使用多余的 html 标记使文件膨胀。 Instead of repeating yourself, practice the DRY principle and separate all of the variable parts from the static parts.不要重复自己,练习 DRY 原则并将所有可变部分与静态部分分开。

Using a lookup array and a template string with improve maintainability and reduce the potential for typos.使用查找数组和模板字符串可以提高可维护性并减少打字错误的可能性。

Code: ( Demo )代码:(演示

$lookup = [
    1 => [
        'img' => 'image1.png',
        'alt' => 'image1',
        'class' => 'class1',
        'caption' => 'Image Caption',
        'href' => 'link1',
        'linkText' => 'And Link',
    ],
    2 => [
        'img' => 'image2.png',
        'alt' => 'image2',
        'class' => 'class2',
        'caption' => 'Image Caption',
        'href' => 'link2',
        'linkText' => 'And Link',
    ],
    3 => [
        'img' => 'image3.png',
        'alt' => 'image3',
        'class' => 'class3',
        'caption' => 'Image Caption',
        'href' => 'link3',
        'linkText' => 'And Link',
    ],
];

$markup = '<img src="%1$s" alt="%2$s">
<p class="%3$s">%4$s <a href="%5$s">%6$s</a></p>';

vprintf($markup, $lookup[array_rand($lookup)]);

Potential Output:潜在产出:

<img src="image3.png" alt="image3">
<p class="class3">Image Caption <a href="link3">And Link</a></p>

If you want to store the array in another file, okay.如果要将数组存储在另一个文件中,可以。 Just be sure to return the array in the file being require ed.只要确保返回require编辑的文件中的数组。 Example例子

lookup.php查找.php

<?php
return [1 => [...], 2 => [...], 3 => [...]];

markup.php标记.php

<?php
$lookup = require 'lookup.php';
$markup = '<img src="%1$s" alt="%2$s"><p class="%3$s">%4$s <a href="%5$s">%6$s</a></p>';
vprintf($markup, $lookup[array_rand($lookup)]);

As you seems to have pure PHP and no template engine you can do this :由于您似乎拥有纯 PHP 而没有模板引擎,您可以这样做:

<?php
$random = rand(0,60);
if ($random < 20) {
?>
<img src=“image1.png” alt=“image1”>
<p class=“class1”>Image Caption<a href=“link1”> And Link</a></p>
<?php
} elseif ($random < 40) {
?>
<img src=“image2.png” alt=“image2”>
<p class=“class2”>Image Caption<a href=“link2”> And Link</a></p>
<?php
} else {
?>
<img src=“image3.png” alt=“image3”>
<p class=“class3”>Image Caption<a href=“link3”> And Link</a></p>
<?php
}
?>

It'll display a random part at each page load.它会在每个页面加载时显示一个随机部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM