GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   PHP Question (https://gfy.com/showthread.php?t=180285)

version4 09-27-2003 10:51 PM

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;
}
?>

SicChild 09-27-2003 11:14 PM

Quote:

Originally posted by version4
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;
}
?>

5 lines after each other? or each one random?

kad 09-27-2003 11:15 PM

PHP Code:


<?php
    $file        
"test.txt";
    
$file_Content    file($file);

    
$j 0;
    
$limit 5;

    while (
$j $limit) {
    
      if (
count($file_Content) > 0)    {
          
$line $file_Content[rand(0,count($file_Content)-1)];
    
      echo 
$line;
      }
      
      
$j++;

  }
?>

Try that. Chances are it will spit out the same line multiple times, but you could get around that with with a few extra lines of code.

Hit me up @ 227230257 and ill help ya if im around :)

BigFish 09-27-2003 11:16 PM

Quote:

Originally posted by version4
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;
}
?>


<?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; }
}
?>

Lane 09-27-2003 11:24 PM

$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

version4 09-27-2003 11:35 PM

Quote:

Originally posted by BigFish



<?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; }
}
?>

Bigfish,

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
google
yahoo
altavista
msn

Thanks

JSA Matt 09-27-2003 11:37 PM

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