• Resolved peowdie

    (@peowdie)


    I’m looking for a way to take a random line from either a txt file or mySQL database (or third option?) and display on a website. Instead of getting a new line on every page refresh, I would like it to refresh on it’s own at the click of a button.

    Here’s an example of my idea, in case that’s a help for anyone.

    Imagine a question like “How should I travel?”, and a button that says “Give me an idea”, then at every click you get a fine random line – “Car”, “Boat”, “Plane”, “Flying Bowl of Ceral” and so on.

    I would prefer any way that’s using the least resources of the server, and if possible a way to keep people from getting access to all the lines.

    I hope someone can help me. 🙂

    Thanks,
    Jonas.

Viewing 7 replies - 1 through 7 (of 7 total)
  • To do that you’ll need JavaScript and AJAX. There’s a lot of info here:

    http://codex.wordpress.org/AJAX_in_Plugins#Ajax_on_the_Viewer-Facing_Side

    Dion

    (@diondesigns)

    If server performance is important, you do not want to use AJAX through WordPress. Each click will load a full version of WordPress, and it’s very likely your server resource usage will skyrocket.

    How much text are we talking about here? If it’s less than a couple thousand bytes, I’d format the entries into a JSON-encoded string and add it to the page as a javascript variable. You could then use JSON.parse() on the string to create a structure that can be used when a user clicks a button. This requires no AJAX whatsoever, and your users would need to look at the page source to see a (hard-to-read) list of entries.

    Thread Starter peowdie

    (@peowdie)

    How much text are we talking about here?

    At least 120 separate lines of 1 to 3 small words, more to be added later on.

    I wouldn’t mind having it as a variable encoded. It’s simply to avoid people cheating and seeing the entire list by seeing source code.

    You dont need the WordPress core to use AJAX. 🙂 At leaat on the PHP side.

    For something that’s as simple as your need, this file woudl be more than enough:

    <?php
    $phrases = array (
      'Bus',
      'Plane',
      'Car',
      'Flying Spaghetti Monster'
    );
    
    $index = mt_rand (0, count ($phrases) - 1);
    
    echo $phrases [$index];
    die ();

    You can add as many entires into the $phrases array as you need and it will still work. The only reason for using the WordPress back-end for this would be if you need any more functions or authentication (there’s no security issues with this).

    But, if you want the least overhead, do what DionDesigns suggested and have them all in the page. Only someone that really wants to find out will go looking for them, and if your example is what you’re looking for I can’t see how someone knowing this sort of list would be an issue.

    Dion

    (@diondesigns)

    Another option would be to store the strings as a file on the filesystem, and then use AJAX to load the file when required. The entries would no longer be in the page source, this type of AJAX doen’t require a PHP file (which means it would have very little impact on server performance), and it gives you more flexibility with adding/deleting entries since they would be external to your WordPress installation.

    If I were doing this for myself or a client, I would write a plugin that controls the addition/deletion of entries in the file. If you don’t know how to write plugins, then the best solution would be to store the entries in a text file in a pre-defined format, then use javascript to parse the text file into the structure you need.

    Thread Starter peowdie

    (@peowdie)

    Thanks for the replies. The current setup right now is some random plugin in a frame where I refresh the frames content onclick. I’ll test out the various ways tomorrow where I have time to. Again, thanks for taking time to help me out 🙂

    Thread Starter peowdie

    (@peowdie)

    Right. Being really new and all with this php and coding in general, I’m still “cheating” a bit with plugins.

    catacaustic, I went with your solution, that array one, and stuffed it all into a shortcode. Posting php directly on a page didn’t seem to please WordPress.

    Thanks alot for the help, both of you. 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to: Pull random string to wordpress, refreshed by button click?’ is closed to new replies.