Database driven affiliate website

Get ready for a FREE database driven affiliate website

That's right, hold on to your hat, I'm going to show you and give you (at least one lucky person) their own database driven affiliate website - a BETTER than the normal type of database driven website - one that makes you money.

Sign up for my mailing list and you are entered to win this website site I will be creating live ... leave comments ... subscribe to this feed ... tell your friends ... the more participation, the more you get!

First name

E-mail address

Random Text

Brian asked the following question in a reply to the random image post I made.

How do you do the same for random snippets, or lines of text that you have in one txt file?

I thought it was a very good question considering that I am working on a database driven affiliate website - the ability to pull in random info is useful, in particular if I am interested in having "unique" content - and I am interested.

So I thought I would share how to pull in random snippets for everyone to see.

For this to work, we need to make a few assumptions.

In this example, you will need to have a text file with your snippets in the same directory that that you want to display the snippets in.

The file should be readable (most are by default).

And finally, each snippet is a separate line (which means that a carriage return was typed at the end of each snippet) - the importance of this is shown further down the page ..

Contents of your text file should look like something like this:

This is the first sentence.
The second sentence is here.
Read the third sentence.

Now we want the php script to (1) open the file and (2) read in the contents.

Here's what that looks like:

<?php

$file 
"file.txt";

$fh fopen($file'r');

$content fread($fhfilesize($file));

?>




Next step is to dump the results into an array.

<?php

$contents 
explode("\n"trim($content));

?>




For those interested, this little line of code says to clean up the info from the variable $content (trim), break the information into pieces every time a carriage return shows up (explode and \n), and put the pieces into an array called $contents.

Now, just print out a random selection (array_rand) from that newly created array.

<?php

print $contents[array_rand($contents)];

?>



Magic!

Here's what the final script looks like.

<?php

$file 
"file.txt";

$fh fopen($file'r');

$content fread($fhfilesize($file));

$contents explode("\n"trim($content));

print 
$contents[array_rand($contents)];

?>




This is something you can insert in your php pages (or php enabled html pages) to display random snippets of text. Like I said, stuff like this is what you can use to make the content of your web pages unique - especially if your are using similar content to others, like database driven affiliate sites.


Share: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Sphinn
  • Mixx
  • StumbleUpon
  • MisterWong

19 Responses to “Random Text

  • 1
    Brian
    March 5th, 2008 07:29

    Wow, you aren't a BOT.....you are a real person who reads the comments!

    Thanks for answering the question because I can put this to work in many ways now!

    Best,
    Brian

  • 2
    Brian
    March 5th, 2008 07:43

    Okay, 2 questions now come up after thinking through this:

    1. What if I want my text files in a directory other than the same directory where the text snippets are going to be added?

    I want to keep all my text files in one centralized directory somewhere near the root of my server. I will name the text files by product or page name.

    ----------------------------------------------------------

    2. How can I add a random number and random selection of lines from a text file?

    If I have my text file in a directory located off the root of my server, I need the PHP code to call from "1 to X" random number of random lines from that file and add it.

    ----------------------------------------------------------

    Now that would make my day....better yet, ...it would make my year!

  • 3
    admin
    March 5th, 2008 08:37

    @Brian,

    1. If you would like to have your text files in a directory other than the same directory where the text snippets are going to be added, then you need to specify the absolute path that the text file is located at.

    WAS:

    $file = “file.txt”;

    IS:

    $file = “/home/username/public_html/subdirectory/file.txt”;

    OR

    $file = $_SERVER['DOCUMENT_ROOT']."/subdirectory/file.txt";

    The first "IS" choice is typical of a site that uses CPanel. You will need to make the appropriate substitutions (username, subdirectory, and file.txt).

    The second lets the php script tell you the root directory - you still need to replace subdirectory and file.txt with the appropriate names.

    2. Not sure I understand the question completely. Why do you want to add a random number and the last part of your question "and add it" - what does that mean?

    If you are interested in displaying a random number of snippets at one time, you can wrap a loop around the print statement. If that is what you are interested in, let me know and I'll code it up.

  • 4
    Brian
    March 5th, 2008 16:14

    Dave, thanks again.

    Let's assume I write 20 lines of text snippets for a keyword, or product description. I put all those into one text file, one snippet per line with a return after each snippet.

    Now, I want the script to select between 2 to 5 (any variable from 1 to 20) of the snippets and display them as text. The 2 to 5 snippets will come from the 20 snippets so they would be randomly selected and not sequential, right?

    Please, keep throwing these little tidbits of golden information out to us, because the "database driven affiliate site" with "custom content" will be a tool to master!

  • 5
    admin
    March 5th, 2008 17:45

    Hi Brian,

    This will do what you are asking for.

    $file = "file.txt";
    $min = 2;
    $max = 5;
    $fh = fopen($file, "r");
    $content = fread($fh, filesize($file));
    $contents = explode("\n", trim($content));
    if ($max > count($contents)) $max = count($contents);
    $contentnew = array_rand($contents,rand($min,$max));
    foreach ($contentnew as $number) {
    print $contents[$number]." ";
    }

    Give it a whirl.

    Dave

    P.S. - Be sure to enclose with the opening and closing php tags, < ?php and ?>

  • 6
    Brian
    March 5th, 2008 18:07

    Thanks..... I am learning and will put this to use.

    Now give us something more to chew on! My appetite for learning your database driven affiliate website is voracious!

  • 7
    Linkvana
    March 6th, 2008 14:05

    I've learnt more in the 5 minutes to read this post than I have in ages and reading any number of "affiliate marketing" ebooks - more poer to your keyboard Dave - nice one.

    Mike

  • 8
    Robert Plank
    March 6th, 2008 19:28

    BTW Dave instead of this line:
    $contents = explode(”\n”, trim($content));

    I tend to do:
    $contents = array_map("trim", file($content));

    Not to nitpick, both of those work but the 2nd way is less hairy for me to do over... and over... and over....

  • 9
    admin
    March 6th, 2008 19:58

    Thanks Robert ... old ways die hard. Dave

  • 10
    Brian
    March 8th, 2008 06:32

    Nothing GREAT is ever written.....only rewritten!

  • 11
    Brian
    March 9th, 2008 06:37

    I tried the code on my site and I am getting error messages. I have attempted a few things to see what I may be doing wrong perhaps in my template but no luck in getting it resolved.

    The error message was indicating that there was an invalid character in this line:

    $contents = explode("\n", trim($content));

    Specifically it was the " \ " character.

    I can't figure it out.

  • 12
    admin
    March 9th, 2008 07:15

    Hi Brian,

    Sorry about that.

    It was pointed out by someone else that copy and pasting PHP code from my blog would not work as Wordpress puts some strange characters into the code I post.

    Since then I have modified wordpress to allow copy/paste properly.

    What is posted in the comment above and here again should work. If it still doesn't, let me know, and I'll zip it up and send it over.

    Thanks.

    Dave

    $file = "file.txt";
    $min = 2;
    $max = 5;
    $fh = fopen($file, "r");
    $content = fread($fh, filesize($file));
    $contents = explode("\n", trim($content));
    if ($max > count($contents)) $max = count($contents);
    $contentnew = array_rand($contents,rand($min,$max));
    foreach ($contentnew as $number) {
    print $contents[$number]." ";
    }

  • 13
    Simonne
    March 9th, 2008 13:25

    This is interesting. I'm currently running a database driven affiliate website, and I've always customized my pages manually. But doing it your way, things can happen faster.

  • 14
    Brian
    March 9th, 2008 18:46

    Hey Dave,

    I just used that new code that you put up. Loaded onto my website and everything is working perfect.

    The possible ways to use this are incredible for developing unique content.

    ------------------------

    I am asking a lot here, but why should I stop? Your information is great!

    How can I take datafeed paragraphs from a vendor and automatically break them into sentences (break at the period) and add a carriage return at the end of each period so that the vendor datafeed ends up as individual lines in my new text file?

    Then I can simply open that file and add some more text lines relevant about that particular product and use your script to reassemble the text. This would be a fast way to randomize the orginal datafeed content with lots of unique content lines mixed in.

  • 15
    Brian
    March 9th, 2008 18:57

    Another question in this code. Robert Plank suggested to use this line in the code:

    $contents = array_map("trim", file($content));

    Should I replace your line: $contents = explode("\n", trim($content)); with Robert's suggested code?

    I am not sure what he meant by " but the 2nd way is less hairy for me to do over... and over... and over.... " ?

    I didn't know PHP had hair!

  • 16
    admin
    March 9th, 2008 19:27

    Brian,

    Regarding this: "I am asking a lot here, but why should I stop? Your information is great!

    How can I take datafeed paragraphs from a vendor and automatically break them into sentences (break at the period) and add a carriage return at the end of each period so that the vendor datafeed ends up as individual lines in my new text file?

    Then I can simply open that file and add some more text lines relevant about that particular product and use your script to reassemble the text. This would be a fast way to randomize the orginal datafeed content with lots of unique content lines mixed in."

    I plan on doing something similar to this for the datafeed site I am creating - break up the sentences that that vendor provides and rearranging them.

    Dave

  • 17
    admin
    March 9th, 2008 19:28

    Brian,

    QUESTION: Should I replace your line: $contents = explode("\n", trim($content)); with Robert's suggested code?

    I am not sure what he meant by " but the 2nd way is less hairy for me to do over... and over... and over.... " ?

    ANSWER: I tried this code and didn't get it to work for me - and I didn't go any further with it.

    Dave

  • 18
    Robert Plank
    March 10th, 2008 11:00

    Sorry, typo in the code. It should be:

    $contents = array_map("trim", file($file));

    Doing it that way cuts out a few steps. You avoid the calls to fopen, fread, and that way works even on files edited on a Mac (they use "\r") or if line breaks are delimited in ASCII (using "\r\n" instead of just "\n").

    Your longer way still works, but it's more unnecessary code.

  • 19
    Brian
    March 11th, 2008 13:02

    Robert,

    I tested your line of code and it worked fine.

    How can I tell, which line of code is the prefered to use on my website? You indicate that it cuts out a few steps and works with files edited on a Mac.

    Based on that idea, I will use your code Robert.

    Dave?

Leave a Reply