Jump to content

Recommended Posts

Hi, I am new here and have a problem with some php code that I am using on my site.

 

There was a PHP tutorial on the net that showed you how to make an advert randomiser. I copied the php code (allowed) and did exactly as it said. In the form it was in it referred to a .txt file for image references, no database. I just replaced the image reference with a '<?php include("prod1.php"); ?>', prod1.php being a file which contains basic layout, text and an image.

 

The code for the ad randomiser is:

 

<?php
// Function to display random ads from a list 
function showRandomAD($ADnumber = 1){
    // Loading the ad list into an array
    $adList = file('adlist.txt');
    
    // Check the total number of ads
    $numberOfADs = sizeof($adList);
   
    // Initialize the random generator
    list($usec, $sec) = explode(' ', microtime());
    srand((float) $sec + ((float) $usec * 100000));
   
    // Initialize the ads counter
    $adCount = 0;
    
    // Loop to display the requeste number of ads
    while ($adCount++ < $ADnumber) {
        // Generate random ad id
        $actAD = rand(0, $numberOfADs-1);
        // Display the above generated ad from the array
        echo $adList[$actAD].'<br/>';
    }
}
?> 

 

It then said to include the following a the top of any pages it needed to be on:

 

<?php
    require_once('microADRotator.php');
?>

 

Then use: <?php showRandomAD(1); ?>        where the ad should go.

 

In the .txt file I replaced: '<a href="#">Demo Text Link< /a>' with '<?php include("prod1.php"); ?>'

 

 

When I load my site and look at the page's source instead of including the code within prod1.php it just writes the include. Where the ads should be the page is just blank.

 

I can't work it out, I have tried a php file instead of a text file but no luck. The thing that confuses me is that the random generator script is working, it seems to just be what it is fetching is the problem.

 

Any help you could give would be great, if any of it is poorly explained then please tell me and I will try to explain it better.

 

Thanks in advance,

Joe

Link to comment
https://forums.phpfreaks.com/topic/78819-small-php-include-problem-not-the-usual/
Share on other sites

Your txt file can't have php code in it because when it is imported via file() method, the php code is being inserted into an array of strings and it is not being parsed. When the page is being displayed it shows the php code as text because it was never parsed.

 

You also have a problem with the generation of a random number because it does not specify numbers that have already been generated, which would give you duplicate ads.

 

In order to make this work like you want you would have to rewrite the function and probably utilize the ob_start() method and use a adlist.php instead.

 

Sorry I cant be more help

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.