...

View Full Version : Javascript Random Script Chooser



swordstoo
03-18-2012, 01:40 AM
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!

Old Pedant
03-18-2012, 02:14 AM
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.

webdev1958
03-18-2012, 02:43 AM
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.

Old Pedant
03-18-2012, 03:26 AM
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.

webdev1958
03-18-2012, 03:34 AM
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.


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.

swordstoo
03-18-2012, 03:34 AM
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.

Old Pedant
03-18-2012, 03:48 AM
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.

swordstoo
03-18-2012, 05:48 AM
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!

swordstoo
03-18-2012, 05:50 AM
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.

Old Pedant
03-18-2012, 07:12 AM
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:


$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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum