Nektar

Make getRandom() give different results each time it is called

Recommended Posts

Hey,

I would like to give me your help. In a project i am working on i am trying to build a slot machine.

I am stuck at the point where everytime i click the button, so the reels will spin, i want it to give me a number of random children of a parent.

Therefore i used getRandom(3), which is called with an onclick event, and does what it does in <script> tags in the php file. The problem is that it produces

3 random pages only one time. I want it everytime i click the button to find me new random children.

 

So this is the code where is tries to Get the Random children:

<script>

    function fillfirst(){

        <?php 
        $i=0;
        foreach  ($get_md->children->getRandom(3) as $md)   // here i take 3 random children of parent page
        {
            if($i==0){$a=$md->image->url;}
            elseif($i==1){$b=$md->image->url;}                // here i put each child's image a 
            else{$c=$md->image->url;}
            $i++;
        }
        ?>
        
        newreels = ['<?php echo $a;?>','<?php echo $b;?>','<?php echo $c;?>'];     // here is the reel with the url of the images i want to have
                                                                                   // but only at one specific click. Every new click i want the getRandom()
      									// to give me new random children.
        document.getElementById("freel").innerHTML= '<div><img src="' + newreels.join('"></img><img src="') + '"></img></div><div><img src="' + newreels.join('"></img><img src="') + '"></img></div>'
            
        return newreels;
    }

</script>

 

And here is the code where it is called from (inside the html):

 <button class="w3-button w3-hover-yellow" onclick="fillfirst();">Main</button>

 

If anyone has an idea it would be really helpful.

Share this post


Link to post
Share on other sites

Because your click action is client side (javascript) and the getRandom is server side (php), you will either need to get all the options initially and do the random filter using javascript, or you would need to do an ajax call on each click and return another set using getRandom.

Does that make sense?

  • Like 2

Share this post


Link to post
Share on other sites

Yes i thought that would be the problem :/ ..

I can't see how i will be able to do it with javascript because i need to call getRandom (as far as i know on how to work on stuff like that, and cause of that i will need php) but i find the ajax solution more logical maybe. Can you provide some code of a url which can help me? Because i am still in the learning process.

Thanks a lot though!

Share this post


Link to post
Share on other sites

Here's a JS demo: https://jsfiddle.net/abdennour/oh3jL82j/

Not knowing your exact scenario, but I think a purely JS solution would be better unless you have a LOT of children that you are doing the getRandom on.

If you really want to go the AJAX route we can help you further, but it's really a PHP issue, rather than PW so googling around would be pretty helpful. The only catch with PW and AJAX is that the URL for the ajax request can't be directly to a php file in /site/templates - the file either needs to be outside /site or you need to assign the file to a PW template and page call the url for that page.

Sorry for the short answer - I am a little rushed at the moment.

  • Like 2

Share this post


Link to post
Share on other sites

No problem i understand. In my case there will be indeed a lot of children. So i will look for the ajax solution.

Thanks a lot for your help! I'll work on it and i will see what i can do!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.