Page 1 of 1

Randomize pages order

Posted: Sun Jul 01, 2012 11:13 am
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!

Re: Randomize pages order

Posted: Sun Jul 01, 2012 4:21 pm
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

Re: Randomize pages order

Posted: Sun Jul 01, 2012 4:32 pm
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 :(

Re: Randomize pages order

Posted: Sun Jul 01, 2012 4:42 pm
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.

Re: Randomize pages order

Posted: Sun Jul 01, 2012 5:52 pm
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??).

Re: Randomize pages order

Posted: Mon Jul 02, 2012 6:59 am
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.

Re: Randomize pages order

Posted: Mon Jul 02, 2012 11:55 am
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.

Re: Randomize pages order

Posted: Mon Jul 02, 2012 12:03 pm
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.

Re: Randomize pages order

Posted: Mon Jul 02, 2012 1:00 pm
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.

Re: Randomize pages order

Posted: Mon Jul 02, 2012 1:09 pm
by krispol
Thanks a lot, I saved them all :)