1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Random include PHP code help?

Discussion in 'PHP & Perl' started by chickenugget, Mar 12, 2009.

  1. chickenugget

    chickenugget Newbie

    Joined:
    Feb 24, 2009
    Messages:
    47
    Likes Received:
    10
    Occupation:
    Food
    Location:
    A deep fat fryer at McDonalds
    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" ; ?>";     
    }
    ?>
     
  2. demoniox

    demoniox Registered Member

    Joined:
    Mar 5, 2007
    Messages:
    98
    Likes Received:
    83
    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");     
    ?>
    
     
  3. rayray

    rayray Newbie

    Joined:
    Sep 13, 2008
    Messages:
    45
    Likes Received:
    6
    Location:
    germany
    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
     
    • Thanks Thanks x 1
  4. chickenugget

    chickenugget Newbie

    Joined:
    Feb 24, 2009
    Messages:
    47
    Likes Received:
    10
    Occupation:
    Food
    Location:
    A deep fat fryer at McDonalds
    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?
     
  5. rayray

    rayray Newbie

    Joined:
    Sep 13, 2008
    Messages:
    45
    Likes Received:
    6
    Location:
    germany
    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 ?
     
  6. BlackMelvyn

    BlackMelvyn Regular Member

    Joined:
    Jul 8, 2008
    Messages:
    202
    Likes Received:
    272
    Home Page:
    or
    PHP:
    <?php 
        
    if (rand(1,4) == 1){
          echo 
    nl2br(file_get_contents("file1.txt"));
        } 
    ?>
     
    Last edited: Mar 23, 2009
  7. chickenugget

    chickenugget Newbie

    Joined:
    Feb 24, 2009
    Messages:
    47
    Likes Received:
    10
    Occupation:
    Food
    Location:
    A deep fat fryer at McDonalds
    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");
         } 
    ?>
     
  8. aReJay

    aReJay Power Member

    Joined:
    Apr 29, 2009
    Messages:
    735
    Likes Received:
    237
    Location:
    Down under
    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");
          } 
    ?>