Need some help - pick random lines from text files & email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
StudioWorks
Forum Newbie
Posts: 7
Joined: Wed Mar 08, 2017 9:34 am

Need some help - pick random lines from text files & email

Post by StudioWorks »

Hello everybody,


I am kind of new to this so I am hoping that you can give me a hand. The task is this:

I have 5 .txt files, each file contains 10 lines of text. I have to randomly select one line from each file and the these generated lines have to be sent through email to two email addresses.

Can you please provide some pointers ?

Thank you!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need some help - pick random lines from text files & ema

Post by Celauran »

What have you done so far and where are you encountering issues?
StudioWorks
Forum Newbie
Posts: 7
Joined: Wed Mar 08, 2017 9:34 am

Re: Need some help - pick random lines from text files & ema

Post by StudioWorks »

So fa I can select random lines from one .txt files however I am having troubles selecting from multiple .txt files, don't know exactly how to do it.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need some help - pick random lines from text files & ema

Post by Celauran »

How are you selecting from the one file? How are you storing the results? Do you have a list of files you could select from?
StudioWorks
Forum Newbie
Posts: 7
Joined: Wed Mar 08, 2017 9:34 am

Re: Need some help - pick random lines from text files & ema

Post by StudioWorks »

So far only have this to select a random line from just one file:

<?php
$content = file("file.txt");
$line = $content[rand(0, count($content) - 1)];
?>


As i said i don't know how to handle this with multiple files or how to send the result through email..
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need some help - pick random lines from text files & ema

Post by Celauran »

What if you used an array to store the list of filenames and another array to store the randomly selected lines?
StudioWorks
Forum Newbie
Posts: 7
Joined: Wed Mar 08, 2017 9:34 am

Re: Need some help - pick random lines from text files & ema

Post by StudioWorks »

Hey,
And how would I do that ? As i said, i'm new to this. I'm not expecting something for free, I can pay for your help if you want.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need some help - pick random lines from text files & ema

Post by Celauran »

I'm not looking for payment. I'm trying to help you arrive at the answer yourself rather than doing your homework for you.
StudioWorks
Forum Newbie
Posts: 7
Joined: Wed Mar 08, 2017 9:34 am

Re: Need some help - pick random lines from text files & ema

Post by StudioWorks »

Hey, thank you for your help! i would gladly stay and study this, it's not homework it's a favor for a friend and this is not exactly my area and it needs to be done by tomorrow, that's why the desperation :)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need some help - pick random lines from text files & ema

Post by Celauran »

Instead of hardcoding "file.txt" into your file() call, create an array of all the files and iterate over it. Create a separate array for the lines you retrieve and push new items into the array with each iteration.

Something like this should give you an idea

Code: Select all

$files = ['file.txt', 'file2.txt', 'file3.txt'];
$lines = [];

foreach ($files as $file) {
    $contents = file($file);
    array_shuffle($contents);
    $lines[] = array_pop($content);
}
StudioWorks
Forum Newbie
Posts: 7
Joined: Wed Mar 08, 2017 9:34 am

Re: Need some help - pick random lines from text files & ema

Post by StudioWorks »

I will give this a try and let you know how it goes!! Thank you so much for your help and patience!! ( I wasn't joking when I said that i don't expect things for free! )
StudioWorks
Forum Newbie
Posts: 7
Joined: Wed Mar 08, 2017 9:34 am

Re: Need some help - pick random lines from text files & ema

Post by StudioWorks »

Thank you, you've been very helpful. I started out from your advice and I managed to get the job done.

Thank you again!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Need some help - pick random lines from text files & ema

Post by Celauran »

Glad to hear it!
Post Reply