![]() |
PHP Question
I've been using the below php script to randomly take a single line from the file test.txt and insert it into a webpage.
Could someone tell me how I could add a variable X to this script so that I can set it to whatever I want, eg x=5. In this case, the script would take 5 lines from test.txt and insert it into a webpage. Thanks <?php $file = "test.txt"; $file_Content = file($file); if (count($file_Content) > 0) { $line = $file_Content[rand(0,count($file_Content)-1)]; echo $line; } ?> |
Quote:
|
PHP Code:
Hit me up @ 227230257 and ill help ya if im around :) |
Quote:
<?php $x = 5; // Your X variable $file = "test.txt"; $file_Content = file($file); if (count($file_Content) > 0) { for($i=0;$i<$x;$i++){ $line = $file_Content[rand(0,count($file_Content)-1)]; echo $line; } } ?> |
$file_Content = file("test.txt");
if (count($file_Content) > 0) for($i=0;$i<5;$i++) echo $file_Content[rand(0,count($file_Content)-1)]; short version :Graucho |
Quote:
Thanks, this works well. Two things I thought of after running your script. 1. How can I avoid duplicate lines being taken from test.txt. I'd like X number of lines, all are different. 2. How to add a break between the lines of data so that each line of text from test.txt is on it's own line. Currently the output is as follows: google yahoo google altavista msn Would like it as (note there are no duplicates and each is on it's own line): lycos yahoo altavista msn Thanks |
That code looks familiar :)
|
All times are GMT -7. The time now is 07:15 AM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123