/    Sign up×
Community /Pin to ProfileBookmark

Controlling shuffle in arrays…

Hello,

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

[code=php]
$arr = array($first,$second,$third,$fourth,$fifth);
$rarr = shuffle($arr);
foreach ($arr as $number) {
include($number);
echo “<br />”;
}[/code]

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

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@TJ111Oct 11.2007 — You could just send the $arr with the form post or something similar.

[code=php]
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 />";
}
[/code]
Copy linkTweet thisAlerts:
@tubaplaya76authorOct 11.2007 — It's still shuffling. Do I need to add something else to the form itself?
Copy linkTweet thisAlerts:
@TJ111Oct 11.2007 — 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".
Copy linkTweet thisAlerts:
@tubaplaya76authorOct 13.2007 — 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
Copy linkTweet thisAlerts:
@scragarOct 13.2007 — if your using sessions store the array to a session.
[code=php]if(isset($_SESSION['arr'])){
$arr = $_SESSION['arr'];
}else{
$arr = array($first,$second,$third,$fourth,$fifth);
$rarr = shuffle($arr);
$_SESSION['arr'] = $arr;
};[/code]
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.
Copy linkTweet thisAlerts:
@NightShift58Oct 17.2007 — Use a placebo...
[code=php]<?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 />";
}
?>[/code]

Later, replace "##[DUMMY]##" with the POSTed segment.
Copy linkTweet thisAlerts:
@NogDogOct 17.2007 — Another option would be to use srand() to control the shuffle results, storing a seed value in your session data:
[code=php]
<?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
}
?>
[/code]
×

Success!

Help @tubaplaya76 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.3,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...