Click to See Complete Forum and Search --> : Controlling shuffle in arrays...


tubaplaya76
10-11-2007, 09:53 AM
Hello,

I have currently [and successfully] shuffled five different segments of a page to include:


$arr = array($first,$second,$third,$fourth,$fifth);
$rarr = shuffle($arr);
foreach ($arr as $number) {
include($number);
echo "<br />";
}

However, I also have a form which submits data to the same page. My problem is, once the different segments and successfully shuffled, and I submit the form, the page re-shuffles. Is there a way I can control this shuffle operation so that once I "submit" the data back to my page, the five different segments don't reshuffle again?

thanks in advance, and Regards,
Sam

TJ111
10-11-2007, 10:07 AM
You could just send the $arr with the form post or something similar.


if (!empty($_POST['arr'])) {
$arr = $_POST['arr'];
} else {
$arr = array($first,$second,$third,$fourth,$fifth);
$rarr = shuffle($arr);
}
foreach ($arr as $number) {
include($number);
echo "<br />";
}

tubaplaya76
10-11-2007, 10:26 AM
It's still shuffling. Do I need to add something else to the form itself?

TJ111
10-11-2007, 10:36 AM
Yeah put a hidden input in the form itself names "arr" containing the array and then convert from that array back into $arr however you like, mine was just an example. Make sure to change $_POST to $_GET if your form method="get".

tubaplaya76
10-13-2007, 08:46 AM
thansk for the response,

I've tried several different variances for what my value needs to be in the hidden input, but only find myself reshuffling whether or not the form is submitted or not. Any other suggestions,

Regards,
Sam

scragar
10-13-2007, 10:21 AM
if your using sessions store the array to a session.
if(isset($_SESSION['arr'])){
$arr = $_SESSION['arr'];
}else{
$arr = array($first,$second,$third,$fourth,$fifth);
$rarr = shuffle($arr);
$_SESSION['arr'] = $arr;
};I'm not sure which your using though, rarr or arr, I would think rarr, but it looks as likly to be arr given the previous response.

NightShift58
10-16-2007, 08:12 PM
Use a placebo...
<?php
$filler = "##[DUMMY]##";
$arr = array($first,$second,$third,$fourth,$fifth,$filler);
shuffle($arr);
foreach ($arr as $number) {
if ($number <> $filler) {
include($number);
} else {
echo $filler;
}
echo "<br />";
}
?>
Later, replace "##[DUMMY]##" with the POSTed segment.

NogDog
10-16-2007, 08:35 PM
Another option would be to use srand() to control the shuffle results, storing a seed value in your session data:

<?php
session_start();
// generate random seed if not already in session data:
if(!isset($_SESSION['seed']))
{
$_SESSION['seed'] = rand();
}
// seed the random number generator
srand($_SESSION['seed']);

$myArray = range(1,9);
shuffle($myArray); // result will be controlled by the srand()
foreach($myArray as $value)
{
echo $value . ' '; // same sequence of numbers while session exists
}
?>