Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Question Javascript Random Script Chooser

    Hello, I need help again :3
    Heres what I need: I need some javascript to random a script. Simply like this:
    The script randoms a number
    Based on the number it runs some lines of PHP script.

    Random 1-10
    If 1
    Then run this PHP script
    *PHP script*
    *PHP script*
    *PHP script*
    *PHP script*
    If 2
    Then run this PHP script
    *Different PHP script*
    *Different PHP script*
    *Different PHP script*
    *Different PHP script*
    If 3
    Then run this PHP Script
    *some more different PHP Script*
    *some more different PHP Script*
    *some more different PHP Script*
    *some more different PHP Script*
    If 4 etc etc etc

    Each bit of PHP Will be similar, but be different in some ways. There may be more compressed ways of doing this. But if it at all possible, I would like it in the way I wrote it above. For instance I dont want:

    Random 1-3
    If 1 Run function 1
    If 2 Run function 2
    If 3 Run function 3
    Function 3
    *PHP Script*
    *PHP Script*
    *PHP Script*
    Function 1
    *PHP Script*
    *PHP Script*
    *PHP Script*
    Function 2
    *PHP Script*
    *PHP Script*
    *PHP Script*

    I will thank the best answer! This website is awesome and has answered so many of my questions! Thanks in advance!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    You can't do it.

    JavaScript runs IN THE BROWSER.

    PHP code runs ON THE SERVER.

    100% of *ALL* PHP code is COMPLETELY FINISHED before the page is ever sent to the browser.

    The only way that you could come close to doing this would be to use AJAX. That is, your JavaScript code would have to make a different AJAX request (or maybe multiple requests, given the way you have organized your code) based on what random number was chosen. \If you don't understand what AJAX is and how to use it, then this code is beyond you at this time.

    *******

    But the whole effort seems wrong-headed, to me. If you want random PHP to run, then choose the random number in PHP code, so that it runs before it ever gets to the browser. As PHP code normally would.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by swordstoo View Post
    Hello, I need help again :3
    Heres what I need: I need some javascript to random a script. Simply like this:
    The script randoms a number
    Based on the number it runs some lines of PHP script.
    You can do that easily with AJAX.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    Quote Originally Posted by webdev1958 View Post
    You can do that easily with AJAX.
    Which is what I said. But if he doesn't understand that PHP has to be run server side then I doubt he understands AJAX.

    And I still wonder if he shouldn't just do all this in PHP.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Old Pedant View Post
    Which is what I said.
    Originally you said it can't be done and then you seemed to be hedging your bets saying AJAX might come close.

    Quote Originally Posted by Old Pedant View Post
    You can't do it.
    ....
    ....
    The only way that you could come close to doing this would be to use AJAX.
    I'm just making sure the op is aware that it definitely can be done with AJAX.

  • #6
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I have seen AJAX being used to call on PHP in HTML forms, but I haven't looked into it enough to write it alone.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    Quote Originally Posted by swordstoo View Post
    I have seen AJAX being used to call on PHP in HTML forms, but I haven't looked into it enough to write it alone.
    So my question still stands: Why do you want/need to do this from JavaScript instead of just doing it all in PHP code?

    Normally, the only reason to use AJAX is to respond to something the user has done. Choosing a random chunk of PHP can be done easier all in PHP code than by getting AJAX involved.

    ************

    And to WebDev: Yes, I see your point. Sorry. I should have said, in my original post, "can't be done the way you are coding it." And when I said that using AJAX he could come close, I was thinking of the fact that he'll have to use DOM manipulation to utilize the PHP code, most likely, and can't just drop the PHP in place.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ok. So I have written the code myself, but I'm not understanding the physics properly. Here is my code:
    ----------------------------------
    $input = array("1", "2", "3", "4", "5");
    $rand_keys = array_rand($input, 2);
    echo $input[$rand_keys[0]] . "\n";
    -----------------------------------
    When testing I got the following results:
    4,3,2,2,1,1,3,2,1,4,3,1,3,2,2
    That seems pretty random to me. But there was no 5 ever in my many results.
    Heres my problem: I will be using 15 arrays. And I need the random number to include all 15. If you can, please include the algorithm for this. When I changed the 2 (in *$input 2); to 5, It instead gave me these results:
    1,1,2,2,1,1,2,1,2,1,2,2,1,2,2,1
    Thats random as well, But only included 2 numbers. 1 + 2. I dont get this at all...
    Changing the value of 0 (in $input[$rand_keys[0]]) to any other integer returns an error.
    Thanks for all your help!

  • #9
    New Coder
    Join Date
    Dec 2011
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Oh and I didn't need to write it in partly javascript. It was just a preference. Now that I know it cant be done withought using some special *complicated* codes, I would much rather write it all in PHP. which I am doing, but failing horribly at it I find it easier to write in Javascript.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    Well, you aren't really using array_rand correctly.

    Read the docs:
    http://www.php.net/manual/en/function.array-rand.php

    But if you wanted to get all 5 values randomly, you would just do:
    Code:
    $input = array("1", "2", "3", "4", "5");
    shuffle($input);
    foreach ($input as $value) 
    {
        echo "$value,";
    }
    But in any case this question no longer belongs in the JavaScript forum.

    Please re-ask it in the PHP forum.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    swordstoo (03-18-2012)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •