I would like to have an area on a website that includes the content of one of 10 PHP files from one folder randomly. I thought it should work like that:
<h1>This is my website.</h1>
<p>Here is an area with a content from one of 10 php files from one folder, with individual CSS and JS:</p>
<?php include <!-- Including randomly one PHP file -->
$directory = '/myTenPHPfiles'; <!-- The folder -->
$files = array_filter(scandir($directory), fn($f) => is_file($f));
$randFile = $directory . '/' . $files[array_rand($files)];
include $randFile; <!-- Including -->
;?>
But it seems to not work. I don't get any error in my console, but it also doesn't show me the content of one file.
For the next step, the PHP files also require individual CSS and JS files. Let's say the first PHP file has the name 1.php
, how can 1.css
and 1.js
included correctly to the HTML document? If for example 8.php
got chosen randomly, then also 8.css
and 8.js
needs to be included to the HTML file. I think the CSS file should get included to the <head>
and the .js
file right before the closing </body>
tag.
This is an example what I would imagine as an example result:
// This is the content of the 1.js file:
$(".one").css({
"background-color": "yellow",
"font-weight": "bold"
});
/* These styles are the original content of the 1.css file: */
.one {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>This is my website.</h1>
<p>Here is an area with a content from one of 10 php files from one folder, with individual CSS and JS:</p>
<p class="one">This text is the original content of 1.php</p>
How should the code look like? I would be very thankful for help!