samedi 26 décembre 2015

php/javascript random number

Hello i am playing around with javascript and trying to choose some random images.

What i'm actually trying to do i have $totalsixes = rand(1,6); in php and lets say that chose 4 then in javascript i want to show 4 images with the number 6 and 2 others whit random numbers from 1-5

here's what I've tried so far:

<?php
$totalsixes = rand(1,6);
?>
<script type="text/javascript">
function shuffle(o){ 
    for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] =     o[j], o[j] = x);
    return o;
};

var imagePaths = shuffle([
    "http://ift.tt/1Tlc09v",
    "http://ift.tt/1J9tyEZ",
    "http://ift.tt/1Tlc09x",
    "http://ift.tt/1J9tyF1",
    "http://ift.tt/1Tlc0pN",
    "http://ift.tt/1J9tyF3"]);

for(var i = 0; i < imagePaths.length; i++) {
    document.getElementById("randterning" + i).src = imagePaths[i];
}
</script>

as you may see above the $totalsixes have no meaning at all in the code yet, as i don't know how to tell the javascript to show X of sixes ($totalsixes chose the X), and also i don't know how to make the javascript chose a random number for those others dices. total there is always 6 dices.

hope it was easy for you to understand what i meant and hopefully you can help me.

Thanks and sorry for my English.




Aucun commentaire:

Enregistrer un commentaire