0

I have a function to generate 13 random integers between a certain range with no duplicates.

I'm not sure on the next part is correct, but I have a button that on $_POST will send to my php script which generates 1 image from the database at a time. I need it to send the random numbers from my generate_numbers function, all 13 of them., because in the end I want to display 13 different images with 1 click of the button. How do I approach this problem? Do i need a queue?

**Button to submit to PHP form named display.php**
<input type="submit" name"display" value=generate_numbers()>

  var generate_numbers = function()
     {

        var i, j;
        var array=[];
        for(i=0; i < 13; i++)
        {
            array[i] = Math.floor(Math.random()*14)+16);
                for(j=0;j<i;j++)
                {
                    while(array[i]==array[j])
                    {
                        array[i]= Math.floor(Math.random()*14)+16);
                    }

                }

        return array[i]; 
     }

2 Answers 2

1

The page with the form

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">

        var rndNums = new Array(16,17,18,19,20,21,22,23,24,25,26,27,28);
        var generate_numbers = function()
        {
            for(var i=0; i < 13; i++)
            {
                var idx = Math.floor(Math.random()*rndNums.length);
                var rndNum = rndNums[idx];
                rndNums[idx] = rndNums[rndNums.length - 1];
                rndNums.pop();
                document.getElementsByName("nums[]").item(i).value=rndNum;
            }
        }

    </script>
</head>
<body>
    <form id="numberForm" action="display.php" method="POST">
        <input type="hidden" name="nums[]">
        <input type="hidden" name="nums[]">
        <input type="hidden" name="nums[]">
        <input type="hidden" name="nums[]">
        <input type="hidden" name="nums[]">
        <input type="hidden" name="nums[]">
        <input type="hidden" name="nums[]">
        <input type="hidden" name="nums[]">
        <input type="hidden" name="nums[]">
        <input type="hidden" name="nums[]">
        <input type="hidden" name="nums[]">
        <input type="hidden" name="nums[]">
        <input type="hidden" name="nums[]">
    </form>
    <button onclick="generate_numbers();document.getElementById('numberForm').submit()">submit</button>
</body>
</html>

And on the php side the "display.php":

<?php
    $nums = $_POST['nums']; // array
    foreach($nums as $key => $val)
        echo 'nums['.$key.']='.$val."<br>\n";
    // ...
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the excellent code i'll look over it some more but for now i'm getting two errors Notice: Undefined index: nums Warning: Invalid argument supplied for foreach()
0

You can use a php foreach loop to loop through each of the numbers. Something like this in your PHP file:

$images = array();

foreach ($POST['display'] as $num) {
  $img_from_number = get_image_from_number($num);
  $images[] = $img_from_number;
}

// code to display images on page
// or for JSON return $images

function get_image_from_number($num)
  // code here
}

If you're doing an AJAX form, you can just loop thru the returned array to create image elements on the page.

1 Comment

the PHP that generates the image is dealing with binary data, can I still use this loop inside?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.