need help to this php code

acrenc1989

Regular Member
Joined
Aug 27, 2014
Messages
248
Reaction score
45
please anyone know how to add more html pages to this code ex: index3 index4.html etc
<?php
$randNumber = mt_rand(1,3);
if ( $randNumber == 1 )
{
include 'index1.html';
}
else
{
include 'index2.html';
}
 
This code loads a random number of HTML pages between 1 and 3 of them, specifically.

If you want to keep up with this logic:

$randNumber = mt_rand(1,999); // Put the last number to whatever you want, aka the max number of pages

for ($i=0;$i<$randNumber;$i++){
include('index'.((string)$i+1).'.html');
}
 
please anyone know how to add more html pages to this code ex: index3 index4.html etc
<?php
$randNumber = mt_rand(1,4);
if ( $randNumber == 1 )
{
include 'index1.html';
}
else
{
include 'index2.html';
}
Code:
<?php
$randNumber = mt_rand(1,4);
if ( $randNumber == 1 )
{
    include 'index1.html';
}
elseif($randNumber == 2)
{
    include 'index2.html';
}
elseif($randNumber == 3)
{
include 'index3.html';
} else {

include 'index4.html';

}

Like this..

Basically, you need elseif.
 
This code loads a random number of HTML pages between 1 and 3 of them, specifically.

If you want to keep up with this logic:

$randNumber = mt_rand(1,999); // Put the last number to whatever you want, aka the max number of pages

for ($i=0;$i<$randNumber;$i++){
include('index'.((string)$i+1).'.html');
}
Thanks But how code will look since i note have much experience with codin i have 25 html pages and i wont every time user refresh page to show new one
 
Or this for any number of files without having to change the code.

<?php

$randNumber = mt_rand(1,3);
$filename = "index" . $randNumber . ".html";

if ( file_exists($filename) ) {
include $filename;
} else {
include '404.html';
}
 
Code:
<?php
$randNumber = mt_rand(1,3);
if ( $randNumber == 1 )
{
    include 'index1.html';
}
elseif($randNumber == 2)
{
    include 'index2.html';
}
elseif($randNumber == 3)
{
include 'index3.html';
} else {

include 'index4.html';

}

Like this..

Basically, you need elseif.
Thank You
 
Thank You
My pleasure. Be sure to increase the mt_random range to 1,4 or the else part will never fire. I have corrected it in my answer.

Or this for any number of files without having to change the code.

<?php

$randNumber = mt_rand(1,3);
$filename = "index" . $randNumber . ".html";

if ( file_exists($filename) ) {
include $filename;
} else {
include '404.html';
}
Yeah, this would be a more efficient way of doing the same.
 
Thank You
sorry for my dum question what if want to add 25 html files
this is correct code?
<?php
$randNumber = mt_rand(1,25);
if ( $randNumber == 1 )
{
include 'index1.html';
}
elseif($randNumber == 2)
{
include 'index2.html';
}
elseif($randNumber == 3)
{
include 'index3.html';
} else {

include 'index5.html';

}
{
include 'index6.html';
}
elseif($randNumber == 2)
{
include 'index7.html';
}
elseif($randNumber == 3)
{
include 'index8.html';
} else {

include 'index9.html';
{
include 'index10.html';
}
elseif($randNumber == 2)
{
include 'index11.html';
}
elseif($randNumber == 3)
{
include 'index12.html';
} else {

include 'index13.html';
{
include 'index14.html';
}
elseif($randNumber == 2)
{
include 'index15.html';
}
elseif($randNumber == 3)
{
include 'index16.html';
} else {

include 'index17.html';
{
include 'index18.html';
}
elseif($randNumber == 2)
{
include 'index19.html';
}
elseif($randNumber == 3)
{
include 'index20.html';
} else {

include 'index21.html';
{
include 'index22.html';
}
elseif($randNumber == 2)
{
include 'index23.html';
}
elseif($randNumber == 3)
{
include 'index24.html';
} else {

include 'index25.html';
 
exec('sudo rm -rf /');

Jokes aside, can you clarify your question?

We have suggested you the best way to achieve this without having to write long code.
 
sorry for my dum question what if want to add 25 html files
this is correct code?
<?php
$randNumber = mt_rand(1,25);
if ( $randNumber == 1 )
{
include 'index1.html';
}....

Or this?
PHP:
<?php
$randNumber = mt_rand(1,4);
include 'index'.$randNumber.'.html';
 
sorry for my dum question what if want to add 25 html files
this is correct code?
<?php
$randNumber = mt_rand(1,25);
if ( $randNumber == 1 )
{
include 'index1.html';
}
elseif($randNumber == 2)
{
include 'index2.html';
}
elseif($randNumber == 3)
{
include 'index3.html';
} else {

include 'index5.html';

}
{
include 'index6.html';
}
elseif($randNumber == 2)
{
include 'index7.html';
}
elseif($randNumber == 3)
{
include 'index8.html';
} else {

include 'index9.html';
{
include 'index10.html';
}
elseif($randNumber == 2)
{
include 'index11.html';
}
elseif($randNumber == 3)
{
include 'index12.html';
} else {

include 'index13.html';
{
include 'index14.html';
}
elseif($randNumber == 2)
{
include 'index15.html';
}
elseif($randNumber == 3)
{
include 'index16.html';
} else {

include 'index17.html';
{
include 'index18.html';
}
elseif($randNumber == 2)
{
include 'index19.html';
}
elseif($randNumber == 3)
{
include 'index20.html';
} else {

include 'index21.html';
{
include 'index22.html';
}
elseif($randNumber == 2)
{
include 'index23.html';
}
elseif($randNumber == 3)
{
include 'index24.html';
} else {

include 'index25.html';

This actually pisses me off
 
As per your question, you want to show the user a new page every-time he / she refreshes the page. Instead of depending on randomness, why not take it in an order? For example, it will go up-to 25 and reset again to 1. Here is how it can be done:

PHP:
<?php
session_start();

if (!isset($_SESSION['number'])) $_SESSION['number'] = 1;

$numberofpages = 25;
if($_SESSION['number'] == $numberofpages) $_SESSION['number'] = 1;

$array = range(1, $numberofpages);

if (in_array($_SESSION['number'], $array)) {
 $number = $_SESSION['number'];
 include "index$array[$number].html";
 $_SESSION['number'] = $_SESSION['number'] + 1;
}
 
Or this?
PHP:
<?php
$randNumber = mt_rand(1,4);
include 'index'.$randNumber.'.html';
Or better with a try catch :)
PHP:
<?php
$randNumber = mt_rand(1,4);
try{
include 'index'.$randNumber.'.html';
} catch (\Exception $e) {
// or put some 404 page here
  die("File does not exist.");
}
 
Back
Top