1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP/mysql help need

Discussion in 'PHP & Perl' started by kkvsam, Dec 7, 2011.

  1. kkvsam

    kkvsam Senior Member

    Joined:
    Oct 11, 2009
    Messages:
    936
    Likes Received:
    569
    Occupation:
    SYS ADMIN
    Home Page:
    I just need to rotate the content for each user visit. need only 10-15 lines. Does any one know how to do this. I can get the content form excel or txt file.
     
  2. bastienvans

    bastienvans Newbie

    Joined:
    May 3, 2009
    Messages:
    20
    Likes Received:
    2
    need more info.
     
  3. nithinreddy

    nithinreddy Junior Member

    Joined:
    Jan 12, 2011
    Messages:
    112
    Likes Received:
    10
    Occupation:
    Android Developer
    Rotate? Are you referring to a marquee, or a random selection from the text file?
     
  4. zelma143

    zelma143 Power Member

    Joined:
    Jun 25, 2010
    Messages:
    571
    Likes Received:
    37
    Occupation:
    PHP programmer,Bot maker,iMacro script maker
    how do you want to rotate?

    is there line read randomly from text file?
     
  5. xpwizard

    xpwizard Junior Member

    Joined:
    Nov 6, 2010
    Messages:
    198
    Likes Received:
    122
    The below will random from a text file using a new line as the delimiter.
    It will make it so there are no duplicates, and will output the results with an html line break.

    PHP:
    <?php
    // input the filename
    $filename "random.txt";
    // # of lines
    $number_lines 10;

    /***** Do not edit below this line *****/

    // read file and explode via new line
    $file getcwd()."/$filename";
    $fp fopen($file'rb');
    $contents fread($fpfilesize($file));
    fclose($fp);
    $cont_explode preg_split('/\r\n|\r|\n/'$contents);

    // if array <= # of lines, then just echo
    if (count($cont_explode)-<= $number_lines) {
        
    $result $cont_explode;
    } else {
        
    $result = array();
        
    // if array > # of lines, random
        
    for($a=1$a<=$number_lines$a++){
            
    // if less than 2, change rand
            
    if (count($cont_explode) <= 2) {
                
    $rand mt_rand(0count($cont_explode)-1);
            
    // else keep rand the same
            
    } else {
                
    $rand mt_rand(0count($cont_explode)-2);
            }
            
    $result[] = $cont_explode[$rand];
            unset(
    $cont_explode[$rand]);
            
    $cont_explode array_merge($cont_explode);
        }
    }

    // echo the results
    $body"";
    foreach (
    $result as $val) {
        
    $body .= $val."<br />";
    }
    $body substr($body,0,-6);
    echo 
    $body;

    ?>
    The result will look like the following:

    Code:
    Harley<br />Carl<br />Pheona<br />Richard<br />Test<br />Parry<br />Amelia<br />Hamish<br />katy<br />Kane
    
    OR
    
    Harley
    Carl
    Pheona
    Richard
    Test
    Parry
    Amelia
    Hamish
    katy
    Kane
     
    Last edited: Dec 7, 2011
  6. gimme4free

    gimme4free Executive VIP Jr. VIP Premium Member

    Joined:
    Oct 22, 2008
    Messages:
    1,884
    Likes Received:
    1,932
    This version is a bit more lightweight:
    PHP:
    <?php
    $file 
    "file.txt";
    $ah fopen($file'r');
    $contents fread($ahfilesize($file));
    $lines explode("\n",$contents);
    echo 
    $lines[array_rand($lines)]."<br />";
    ?>
     
  7. zelma143

    zelma143 Power Member

    Joined:
    Jun 25, 2010
    Messages:
    571
    Likes Received:
    37
    Occupation:
    PHP programmer,Bot maker,iMacro script maker
    more short lines :)
    PHP:
    $file file("file.txt");
    $count count($file);
    echo 
    $file[rand(0,$count-1)];
     
  8. EdgarFoX

    EdgarFoX Newbie

    Joined:
    Nov 18, 2011
    Messages:
    16
    Likes Received:
    2
    more short lines:
    Code:
    $file = file("file.txt");
    echo $file[rand(0,count($file)-1)];  
    
     
  9. Frement

    Frement Registered Member

    Joined:
    Sep 26, 2010
    Messages:
    66
    Likes Received:
    22
    Location:
    46696E6C616E64
    Home Page:
    Now heres something that will rotate your content, if you have multiple files, it doesn't matter what they are named, it will choose one randomly. You need to change $dir variable to your directory.

    PHP:
    <?php

    $dir 
    "myfiles/content/";

    $files = array();
    $i 0;

    if (
    is_dir($dir)) {
        if (
    $dh opendir($dir)) {
            while ((
    $file readdir($dh)) !== false) {
                if (
    $file != ".." && $file != "." && substr($file01) != ".") {
                    
    $files[$i] = $file;
                    
    $i++;
                }
            }
            
    closedir($dh);
        }
    }

    $rand rand(0count($files) - 1);

    $file fopen($files[$rand], "r");
    $cont fread($filefilesize($dir.$files[$rand]));
    fclose($file);

    echo 
    $cont;

    ?>
     
  10. menzow

    menzow Junior Member

    Joined:
    Apr 20, 2010
    Messages:
    141
    Likes Received:
    101
    @All the above, if you read the title:
    "PHP/mysql help need"

    The fact that he also needs mysql helps, to me implies that the data is stored in a database, not in a text file.

    Code:
    <?php
    $db_host = "localhost";
    $db_user = "root";
    $db_pass = "pass";
    $db_name = "databasename";
    
    mysql_connect($db_host,$db_user,$db_pass) or die(mysql_error());
        mysql_select_db($db_name) or die(mysql_error());
    $content = mysql_query("SELECT `id` FROM content") or die(mysql_error());;
    $count = mysql_num_rows($content);
    if($count != 0){
        $post = (int)rand(0,$count);
        $content = mysql_query("SELECT * FROM content WHERE `id`='$post'");
        print_r(mysql_fetch_array($content));
    }else{
        echo("There is no content to display.");
    }
    mysql_close();
    ?>
    Dont know if it works, but it should.
    Wrote this in 3 minutes exact, so havent tested it.

    Better version:
    Code:
    <?php
    $db_host = "localhost";
    $db_user = "databaseuser";
    $db_pass = "databasepassword";
    $db_name = "databasename";
    
    mysql_connect($db_host,$db_user,$db_pass) or die(mysql_error());
        mysql_select_db($db_name) or die(mysql_error());
    $content = mysql_query("SELECT * FROM `content` ORDER BY RAND() LIMIT 0,1;") or die(mysql_error());;
    $count = mysql_num_rows($content);
    if($count != 0){
        print_r(mysql_fetch_array($content));
    }else{
        echo("There is no content to display.");
    }
    mysql_close();
    ?>
     
    Last edited: Dec 9, 2011
  11. Frement

    Frement Registered Member

    Joined:
    Sep 26, 2010
    Messages:
    66
    Likes Received:
    22
    Location:
    46696E6C616E64
    Home Page:
    Seems that you didn't read the post that good, he clearly says:
    "I can get the content form excel or txt file."