a script that picks a random file...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnny5
    Confirmed User
    • Jun 2003
    • 291

    #1

    a script that picks a random file...

    anyone know where i can get a script that will pick a random html file from a folder on my server once a day for a "story of the day"?
    any help is greatly appreciated, thanks!
    sincerely,
    me
  • cezam
    Confirmed User
    • Jun 2003
    • 1363

    #2
    index those files into a sql table, and use query like this:

    SELECT * FROM table_name ORDER BY RAND() LIMIT 0,1

    it's really easy in php

    Comment

    • johnny5
      Confirmed User
      • Jun 2003
      • 291

      #3
      so if i index the files... the only thing i put in a php file is
      SELECT * FROM table_name ORDER BY RAND() LIMIT 0,1
      and when i call that file it will return a random file and open it in a browser?
      sincerely,
      me

      Comment

      • cezam
        Confirmed User
        • Jun 2003
        • 1363

        #4
        Originally posted by johnny5
        so if i index the files... the only thing i put in a php file is
        SELECT * FROM table_name ORDER BY RAND() LIMIT 0,1
        and when i call that file it will return a random file and open it in a browser?
        no.. take a look at mysql_connect() and mysql_query() commands and learn how to use them. it will look like this:

        this is the sql table:

        <PRE>
        id | name
        ---------------
        1 | story1.html
        2 | story2.html
        ...
        </PRE>

        this is a sample php file:

        <PRE>
        &lt;?
        $db = mysql_connect("localhost", "user", "password");
        $res = mysql_query("SELECT name FROM table ORDER BY RAND() LIMIT 0,1");
        $row = mysql_fetch_array($res, MYSQL_ASSOC));
        mysql_free_result($res);
        mysql_close($db);
        ?&gt;

        this is a link to a &lt;a href="&lt;?=$row["name"]?&gt;">random story&lt;/a&gt;
        </PRE>

        check out the php manual at www.php.net/manual

        Comment

        • jeffs
          Confirmed User
          • Oct 2002
          • 135

          #5
          Code:
          function randompage($folder){
          	$or = opendir($folder);
          	$htmls = array();
          	while($file = readdir($or)){
          		if(ereg(".+\.html$") || ereg(".+\.htm$")) $htmls[] = $file;
          	}
          	$rint = mt_rand(0, count($htmls)-1);
          	return $htmls[$rint];
          }
          //replace "/storieS" with the path to your stories dirctory
          echo randompage("/stories") ;
          sorry if there are errors, i didnt test it. you'll need to do the HTML yourself; the board is crapping it up.
          Last edited by jeffs; 07-28-2003, 10:35 AM.

          Comment

          • SpaceAce
            Confirmed User
            • Jul 2002
            • 6493

            #6
            Originally posted by johnny5
            anyone know where i can get a script that will pick a random html file from a folder on my server once a day for a "story of the day"?
            any help is greatly appreciated, thanks!
            What should it do with the file? Copy it somehwere? Insert it's name somewhere? What you want is really simple and doesn't require an SQL database as long as you don't have 20,000 stories in the directory you want the script to pick from.

            SpaceAce

            Comment

            • johnny5
              Confirmed User
              • Jun 2003
              • 291

              #7
              Originally posted by SpaceAce


              What should it do with the file? Copy it somehwere? Insert it's name somewhere? What you want is really simple and doesn't require an SQL database as long as you don't have 20,000 stories in the directory you want the script to pick from.

              SpaceAce
              i just want it to pick a random html file and display it for the surfer to read...
              sincerely,
              me

              Comment

              • johnny5
                Confirmed User
                • Jun 2003
                • 291

                #8
                Originally posted by jeffs
                Code:
                function randompage($folder){
                	$or = opendir($folder);
                	$htmls = array();
                	while($file = readdir($or)){
                		if(ereg(".+\.html$") || ereg(".+\.htm$")) $htmls[] = $file;
                	}
                	$rint = mt_rand(0, count($htmls)-1);
                	return $htmls[$rint];
                }
                //replace "/storieS" with the path to your stories dirctory
                echo randompage("/stories") ;
                sorry if there are errors, i didnt test it. you'll need to do the HTML yourself; the board is crapping it up.
                imma give this a shot, thanks man looks like it will work.. thanks to all of you :]
                sincerely,
                me

                Comment

                • Commrade_ICQ
                  Registered User
                  • Jul 2003
                  • 25

                  #9
                  I wantski programski like this.

                  I wantski to run 'ICQski Luck DipSki'

                  Person send me cash via carrier pigeon forski number and program pick themski random one.

                  Canski this be doneski?
                  I sale ICQ number on the seashore....

                  Comment

                  • johnny5
                    Confirmed User
                    • Jun 2003
                    • 291

                    #10
                    didnt work...lol
                    sincerely,
                    me

                    Comment

                    Working...