I need your help to make this work. I need to combine two php codes to work. I am a beginner in php and thank you very much for every advice you can give me. Thank you in advance!
Description: I want to display content (some text) from 8 pages in my index.php and display the obtained content in random order. I didn't know how to choose where to start retrieving the text data for the page, so I chose the character option (starts after 1305 characters, counting 375 characters).
Code 1 (Select 8 pages and display them in random order)
<?php $files = [ "folder/content1.php", "folder/content2.php", "folder/content3.php", "folder/content4.php", "folder/content5.php", "folder/content6.php", "folder/content7.php", "folder/content8.php" ]; shuffle($files); foreach ($files as $key => $file) { require($file); } ?>
Code 2 (get content from the page, starting from the 1305th character, only display 375 characters)
<?php $section1 = file_get_contents("folder/content1.php", FALSE, NULL, 1305, 375); echo $section1; ?> </p> <?php $section2 = file_get_contents("folder/content2.php", FALSE, NULL, 1305, 375); echo $section3; ?> </p> <?php $section3 = file_get_contents("folder/content3.php", FALSE, NULL, 1305, 375); echo $section3; ?> </p> <?php $section4 = file_get_contents("folder/content4.php", FALSE, NULL, 1305, 375); echo $section4; ?> </p> <?php $section5 = file_get_contents("folder/content5.php", FALSE, NULL, 1305, 375); echo $section5; ?> </p> <?php $section6 = file_get_contents("folder/content6.php", FALSE, NULL, 1305, 375); echo $section6; ?> </p> <?php $section7 = file_get_contents("folder/content7.php", FALSE, NULL, 1305, 375); echo $section7; ?> </p> <?php $section8 = file_get_contents("folder/content8.php", FALSE, NULL, 1305, 375); echo $section8; ?> </p>
My merge code doesn't work
<?php $files = [ "folder/content1.php", "folder/content2.php", "folder/content3.php", "folder/content4.php", "folder/content5.php", "folder/content6.php", "folder/content7.php", "folder/content8.php" ]; shuffle($files); foreach ($files as $key => $file) file_get_contents("$files", FALSE, NULL, 1305, 375); { require($file); } ?>
It might be easier to understand if you format your code better. Especially the last two lines are probably best written like this:
Then you can see the problem: the result of calling
file_get_contents()
is not assigned to any variable, so it just disappears; instead you see the lastrequire() The result of
, which passes the last value of$file
in the previousforeach
loop.Actually, the call to
file_get_contents()
looks problematic - the first argument should be a string, but you're passing it the name of an array embedded in the string, which is a bit Weird - I haven't tried it so not sure what to expect.In my opinion, whenever there is only one statement in a loop, it is better to use curly braces - it makes it easier to avoid errors.