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

article a day random text

Discussion in 'PHP & Perl' started by Xilb51X, Nov 22, 2010.

  1. Xilb51X

    Xilb51X Junior Member

    Joined:
    Mar 25, 2009
    Messages:
    154
    Likes Received:
    38
    Occupation:
    S.S.S
    Location:
    the greastest country on earth
    i have a site and it has 5 pages (adsense) anyways is there a way i can take the main content and use php to pull random articles that are page specific and appear every time a visitor loads the page?
    so say 1 page is apples, i need the script to pull all articles about apples and another page is oranges i need the code to pull a article about oranges but i would like to just have one php code on each page and then have it pull a the articles from a folder that i make instead of editing the page every time? hopefully this makes sense. thanks for the time. im also using Dreamweaver.
     
  2. altschule

    altschule Regular Member

    Joined:
    Sep 1, 2010
    Messages:
    282
    Likes Received:
    185
    Location:
    Sector 9
    Sure. put the ready-made articles in a directory for each page, in the php just grab a list of all of the file names, put into an array, select a random one and voila. I'm not going to write it for you, but here's what im talking about in a sense:

    Code:
    <?
    $myDir = opendir("./oranges/");
    
    while($file = readdir($myDir)) {
       $fileList[] = $file;
    }
    
    closedir($myDir);
    
    $articleCount = count($fileList);
    
    $randNumber = (rand()%$articleCount);
    $randArticle = $fileList[$randNumber];
    
    // now open the article and display it
    
    $fh = file_get_contents($randArticle);
    
    echo $fh;
    ?>
    
    Or at least that's what I thought you were looking for.