Random include PHP code help?

chickenugget

Newbie
Joined
Feb 24, 2009
Messages
47
Reaction score
10
I am complete noob when it comes to coding and am trying to display a random lead merchant on my page each time the page is loaded.

Below is the script so far - I can't seem to get it to pull in the actual 'merchant.txt' file though when the random number is equal to 1.

Any help is much appreciated.

Code:
<?
    $random_number = rand(1,4);
    if($random_number = 1){
    echo "<?php include "/merchant1.txt" ; ?>";     
}
?>
 
First, the = is the assignation operator, and == is the comparasion operators

second..merchant1.txt is a php file or a html?

try this
Code:
<?
    $random_number = rand(1,4);
    if($random_number == 1)
        echo file_get_contents("merchant1.txt");     
?>
 
Code:
<?php
srand();
$files = array("file1.html", "file2.txt", "file3.php");
$rand = array_rand($files);
include ($files[$rand]);
?>

change the filenames in the array for your needs. should work with html, txt and php files
 
Code:
<?php
srand();
$files = array("file1.html", "file2.txt", "file3.php");
$rand = array_rand($files);
include ($files[$rand]);
?>
change the filenames in the array for your needs. should work with html, txt and php files

How do you assign the random number that you want to files to be outputted from e.g. I want it to pick a random number between 1 and 4 and only display the files when it picks 1?
 
with this code, the file file1.txt is only display/executed when the number 1 is generated, otherwise a 2,3 or 4 is generated and nothing happens
PHP:
<?php 
$i = rand(1,4); 
    if ($i==1){
	  include ("file1.txt");
    } 
?>

is this want you want ?
 
or
PHP:
<?php 
    if (rand(1,4) == 1){
      echo nl2br(file_get_contents("file1.txt"));
    } 
?>
 
Last edited:
with this code, the file file1.txt is only display/executed when the number 1 is generated, otherwise a 2,3 or 4 is generated and nothing happens
PHP:
<?php 
$i = rand(1,4); 
    if ($i==1){
      include ("file1.txt");
    } 
?>
is this want you want ?

Can you do multiple calls with this e.g.

Code:
<?php 
$i = rand(1,4); 
    if ($i==1){
      include ("file1.txt");
    } 
if ($i==2){
       include ("file2.txt");
     } 
?>
 
You can do multiple queries however try this format

PHP:
<?php 
$i = rand(1,4); 
    if ($i==1){
       include ("file1.txt");
     }elseif ($i==2){
        include ("file2.txt");
      } 
?>
 
Back
Top