Question

I need to print an array (more specifically prime numbers) one 'click' at a time. I have created the prime array but I am not sure how to it run like user is clicking a button "next" and the next prime number shows up and then he clicks next and another one shows up. Or maybe there is a better way doing this?

Here is the prime array:

$primes = array();
for ($x = 2; $x <= 1000; $x++) {
    $xIsPrime = TRUE;
    $sqrtX = sqrt($x);
    foreach ($primes as $prime) if ($prime > $sqrtX || ((!($x % $prime)) && (!$xIsPrime = FALSE))) break;
    if ($xIsPrime) echo ($primes[] = $x)  . "<br>";
}
Was it helpful?

Solution

These things are best done with Javascript but, since your question is tagged PHP, I’ve written the following code:

<?php
$primes = array(2,   3,   5,   7,  11,  13,  17,  19,  23,  29, 
               31,  37,  41,  43,  47,  53,  59,  61,  67,  71, 
               73,  79,  83,  89,  97, 101, 103, 107, 109, 113, 
              127, 131, 137, 139, 149, 151, 157, 163, 167, 173);
// Add as many primes as you wish to the array
$num = array_key_exists("hid", $_POST) ? $_POST["hid"]+1 : 0;
?>
<html>
<head>
<!-- Put your meta tags, title, Javascript and CSS here -->
</head>
<body>
<form method="POST">
<h2><?php print($primes[$num]); ?></h2>
<?php
if(sizeof($primes)-$num > 1){
?>
<input type="hidden" name="hid" value="<?php print($num); ?>"/>
<input type="submit" name="sub" value="Next Prime"/>
<?php
}
else{
?>
<input type="submit" name="sub" value="Next Prime" disabled/>
<?php
}
?>
</form>
</body>
</html>

OTHER TIPS

You can use querystring to determine up to what number have the prime shown and find the next prime number based on that.

$begin = isset($_GET['num']) ? $_GET['num'] : 2;
for ($x = $begin; $x <= 1000; $x++) {
    ...
    if ($xIsPrime) {
        echo $x  . "<br>";
        echo "<a href='...?num=$x'>next</a>";
        break;
    }
}

You could use Ajax or you can do a CRUD system using PHP (which would require a page refresh each time)

Psuedo code:

Say your script is at /primes.php (and this is the page you are on)

First - because your list is static - run you script and output all the prime numbers to an array - there is no point generating them each time.

On you page the user sees you would have a form with a hidden field containing the last shown prime number (lets say 3 for ease).

You would then post this to your script using the action='primes.php'

Get the posted data - then simply find the position in the array of the last known prime number (3 in this case - which will return position 1 (remember arrays are 0 indexed!)

Then just query the array for the next prime number (so with you returned 1 (lets say held in $lastPrime and with an array called $primes just ask for `$primes($lastPrime +1) - this will give you the next prime which you can add into the hidden field again.)

hopefully that will help you work it out.

If you were using Ajax - do exactly the same but just update the hidden field OR you can use a local javascript variable to store the current / last value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top