Welcome to Gaia! ::


Wealthy Capitalist

5,750 Points
  • Millionaire 200
  • Entrepreneur 150
  • Money Never Sleeps 200
User Image - Blocked by "Display Image" Settings. Click to show.
~ By Lord-of-the-flies ~
Welcome to the random signature tutorial. This tutorial will not only provide you with information on how to build two types of simple server-side image randomizers, but also allow you access to the free software "myRAND" with full support. For your convenience, I have made an effort to break down each portion of the tutorial to it's simplest possible form. If you have any questions reguarding this tutorial, please PM me with your question, and I will edit the tutorial to better fit your needs. Thank you.
User Image - Blocked by "Display Image" Settings. Click to show.
-------------------------
My other tutorials
PHP-free SIG MAPS
Shoutbox Sigs
-------------------------

User Image - Blocked by "Display Image" Settings. Click to show.

Wealthy Capitalist

5,750 Points
  • Millionaire 200
  • Entrepreneur 150
  • Money Never Sleeps 200
~ Important: Read before continuing ~

It has recently come to my attention that there are some people trying to make a profit off my tutorials, selling the files in their mini shop. This will not be tolerated. I provide you this tutorial free of charge to make you, the user, able to enjoy your experience of Gaia Online a little better - but when people sell something that is provided free of charge, it makes me angry.

So, let me make this perfectly clear to anyone who might be trying to make a quick buck by selling anything that I provide - YOU WILL BE REPORTED IF I CATCH YOU. The same problem had happened with the GavSim program, and it had shut down for some time as a result - I do not want to have to take a service away from honest Gaians because some greedy n00b decides it would be a good idea to resell.

DO NOT SELL ANY RESULT OR CODE FROM ANY OF MY TUTORIALS
ANYONE CAUGHT DOING SO WILL BE REPORTED IMMEDIATELY.

Wealthy Capitalist

5,750 Points
  • Millionaire 200
  • Entrepreneur 150
  • Money Never Sleeps 200
User Image - Blocked by "Display Image" Settings. Click to show.
There are a few different varieties of random imagery, however they all have the same function: pick an image out at random and display it. Unfortunately, due to restrictions of Gaia, and most phpBB boards, one can not use a client side script to generate random images, so we have to refer to the server-side scripting - In this case, we will be using PHP. The tutorials I have posted will make it possible to display any type of image, though it is strongly recommended that you do not use the BMP format. If you have any questions, feel free to PM me or post on the thread - I'll get back to you with the answers as soon as I can. Happy randomizing!


User Image - Blocked by "Display Image" Settings. Click to show.

|| Find a host
|| The directory method
|| The array method
|| Understanding the code
|| myRAND
|| Example

Wealthy Capitalist

5,750 Points
  • Millionaire 200
  • Entrepreneur 150
  • Money Never Sleeps 200
User Image - Blocked by "Display Image" Settings. Click to show.
Because of the nature of the scripts in this tutorial (being PHP, that is), we must find a host that can support it. There are any number of hosts out there that will support PHP on their servers, but because I know that it's better to have a list of hosts (I know I wouldn't want to search), I'm going to provide you with a few quality PHP web hosts.

|| Tripod UK
|| Host Ultra
|| My Site Space
|| Ripway
|| FBHosting

Wealthy Capitalist

5,750 Points
  • Millionaire 200
  • Entrepreneur 150
  • Money Never Sleeps 200

Wealthy Capitalist

5,750 Points
  • Millionaire 200
  • Entrepreneur 150
  • Money Never Sleeps 200

Wealthy Capitalist

5,750 Points
  • Millionaire 200
  • Entrepreneur 150
  • Money Never Sleeps 200
User Image - Blocked by "Display Image" Settings. Click to show.
This portion of the tutorial is designed to help you understand the PHP code put into the directory and array methods in this tutorial. This is not a required reading to make the image randomizer work, but instead it is designed to help you learn. Note that this portion assumes that you have some basic programming experience in one or more varibable language.

First, we'll take a look into the variables of this script. For those new to PHP, variables are simple assigned sets of characters that stand for something else. In PHP, variables are indicated with a dollar sign prefixed upon the variable name.
$files

This works just like it would in any other programming language, and you simply assign the variable using an equal sign, followed by the variable in question.

Directory Method
Next we look into the function assigned to the $files variable: scandir().
scandir("images")

Scandir() is a function that's been around since PHP 1.0 or so. This function will literally scan a directory and return it's contents as an array. This makes it possible to read file names from a directory without having to first open it, read it, loop it, etc. Very handy indeed. Inside the parenthesis, there is the word "images" encapsulated by quotation marks - This tells the function what directory to read and return.


Now we move on to count().
count($files)

Count() tells us exactly how many objects are inside an array. In this instance, we are counting the objects in the scandir() returned array, which was assigned to $files.

Directory Method
if(count($files) > 2){

The "if" statement, followed by a requirement in parenthesis, makes sure the code inside the brackets is executed only if the statement is true. In this case, we will only execute the code in the brackets provided the number of items in the directory is greater than two. The reason that we look for more than two is because, with each directory there comes two pre-defined statements which lead to a higher directory. Because these are not actual files, we don't want to include them.

Directory Method
$n = rand(2,(count($files)-1))

Rand(a,b) provides us with a random whole number between "a" and "b". Again, we exclude the first two variables (the non-files) by placing "2" as our "a".

Array Method
$n = rand(0,(count($files)-1))

Rand(a,b) provides us with a random whole number between "a" and "b". To start us off, we place "0" in the "a" position to hold the base of the array,

And our "b" will be the number of files minus one. Why the minus one? I'll explain that shortly.

$files[$n]

$files is our array, and $n is our random number. Arrays automatically assign each object in itself a number, starting at "0", and each object can be called though the assigned variable using a square bracket and the number. Because arrays start at zero, we have to compensate for that additional number we would get from the actual number of items, thusly we subtract one from our random number.
Directory Method
In all instances of a directory scan (using the $files variable for example), the first two variables will be:
$files[0] = "."
$files[1] = ".."

This makes $files[2] and higher files in that directory.

header("location: images/$files[$n]")

Here's the most important portion of the file, the header(). Headers are kinda tricky when programming, but because this script is tiny, it shouldn't hurt too much. Header basically re-defines the program after receiving variables, making the files seem like another file in this case. "location: " tells the header that this file will act as the files that it's being assigned to, and "images/$files[$n]" fills in where it is supposed to go.

Directory Method
}

This ends the code that would be executed if the statement is true.

Now, what if the statement actually wasn't true? What if we had no files in that directory? Well, in that case, we have the "Else" statement ready.
else echo "No images avalible";

"Else" follows after a "If" statement, allowing the script something to do if we can't do the first. In this case, we "echo" the sentence "No images avalible." What exactly does echo do? It prints out words on the screen for us to see. Simple, no?


Aaannnndd... that's it! We're done with understanding the code!

Wealthy Capitalist

5,750 Points
  • Millionaire 200
  • Entrepreneur 150
  • Money Never Sleeps 200

Wealthy Capitalist

5,750 Points
  • Millionaire 200
  • Entrepreneur 150
  • Money Never Sleeps 200
~ Example coming soon ~

Wealthy Capitalist

5,750 Points
  • Millionaire 200
  • Entrepreneur 150
  • Money Never Sleeps 200
*cuts ribbon*
Alrighty. Tutorial officially open for buisness!
You may want to check this out:
http://www.gaiaonline.com/forum/viewtopic.php?t=4400804

The array method is already listed in almost the same form. I think Razma may still be using an older version. Jakobo created a directory method, but it uses more lines of code that yours.

Personally, I would prefer if you two combined tutorials to conserve threads in EF. For imagemaps, it's less of a problem as they are more complicated in general and people need more help...thus, each method having a different thread is justifiable.

Wealthy Capitalist

5,750 Points
  • Millionaire 200
  • Entrepreneur 150
  • Money Never Sleeps 200
borobdin
You may want to check this out:
http://www.gaiaonline.com/forum/viewtopic.php?t=4400804

The array method is already listed in almost the same form. I think Razma may still be using an older version. Jakobo created a directory method, but it uses more lines of code that yours.

Personally, I would prefer if you two combined tutorials to conserve threads in EF. For imagemaps, it's less of a problem as they are more complicated in general and people need more help...thus, each method having a different thread is justifiable.


Heh, yeah I saw that thread. I included the array method for reference when dealing with the directory method, and it makes helping people that much easier. Had I not the "Understanding the code" and "myRAND" sections, I would think better of a merge. However, as it stands currently (a learning tool), I believe it best seporate.
I thought I'd add that you should say that the file, the .php file should not be in the images folder. If you did you should say it closer to the part it pertains to.
is there anyway to work one of your Shoutboxes on to the rando-sig
<? $files = scandir("images" wink ;
if(count($files) > 2){
$n = rand(2,(count($files)-1));
header("location: images/$files[$n]" wink ; }
else echo "No images avalible"; ?>

Quick Reply

Submit
Manage Your Items
Other Stuff
Get GCash
Offers
Get Items
More Items
Where Everyone Hangs Out
Other Community Areas
Virtual Spaces
Fun Stuff
Gaia's Games
Mini-Games
Play with GCash
Play with Platinum