0

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!

Anna_B
  • 820
  • 1
  • 4
  • 23
  • 1
    "_Or is there already a mistake?"_ - why don't you tell us? Explain what should happen, what actually happens, and include any error messages you see. See [ask] – Tangentially Perpendicular Sep 05 '22 at 01:24
  • As long as your PHP files are not relying on the session, what you have will work. Otherwise, I would stick your header and paragraph into two variables. And in the PHP files, you check if these variables are set. If they are, then display them to suit. – Rohit Gupta Sep 05 '22 at 01:36
  • 1
    try using relative path – Ken Lee Sep 05 '22 at 01:40
  • @TangentiallyPerpendicular Hey, I just put a lot of effort in rewriting my question. It still got closed. Can you tell me what exactly the problem is? I think I met all guidelines from the link you mention. – Anna_B Sep 05 '22 at 01:48
  • @RohitGupta How would you do that? – Anna_B Sep 05 '22 at 01:49
  • @KenLee What do you mean with relative path? – Anna_B Sep 05 '22 at 01:49
  • @Anna_B - It needs 3 votes to reopen, I have added my vote. It needs one more vote. If it gets reopened, I can answer your question. – Rohit Gupta Sep 05 '22 at 01:51
  • 1
    @RohitGupta Thank you! I honestly don't really understand why such questions get closed. I think even if it's not perfectly asked, it can help many people in the long run and can show a bigger variation of solutions. – Anna_B Sep 05 '22 at 01:56
  • 1
    I used to feel that way too. However, now that I can vote to close or open, I see it as a mechanism to force the OP to correct the question. – Rohit Gupta Sep 05 '22 at 02:05
  • @RohitGupta The question is open again :) – Anna_B Sep 05 '22 at 04:05
  • For once, this question is too broad. "How to do this... and also this and this!" is not a suitable question on Stack Overflow. Next, for the main part it is still unclear, what is asked here: *how* to include a random file, or *why* doesn't my code work. Looks like it's the latter but it phrased as an exact duplicate of existing question. That's why it gets closed. – Your Common Sense Sep 05 '22 at 05:16
  • To resolve your (main) problem, you need at least two things: 1. to learn [how to enable *error reporting*](https://phpdelusions.net/articles/error_reporting). That's the most crucial part, because disabled error reporting is the reason for you "don't get any error", but what is most important, will hinder you all the time. 2. To learn [the difference between paths on a web-server and a filesystem](https://phpdelusions.net/articles/paths). Although you still need an **absolute** path, it must be a **filesystem** path, as opposed to the virtual web-server path you are using at the moment. – Your Common Sense Sep 05 '22 at 05:19
  • Regarding your follow-up questions, simply split the filename, get the number from it, and construct your css filenames from that number. – Your Common Sense Sep 05 '22 at 05:20
  • You see: that's way too much info for just one answer. So broad answer will be of no help for anyone else. – Your Common Sense Sep 05 '22 at 05:21

0 Answers0