jdb142 Posted November 25, 2007 Share Posted November 25, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/78819-small-php-include-problem-not-the-usual/ Share on other sites More sharing options...
willpower Posted November 25, 2007 Share Posted November 25, 2007 can you include your prod1 script please and a sample of the html where you include the include Quote Link to comment https://forums.phpfreaks.com/topic/78819-small-php-include-problem-not-the-usual/#findComment-398971 Share on other sites More sharing options...
runnerjp Posted November 25, 2007 Share Posted November 25, 2007 try require_once '../microADRotator.php'; Quote Link to comment https://forums.phpfreaks.com/topic/78819-small-php-include-problem-not-the-usual/#findComment-398990 Share on other sites More sharing options...
paradigmapc Posted November 25, 2007 Share Posted November 25, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/78819-small-php-include-problem-not-the-usual/#findComment-399009 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.