7

Hi,

I'm trying to preventing consecutive duplicate pages.

For example when I refreshed a page it show a word from each page and when I refresh again it should show a different word from a different page not staying on one page.

This is my pages:

Page1 = Mozart
Page2 = Bach
Page3 = Choplin
Page4 = Brahms
Page5 = Beethoven
Page6 = Schumann

I'm using these functions: rand(), shuffle(), sort(), array(), for my code.

This is my whole code so far:

<?php
$num=rand(1,6);
shuffle($num);
for ($m=0; $m<6; $m++) {     
$result[$m]=$num[$m];     
sort($result); 
$m = array("page1.php" => "page1.php", "page2.php" => "page2.php", "page3.php" => "page3.php", "page4.php" => "page4.php", "page5.php" => "page5.php", "page6.php" => "page6.php");
echo $num;
}
?>

The only thing it echo out is the array number.

Also this error: shuffle() expects parameter 1 to be array

Any Suggestions and explanation will help. I appreciate it. Thanks!

Votes + Comments
To Rectify what some retard did to LastMitch
2
Contributors
3
Replies
6
Views
4 Years
Discussion Span
Last Post by LastMitch
1

Loads of ways to do this. Although I don't quite understand what you're trying to do.

Keeping an array of pages in a session will probably be the easiest way. So on page load, check to see if an array exists, if not create a randomized itemed array. Save to session array var. array_shift the array - this will give you the item to display and shorten the array for the next page load.

So:

session_start();

if(!isset($_SESSION['randArray'])){
    $sourceArray = array("item1","item2","item3","item4","item5","item6");
    shuffle($sourceArray);
    $_SESSION['randArray'] = $sourceArray;
    /*
    echo "NEW ARRAY : <pre>";
    print_r($sourceArray);
    echo "</pre>";
    */
}
$item = array_shift($_SESSION['randArray']);
if(!count($_SESSION['randArray']))unset($_SESSION['randArray']);

echo "CURRENT ITEM: " . $item . "<br />";

/*
echo "REMAINING UNUSED : <pre>";
if(isset($_SESSION['randArray'])){ 
    print_r($_SESSION['randArray']);
}else{
    echo "array unset - ready to start again";  
}
echo "</pre>";
*/

It has the advantage of not having to know the number of items beforehand. Your code would need to be altered every time you want to change the number of pages.
//EDIT
However as I mentioned there are loads of ways of doing this.

Edited by diafol

Votes + Comments
Thanks for the explanation & example !
7

@diafol

Thanks for the reply and explanation and example. I will test it out.

Loads of ways to do this. Although I don't quite understand what you're trying to do.

I know sometime I do post things that are off the wall. But I appreciate for your understanding.

Edited by LastMitch: grammer

Votes + Comments
To Rectify what some retard did to LastMitch
9

@diafol

It work! You are right about other options I might have to google it more.

I made 2 minor adjustments

As you can see:

<?php
session_start();
if(!isset($_SESSION['randArray'])){
    $sourceArray = array("page1.php","page2.php","page3.php","page4.php","page5.php","page6.php");
    shuffle($sourceArray);
    $_SESSION['randArray'] = $sourceArray;
}
$item = array_shift($_SESSION['randArray']);
if(!count($_SESSION['randArray']))unset($_SESSION['randArray']);
include $item
?>

It works fine, I never thought about using array_shift() at all. That's something new.

I appreciate you taking your time to ask my question and resolving this issue.

Thanks!

Votes + Comments
here you buddy, hang in there!
My 2cents worth
To Rectify what some retard did to LastMitch
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.