New and looking for Random button help

11 replies
  • WEB DESIGN
  • |
Hello, when researching a way to make a Random button for one of my web sites, I came across this forum and decided to ask for help.

I would like to make a Random Link button for my web site so when a person clicked the button they are taken to another one of my web page. I have found some old javascript that would do this but since I have a huge web page was looking for something that could pick any html file in a set folder.

Any thoughts?
Thanks
Kimber
#button #random
  • Profile picture of the author webpeon
    I would use a database and a little coding to accomplish this myself,

    store the file url's in a database table

    code the button...

    1) to retrieve the amount of rows in the table
    2) pick a random number from 1 to however many rows you have in the table
    3) select and navigate to the url row randomly chosen above
    Signature
    Web 2 Mobile
    The Future of The Web
    {{ DiscussionBoard.errors[5708320].message }}
    • Profile picture of the author KimberSue
      Thanks for the tip. I looked in my expression web designer and found a DataPager tool I am looking into learning.

      Been doing this since 1999 and I still love my simple html.

      Thanks
      Kimberly
      {{ DiscussionBoard.errors[5709573].message }}
      • Profile picture of the author Brandon Tanner
        If all you need to do is have the button redirect to a random webpage in a specific directory, then you don't need to setup a database for that. Just a few lines of php will do.

        First, in your landing page's HTML code, set the form "action" of your button to "random.php". For example...

        <form action="random.php">
        <input type="submit" value="Random Page" />
        </form>


        Then create a new php file in the same directory named "random.php", and paste the following code in it...

        <?php
        $directory = "DirectoryName";
        $pages = array_diff(scandir($directory), array('..', '.'));
        $random_pages = array_rand($pages, 1);
        $page = $pages[$random_pages];
        header("Location: $page");
        ?>


        Make sure to change the red part above to the actual name of the directory that all of your "random" webpages will be in (the example above assumes that this directory folder is on the same level as the "random.php" file).

        That's it!
        Signature

        {{ DiscussionBoard.errors[5713005].message }}
        • Profile picture of the author KimberSue
          Bless You!! I have done enough php that I get this. Thanks you so much.
          {{ DiscussionBoard.errors[5713218].message }}
        • Profile picture of the author KimberSue
          Ok, I feel like the village idiot here but I am doing something wrong.

          I made a set of five test pages used the codes below with /public_html/free/ in place of DirectoryName and it goes to /free/random.php folder instead of a page. I know you said "the example above assumes that this directory folder is on the same level as the "random.php" file" and I tired use urls, full folder directory and other formats I can think of and nothing is working. Can you please advice? Thanks you very much for your time.

          The forum won't allow me to put a url so I giving it to you broken up testformat. info /free/ 2.html

          <?php
          $directory = "/public_html/free/";
          $pages = array_diff(scandir($directory), array('..', '.'));
          $random_pages = array_rand($pages, 1);
          $page = $pages[$random_pages];
          header("Location: $page");
          ?>
          {{ DiscussionBoard.errors[5713550].message }}
          • Profile picture of the author Brandon Tanner
            random.php should be in the root folder (not in the 'free' folder). For example --> public_html/random.php

            And the page that the button is on should also be in the root folder (ie public_html/buttonpage.html).

            Then assuming your 'free' folder is located at "public_html/free/", your $directory variable would need to look like this...

            $directory = "free";

            I just tested it on my server, so it definitely works.
            Signature

            {{ DiscussionBoard.errors[5714177].message }}
            • Profile picture of the author KimberSue
              Originally Posted by Brandon Tanner View Post

              And the page that the button is on should also be in the root folder (ie public_html/buttonpage.html).
              The button can only be on the pages in the public_html file and not the pages in the free file? Or can I make a page holding the button in the public_html file and use php includes to place it on ever page?

              Thanks
              {{ DiscussionBoard.errors[5714403].message }}
              • Profile picture of the author Brandon Tanner
                Originally Posted by KimberSue View Post

                The button can only be on the pages in the public_html file and not the pages in the free file? Or can I make a page holding the button in the public_html file and use php includes to place it on ever page?

                Thanks
                Sure, you can set it up however you want. I was just giving an example. If you want the button page and the random.php page inside the "free" folder, then do this...

                $directory = "../free/";

                And if you want to use the button on all of the pages in that folder, then simply paste the button code into each of those pages. Of course, if you think you will ever need to edit the button code, then it would be better to use 'includes' on each of the pages that link to the single button page
                Signature

                {{ DiscussionBoard.errors[5714719].message }}
                • Profile picture of the author KimberSue
                  Thanks for all your help. I going to table this for a couple of days. I know I must being making some stupid mistake because what you are telling me I understand but I can't get it to work. I think a fresh look in a couple of days is the best idea.
                  {{ DiscussionBoard.errors[5715060].message }}
                  • Profile picture of the author Brandon Tanner
                    Originally Posted by KimberSue View Post

                    Thanks for all your help. I going to table this for a couple of days. I know I must being making some stupid mistake because what you are telling me I understand but I can't get it to work. I think a fresh look in a couple of days is the best idea.
                    Are you getting a specific error message? If so, PM me the URL and I'll be glad to take a look at it.
                    Signature

                    {{ DiscussionBoard.errors[5715118].message }}
                    • Profile picture of the author KimberSue
                      Hi, Since you have been so much help I thought I take a minute and tell you why it is not working.
                      The problem is expression web “I am Microsoft and know better than you” issue. I have my own search engine using zoom indexer so I will put a search box above each free printable or clipart I make. I am a graphic designer.
                      Expression web gives me this message: “This page contains one or more PHP script blocks which require that you save the page with a PHP file extension. Remove the PHP script blocks from the page or save the page with a PHP file extension.”
                      So the expression web strips out the includes php if I save as html and forces me to save as PHP. It is not worth the hassle to rename the pages I have to php for this type of button.
                      Thanks again and have a nice day. I’ve decided to give up on the random page or next page button.
                      Kimberly
                      {{ DiscussionBoard.errors[5715368].message }}

Trending Topics