Forum Moderators: coopster

Message Too Old, No Replies

using php to randomly pull a line of php variables from a txt file

         

jh20001

9:58 pm on Feb 22, 2009 (gmt 0)

10+ Year Member



I have tried using php to randomly pull a line of variables from a txt file, and then echo them back into my code. It will not pass the variables over though.

I am using the following on my page to pull a random line from a txt file an display the content:


<?php
$filename = $docroot.'/videos.txt';
$how_many_to_show = 1;
if ($fileContents = file($filename)) {
shuffle($fileContents);
for ($i = 0; $i < $how_many_to_show; $i++) {
print($fileContents[$i]);
}
} else {
die('Could not get contents of: ' . $filename);
}
?>
<object width="480" height="295"><param name="movie" value="http://www.youtube.com/v/<?php echo $vidurl; ?>&hl=en&fs=1&rel=0&fmt=22"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/<?php echo $vidurl; ?>&hl=en&fs=1&rel=0&fmt=22" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="295"></embed></object>
<br><br>
<font color="#CCCCCC" size="2" face="Verdana, Arial, Helvetica, sans-serif"><?php echo $vidcomment; ?></font>

Each line in the txt file contains something like:
<?php $vidurl='xokthY5zuPU'; $vidcomment='For more videos, visit our video section!'; ?>

So notice how the txt file is supposed to provide two variables...1) a youtube video id, and 2) Random Txt/comment. Then it should return the video id into the url of the code below the random script, as well as insert the second below it....allowing me to provide a special comment about that video (if needed...else I'd have the generic msg used in this post).

For the part of the random script that is: print($fileContents[$i]);

....I have tried print (doesnt seem to return the variables properly), echo (doesnt seem to return the variables properly), return (causes all code to stop from part of the page on...so thats bad)....

Nothing works. Everything ends with nothing being echo'd into the youtube url and comment area.

I could easily just put the full youtube code + br's and comment on each line of the txt file and pull it that way...which is how I have been doing it before this. But if I can get this to work...it would make adding video's to the random pick, so much easier (less tedious)

Am I doing something wrong? Thanks :)

penders

12:56 pm on Feb 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Providing you are successfully returning a random line of code from your text file of the form "<?php .... ?>" then your problem is that it needs to be evaluated as PHP. At the moment it is simply a string of plain text. Look at PHPs eval() [uk.php.net] function.

whoisgregg

3:11 pm on Feb 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It sounds like you have control over that text file so just create it differently to make this easier.

First, give it a .php extension. Then make that file look like this:

<?php
$vidarr = array();
$vidarr[] = array('url=> 'xokthY5zuPU', 'comment' => 'For more videos, visit our video section!');
$vidarr[] = array('url=> 'somethingelse', 'comment' => 'A different comment!');

Then, in the file you are pulling from, it's as easy as this:

<?php
$filename = $docroot.'/videos.php';
include($filename);
shuffle($vidarr);
echo $vidarr[0]['url'];
echo $vidarr[0]['comment'];

jh20001

6:34 pm on Feb 23, 2009 (gmt 0)

10+ Year Member



Great! What I did was...

1) change the txt file's extension to php...
2) in the random php script, I changed print to eval...
3) changed the lines in the txt to: $vidurl='xokthY5zuPU'; $vidcomment='For more videos, visit our video section!'; (basically I just took out the <?php and ?> tags)

Worked perfectly!

Thanks! You're a life saver! Well....Headache saver :)

franco190453

1:47 am on Feb 25, 2009 (gmt 0)

10+ Year Member



jh2001:

I have learned that if you have a txt file
with php code inside like this ->
video.txt
<?php
$vidurl='xokthY5zuPU';
$vidcomment='For more videos, visit our video section!';
?>
In your main file ->
if you include the video.txt
include('video.txt');
You can use the variables as if
they were declared in the main file.

Regards
Hope it helps you
Franco

jh20001

4:21 pm on Feb 25, 2009 (gmt 0)

10+ Year Member



franco190453,

This has already been solved.

I know including a file with variables would load the variables properly but that doesn't apply to this scenario. That would cause chaos with this one, being that the file contains multiple lines of the same variables.

The point was to randomly pick a line from the txt and include the variables, not include the entire txt file. Check out my last post though, confirming what I did based on the two responses before me (kind of a mix between the two). Chances are changing the file extension didn't help any but I like .php better than .txt anyways. It was all about eval() vs print().