SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Addict fut's Avatar
    Join Date
    Dec 2004
    Location
    New York
    Posts
    264
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Random Text Rotation?

    Anyone have available a simple text rotation script where on every page load the given text is selected randomly?

    Sort of like banner rotation but with regular text.

    Thanks.
    ATTENTION: Affiliates in the Health Niche
    $297 Product rigorously tested to at least 2% Conversion Rate
    PM for details.

  2. #2
    SitePoint Member
    Join Date
    Jul 2005
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    solution

    We can give you this script . As here at Xaprio Solutions we have a experts who can do this kind of work.
    So, just contact us at our site. <advisor edit>Removed self promotional link</advisor edit>

    Regards
    Ramneek Chadha
    Last edited by Helge; Jul 23, 2005 at 09:27.

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2005
    Location
    Venlo, the Netherlands
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Contact you for 3 simple statements?

    PHP Code:
    <?php
    $aText 
    = array(=> 'tekst1',
                   
    => 'tekst2',
                   
    => 'tekst3',
                   
    => 'etc');

    $iNumText count($aText);

    echo 
    $aText[rand(1$iNumText)];
    ?>
    Enjoy bud

  4. #4
    SitePoint Enthusiast Durinthiam's Avatar
    Join Date
    Jan 2005
    Location
    www.raidshout.com
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Get a quote for simple code? come on dude, dont shark!

    PHP Code:
    <? 
    srand
    ((double)microtime()*1000000); 
    $arry_txt=preg_split("/--NEXT--/",join('',file("text.txt"))); 
    echo
    $arry_txt[rand(0,sizeof($arry_txt)-1)]; 
    ?>
    Put that where you want the text, then make a new file in the same dir called "text.txt" and space it like so

    1234 I love Marine Corps!
    --NEXT--
    I don't know but I've been told, eskimo's are mighty cold
    --NEXT--
    hmmm goood
    --NEXT--
    so good
    --NEXT--
    good for you
    --NEXT--
    good for me
    tadaa

  5. #5
    SitePoint Addict fut's Avatar
    Join Date
    Dec 2004
    Location
    New York
    Posts
    264
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I was just about to laugh at this guy!
    ATTENTION: Affiliates in the Health Niche
    $297 Product rigorously tested to at least 2% Conversion Rate
    PM for details.

  6. #6
    I'm in your computer! Halo-13's Avatar
    Join Date
    Jun 2004
    Location
    Minneapolis, MN
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Durinthiam
    Get a quote for simple code? come on dude, dont shark!

    PHP Code:
    <? 
     srand
    ((double)microtime()*1000000); 
     
    $arry_txt=preg_split("/--NEXT--/",join('',file("text.txt"))); 
     echo
    $arry_txt[rand(0,sizeof($arry_txt)-1)]; 
     
    ?>
    Put that where you want the text, then make a new file in the same dir called "text.txt" and space it like so



    tadaa
    I just used this to create a random property generator on a REALTOR's home page and it worked out great! Great post, Thanks a million~!
    Halo-13 Network
    ArcadeApe

  7. #7
    SitePoint Addict
    Join Date
    Jul 2002
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a ton. This just saved me a s--tload of time.

  8. #8
    I'm in your computer! Halo-13's Avatar
    Join Date
    Jun 2004
    Location
    Minneapolis, MN
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Durinthiam View Post
    Get a quote for simple code? come on dude, dont shark!

    PHP Code:
    <? 
    srand
    ((double)microtime()*1000000); 
    $arry_txt=preg_split("/--NEXT--/",join('',file("text.txt"))); 
    echo
    $arry_txt[rand(0,sizeof($arry_txt)-1)]; 
    ?>
    Put that where you want the text, then make a new file in the same dir called "text.txt" and space it like so



    tadaa
    Any way to get this to pull 3 different lines from the text.txt file and have them display at random?
    Halo-13 Network
    ArcadeApe

  9. #9
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, this is my first post. I registered just to get my script out there .

    I have created this script from scratch for you. It is a lot more flexible than a text file so it should suite your needs a lot better. Maybe not with this project, maybe with the next.

    Please look at the last script in this post for the script for grabbing multiple random text entries from a database. This first one has most of the explanations included. This makes it easier to understand the second one when you aren't sure about what it does.

    PHP Code:
    <?php
    //Welcome to random text reader by Achilleas Buisman
    //Use this script in any way you want, I would of course appreciate it
    //if you kept the disclaimer at the bottom intact and present.

    //The database connection info.
        
    $host "";
        
    $user "";
        
    $pass "";
        
    $dbname"";

        
    $db mysql_connect($host,$user,$pass) or die(mysql_error());
        
    mysql_select_db($dbname$db) or die (mysql_error());
    //You can best place the above in a 'config.php' file or something and then include it with require_once('config.php'.

    //Time to read how many lines there are in the table with the text you want to rotate, thus determining from
    //where to where to run the random generation.
    //Enter the table name where you keep the text in this case: 'texts'    
        
    $query ="SELECT * FROM texts;"
        
    $query_result mysql_query($query$db) or die (mysql_error());
    //$number_texts will be given the value of the amount of texts to cycle through
        
    $number_texts mysql_num_rows($query_result);
    //Now the query will be made that selects what text to find in the database
        
    $text_id mt_rand(1$number_texts);
        
    $random_query_format "SELECT * FROM texts WHERE text_id='%s';";
    //The variable will now be entered into the query
        
    $query sprintf($random_query_format$text_id);
        
    $random_result mysql_query($query$db) or die (mysql_error());
    //Now it is time to echo all the data from the fetched text
        
    $text mysql_fetch_array($random_result);
    //The variable $text is now an array, the keys are named after the mysql database columns of the table 'texts'
    //E.g. the table 'texts' has 3 columns. 'text_id' (needed), 'text', 'link'
    //To write the data from the table onto your page do the following.
            
    echo($text['text']);
            echo(
    $text['link']);
    //You can of course format this into tables in any way you want.
    //Also you can create any amount of columns you want, simply call them to the page by
    //Entering it inside the $text[''] and echo it as I did above, then it should all work.

    //Disclaimer:
    //Random MySQL data fetcher v1.0
    //Created by: Achilleas Buisman
    //Freeware

    //Please include the disclaimer in the version you are going to use.
    ?>
    The script requires a mysql database to get the data from. You need to use the following commands to create the table and insert data:
    Code:
    The mysql commands to create the tables that are required are:
    CREATE TABLE texts 
    (text_id INT(10) PRIMARY KEY AUTO_INCREMENT, 
    text TEXT, 
    link VARCHAR(200)
    );
    
    To enter data into the database you can use:
    INSERT INTO texts (text, link) VALUES ('TEXT1.', 'LINK1');
    To understand the script a little bit better this is what you get when the script executes the following:
    Code:
    mysql> SELECT * FROM texts;
    +---------+--------+-------+
    | text_id | text   | link  |
    +---------+--------+-------+
    |       1 | TEXT1. | LINK1 |
    |       2 | TEXT2. | LINK2 |
    |       3 | TEXT3. | LINK3 |
    |       4 | TEXT4. | LINK4 |
    |       5 | TEXT5. | LINK5 |
    |       6 | TEXT6. | LINK6 |
    |       7 | TEXT7. | LINK7 |
    |       8 | TEXT8. | LINK8 |
    +---------+--------+-------+
    8 rows in set (0.00 sec)
    
    And:
    
    mysql> SELECT * FROM texts WHERE text_id='5';
    +---------+--------+-------+
    | text_id | text   | link  |
    +---------+--------+-------+
    |       5 | TEXT5. | LINK5 |
    +---------+--------+-------+
    1 row in set (0.00 sec)
    Now you can see the names of the columns above and you can use those in the array "$text['']" as I commented in the script.

    You can find an excellent and very complete yet quick to work through tutorial on mysql here. You can find more info here about for example creating tables, databases, etc.

    Now I can very easily adjust the script to find 3, 4, 5, etc random entries from the database. Here you go:

    PHP Code:
    <?php
    //Welcome to random text reader by Achilleas Buisman
    //Use this script in any way you want, I would of course appreciate it
    //if you kept the disclaimer at the bottom intact and present.

    //The database connection info.
        
    $host "";
        
    $user "";
        
    $pass "";
        
    $dbname"";

        
    $db mysql_connect($host,$user,$pass) or die(mysql_error());
        
    mysql_select_db($dbname$db) or die (mysql_error());
    //script
    function random_text($want$db){
      
    $fetch_want $want++;
          
    $query ="SELECT * FROM texts;"
        
    $query_result mysql_query($query$db) or die (mysql_error());
        
    $number_texts mysql_num_rows($query_result);
        if(
    $number_texts $want){  //There aren't that many unique entries in the database
          
    $fetch_want $number_texts++;
        }
        elseif(
    $number_texts >= $want){
          }
    for(
    $i0$i $fetch_want$i++){       
        
    $query ="SELECT * FROM texts;"
        
    $query_result mysql_query($query$db) or die (mysql_error());
        
    $number_texts mysql_num_rows($query_result);
        
    $text_id mt_rand(1$number_texts);
        
    $random_query_format "SELECT * FROM texts WHERE text_id='%s';";
        
    $query sprintf($random_query_format$text_id);
        
    $random_result mysql_query($query$db) or die (mysql_error());
        
    $text mysql_fetch_array($random_result);
            echo(
    $text['text']);
            echo(
    $text['link']);
    }
    }
    //Simply include the above code in an external file that you then include with include_once('include.php');
    //Then wherever you want to have lets say 4 random entries from the database use the following to echo it to the page:
    random_text('5'$db);


    //Disclaimer:
    //Random MySQL data fetcher v1.5
    //Created by: Achilleas Buisman
    //Freeware

    //Please include the disclaimer in the version you are going to use.
    ?>
    Did you read that correctly? You now only need to enter random_text('4' ,$db); for 4 random texts... And if you would like 100 you simply change the 4 to '100'? Yep thats right. Ultimate flexibility. The script simply gets the entered amount of random entries from the database and every single one of them is generated independent from the other.

    I hope this will help you and anyone else of course.
    Last edited by Mosselman; Mar 9, 2007 at 09:01.

  10. #10
    SitePoint Zealot Bill Palmer's Avatar
    Join Date
    Oct 2005
    Location
    London, UK
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm seeing a disturbing lack of mt_rand() here.

    For when you need a truly random string of text.

  11. #11
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Bill Palmer View Post
    I'm seeing a disturbing lack of mt_rand() here.

    For when you need a truly random string of text.
    Well since there isn't such a thing as true randomness, that shouldn't be a problem. Either there something is random or it isn't, but fair enough simply change rand( to mt_rand( if you want to use the other random number generator. This could be especially suitable for gaining speed. mt_rand() is faster than rand() and for bigger scripts this could be more useful. Thanks for reminding me ill change the script in my post.

  12. #12
    SitePoint Evangelist superuser2's Avatar
    Join Date
    Aug 2006
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To get it out of a file.txt where each phrase is separated by a newline (\n) do this:

    PHP Code:
    <?php
    $text
    file_get_contents("text.txt");

    $arr explode("\n"$text);

    $max count($arr);

    $i 0;
    while(
    $i 3)
    {
         
    $i++;
         echo 
    $aText[rand(0$arr)];
    ?>
    I haven't tested it but it should get and echo 3 random lines of text from text.txt.

    BTW: This thread is more than 19 months old according to the little red message. Do you think the OP is still interested in the solution anymore?

  13. #13
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by superuser2 View Post
    BTW: This thread is more than 19 months old according to the little red message. Do you think the OP is still interested in the solution anymore?
    No, but I think it might be useful for others.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •