Php combine 2 codes (random + length)

Viewed 22

I need some help from you guys to make this work. I need to combine 2 php codes to work together. I am a beginner in php, I appreciate to the maximum every advice received from you. Thank you in advance!

Description: I want to display content from 8 pages (some text) and display the taken content in random order in my index.php I don't know how to select start line from which to retrieving the text data from the pages, that's why I picked the character option (starts after 1305 characters and count 375 characters).

Code 1 (pick 8 pages and show 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 (take content from page after count 1305 CHARACTERS and show only 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 combined codes are not working

<?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); }
?>

1 Answers

If you format your code better, it might be easier to understand. The last two lines in particular might be better written like this:

foreach ($files as $key => $file) {
    file_get_contents("$files", FALSE, NULL, 1305, 375);
}

require($file);

And then you can see the problem: the result of the file_get_contents() call is not being assigned to anything, so it just disappears; and what your seeing is the result of the last require(), which is being passed the last value of $file from the previous foreach loop.

In fact, the file_get_contents() call looks wrong - the first argument should be a string but you're passing the name of an array embedded in a string, which is a bit weird - I haven't tried it so I'm not sure what will happen.

IMHO it's a good idea always to use curly brackets in loops, even when there's only one statement inside it - it's easier to avoid mistakes by doing that.

Related