Randomize pages order

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
krispol
Forum Newbie
Posts: 5
Joined: Sun Jul 01, 2012 11:10 am

Randomize pages order

Post by krispol »

Hey

Here's my php code:

Code: Select all

<?php  
error_reporting (E_ALL | E_STRICT);

$files = array();

$path = "./questions/";

if( $handle = opendir( $path ) ) {
        while (false !== ($file = readdir($handle))) {
                if( $file != "." && $file != ".." && !is_dir( $path.$file ) ) {
                                   $files[] = $path.$file;
                                }
                }
        // You need to populate the $files variable before you count it;)
        $file = $files[ rand( 0, count( $files )-1 ) ];
        // Now we can include it!
        include ("$file");
}

else {
        print "no files found";
}       
?>
It does what I need: randomly calls for html pages from the questions folder.

What I would need help with is:
1. How to prevent pages from repeating?
2. How to stop and redirect to some other page after all the html pages from the questions folder have been displayed?

I would appretiate any help!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Randomize pages order

Post by social_experiment »

krispol wrote:1. How to prevent pages from repeating?
Write all chosen values to an array; before displaying a page check if the next value is inside the array (in_array() can be helpful here) and continue accordingly
krispol wrote:2. How to stop and redirect to some other page after all the html pages from the questions folder have been displayed?
Count the amount of matches and compare that value to the amount of files; if those two are equal all files have been displayed and you can move on to the next part of the script
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
krispol
Forum Newbie
Posts: 5
Joined: Sun Jul 01, 2012 11:10 am

Re: Randomize pages order

Post by krispol »

social_experiment wrote:
krispol wrote:1. How to prevent pages from repeating?
Write all chosen values to an array; before displaying a page check if the next value is inside the array (in_array() can be helpful here) and continue accordingly
krispol wrote:2. How to stop and redirect to some other page after all the html pages from the questions folder have been displayed?
Count the amount of matches and compare that value to the amount of files; if those two are equal all files have been displayed and you can move on to the next part of the script
Thank you for your reply! I kind of get what you are saying but I would appretiate if you could help me with the code, cause Im really not good at it :(
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Randomize pages order

Post by requinix »

I'd say
krispol wrote:1. How to prevent pages from repeating?
Get a list of all the questions and shuffle it. If you only want a few of them then grab a portion of the array.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Randomize pages order

Post by califdon »

What we're trying to tell you is that we are here to help people learn to use PHP, not to write PHP scripts for you (this wouldn't happen to be a class assignment, would it??).
krispol
Forum Newbie
Posts: 5
Joined: Sun Jul 01, 2012 11:10 am

Re: Randomize pages order

Post by krispol »

califdon wrote:What we're trying to tell you is that we are here to help people learn to use PHP, not to write PHP scripts for you (this wouldn't happen to be a class assignment, would it??).
Thank you for your time then. No this is not a class assignment :) I have learned html and css only in school, and now trying to familiarize with a bit of PHP through Internet cause I need it for a website, which seems to be so complicated for me.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Randomize pages order

Post by califdon »

OK, I don't mean to be coming down hard on you. We really do want to be of help to you and anyone who has a question or problem related to PHP web development. But forums like this are terrible places to try to learn the basics of a computer language; that's beyond our ability to do. What we're good at is helping someone who has a basic knowledge of the language but is having a problem with a particular operation, or is stumped by a specific error message, or needs advice about how to approach a well defined task. This works best when the person requesting the help has enough knowledge of the language that he or she can be given references to applicable PHP functions or language constructs, or to tutorials and examples, that they can then apply those things to their situation. When one of us tries to write your code for you, it is likely to not really meet your requirements, resulting in yet another request for us to adapt it further, which may lead to more and more time spent helping just one person, and since all of us have other things to do, we'd like to help as many people as we can in the time we can devote to this forum. I hope this doesn't sound like I'm blowing you off. Let me try to be more helpful:

If you don't already have some experience using arrays, study that; experiment with a short test script, just to learn what an array can do for you and how to do it. Unless this is the last PHP script you will ever write, this knowledge will pay you back many times over. Then go back and read the suggestions from social_experiment and requinix. I'm sure you will understand how to use their suggestions.
krispol
Forum Newbie
Posts: 5
Joined: Sun Jul 01, 2012 11:10 am

Re: Randomize pages order

Post by krispol »

Thank you for your time again. I absolutely understand. No hard feelings at all. I really hope I will have an opportunity to take some courses related to php or even javascript in the near time at my university.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Randomize pages order

Post by califdon »

If you can spare some time away from your other studies, I suggest such online tutorials as:
http://www.php.net/manual/en/tutorial.requirements.php (and following pages of the tutorial)
http://www.w3schools.com/php/php_ref_array.asp
http://www.homeandlearn.co.uk/php/php6p1.html
http://www.1keydata.com/php-tutorial/array.php
http://www.freewebmasterhelp.com/tutorials/php/4

There are many others, but those would get you started.
krispol
Forum Newbie
Posts: 5
Joined: Sun Jul 01, 2012 11:10 am

Re: Randomize pages order

Post by krispol »

Thanks a lot, I saved them all :)
Post Reply