This is a fairly simple little starter tutorial for people interested in coding their own generators. Hopefully we'll end
up with something that looks like
[ This ].
You will need:
- A fairly good understanding of HTML
- A basic understanding of what PHP is
- A file host that supports PHP like [ 110mb.com ]
PHP BASICS REMINDER
- PHP is a server side language. This means you can't preview PHP pages directly on your computer-
you have to upload them first.
- To save your page as PHP, just name it yourfilename.php
- Anything that starts with a $ sign in PHP is a variable. Variables can contain any value you assign
to them, eg if you wrote $myname = "ElectricStorm" $myname would be read as ElectricStorm for the rest of the page.
Let's start with the HTML form. We're going to want the user to select from a choice of color schemes, so we'll use radio
buttons for those fields since they allow an either-or choice. For the text the user wants to rainbow-ify, we're going to
use a textarea.
Code:
<form action="" method="post">
<input type="radio" name="colorscheme" value="rainbow" checked="checked">
Rainbow Colors <br>
<input type="radio" name="colorscheme" value="pinkblue">
Pinks & Blues <br><br>
Enter some text to color: <br>
<textarea rows="6" cols="50" name="message"></textarea> <br><br>
<input type="submit" name="submit" value="Generate Rainbow Text">
</form>
Notice that the form action is empty, which means it'll post to the same page its on, so we can put the PHP we're going to
use to process the form right above it.
JARGON BIT: POSTING FORMS / $_POST
When someone pushes the button to submit a HTML form, the data can be sent for processing using either the POST or the GET
method. Most of the time you're going to want to use POST, as it sends data invisibly, and is a little more flexible (GET
sends data in the address bar, and has a limit on the number of characters). Notice on our form above the method is post.
In PHP, we retrieve posted data by using $_POST['fieldname'] to refer to the form field we want to look at. In the form above,
the textarea is named "message" so once our form is submitted to PHP, $_POST['message'] will contain any text that was in
the textarea.
Now to start writing the PHP, so that something happens when you submit the form! You can write PHP so that it executes as
soon as someone opens that page, but in this case, we only want the PHP code to go off if someone actually submits the form,
so we're going to put it all inside an if statement:
Code:
<?php
if ($_POST['submit'] == "Generate Rainbow Text") {
The <?php tag tells the server that the next bit of code is in PHP. Then the if statement checks whether the posted value
of the submit button is "Generate Rainbow Text" (notice that's the value it has on the form above). This works because the
$_POST vars are only created when the form is posted, so initially $_POST['sumbit'] will be empty. Anything we put between the
curly bracket after the if statement, and the closing curly bracket } that we're going to put at the end of our php code, will
only execute if the form is posted.
JARGON BIT: COMPARING VALUES
- = one equals makes one value equal the other, eg $myname = "ElectricStorm"
- == double equals checks whether 2 values are the same as each other, eg if ($_POST['submit'] == "Generate Rainbow Text")
checks whether $_POST['submit'] contains the value "Generate Rainbow Text"
- != means not equal. If we only wanted a piece of code to execute if $myname isn't ElectricStorm, we could use
if ($myname != "ElectricStorm") to check the values aren't equivalent.
Next, we want to tell PHP which colors to make the text. I'm going to use arrays of hex codes here, one that contains rainbow colors
and one that contains pinks and blues:
Code:
//color arrays
$rainbow = array('#ff0000', '#ff3300', '#ff6600', '#ff9900', '#ffcc00', '#ffff00', '#ccff00', '#99ff00', '#66ff00', '#33ff00', '#00ff00', '#00ff33', '#00ff66', '#00ff99', '#00ffcc', '#00ffff', '#00ccff', '#0099ff', '#0066ff', '#0033ff', '#0000ff', '#3300ff', '#6600ff', '#9900ff', '#cc00ff', '#ff00ff', '#ff00cc', '#ff0099', '#ff0066', '#ff0033');
$pinkblue = array('#cc0000', '#ff6600', '#ff9900', '#99cc00', '#66cc00', '#339900', '#339966', '#009999', '#0099cc', '#3399ff', '#3366ff', '#3333ff', '#3300cc', '#6600cc', '#9900cc', '#cc00cc', '#ff00ff', '#ff00cc', '#ff0099', '#ff0066', '#ff0033');
As you can see, it doesn't matter how many different colors you have in each array, and you can use color names instead of
hex codes if you like, or a mix of both.
JARGON BIT: ARRAYS
Variables can be simple and contain just one value (eg $myname = "ElectricStorm"), or they can be a little more complex and
contain a whole array of values like $rainbow in the example above.
The individual values in an array can be picked out using $arrayname[number], (numbers always start counting from 0 rather
than 1) so $rainbow[0] would be the first value in $rainbow, which is #ff0000. $rainbow[1] would be #ff3300, etc.
Next we're going to take the message the user wants to rainbow-ify, and make sure it isn't empty and doesn't contain
any malicious code.
Code:
//get the message
$message = strip_tags($_POST['message']);
if (empty($message)) {
echo("Oops! You didn't type any text to rainbow-ify!");
}
else {
The very first thing we're doing in this bit of code, is getting the posted value of the form field named "message" (the
textarea) and making sure it doesn't contain anything it shouldn't. strip_tags means that $message becomes whatever was in
the textarea, but with any tags removed.
Next, if the message is empty we want to show the user an error. Echo statements make something display on the page, so if
the message is empty, the user will see "Oops! You didn't type any text to rainbow-ify!" on the page.
Finally we have an else bit. This is because we only want the next bit of code to execute if the message isn't empty, and
there's some text to color! (We need to remember to end this else statement with a } closing curly bracket at the end of
the section).
Since we have 2 choices of colorscheme, the next thing to do is decide which one to use:
Code:
//decide which color array to use
if ($_POST['colorscheme'] == "rainbow") { $colorarr = $rainbow; }
else { $colorarr = $pinkblue; }
$_POST['colorscheme'] contains the value of the selected radio button on the form. Since there's only 2, we know it's going
to be either "rainbow" or "pinkblue", so we set the $colorarr variable to equal whichever array of color codes matches the
radio button selected. Now $colorarr is either $rainbow or $pinkblue, depending which one the user chose.
Now for the really clever part. We're going assign each letter in $message a different color from $colorarr. To do that, we
first have to split $message into an array of individual letters, so we can work through them in a foreach loop.
Code:
//split message into array of single letters
$messagearr = str_split($message);
JARGON BIT: FOREACH LOOPS
We can use foreach loops to look at each value in an array in turn. The code between the { curly brackets } of the foreach
statement is executed for every item in the array. A foreach loop looks like this:
foreach ($array as $item) { //do this code }
Each time the loop runs, $item is assigned a new value, so for example when we loop through the $messagearr array
below, $letter is the first letter of the message on the first loop, the second letter the next time, and so on, until the code
has executed for every letter in the array. Then the loop stops, and the parser moves on to whatever code is next.
Because we want each letter in the message a different color from $colorarr, we need to keep a count of which color we're
up to. (remember $colorarr[0] is the first hex code, $colorarr[1] is the second, etc). We also need to know how many hex
codes are in the color array altogether, so that when we get to the last one, we can reset this count to zero, and run
through the colors again, as many times as we need to color all the letters in the message.
Code:
$i = 0; //$i will keep count of which color in $colorarr we're up to
$max = count($colorarr); //the total number of colors in $colorarr
Now for the loop. Look out for a new var in the code below, called $rainbowmsg. By the time the loop has finished, $rainbowmsg
will contain the entire rainbow-ified message. We're going to add it one letter at a time:
Code:
//loop through $messagearr
foreach ($messagearr as $letter) {
//if character isn't a space, assign color and add to $rainbowmsg:
if ($letter != " ") {
$rainbowmsg .= "<font color=\"".$colorarr[$i]."\">$letter</font>";
}
//else add a space to $rainbowmsg:
else { $rainbowmsg .= " "; }
//+1 to $i (so that the next loop uses the next color in $colorarr):
$i++;
//if we've reached the end of $colorarr, set $i to zero to start again:
if ($i == $max) { $i = 0; }
} //(end foreach loop)
JARGON BIT: DOT EQUALS AND ++
.= No it isn't a typo- .= means "also equals". In the loop above we're using it to add each new letter or space to the existing
$rainbowmsg. Without the dot, $rainbowmsg would be overwritten on each new loop, so end up being only the last character of the
message!
++ Is simply a shorthand way of adding 1 to the existing number. $i++ is exactly the same as saying $i = $i +1.
Notice that our choice of font color on each loop is defined by $colorarr[$i]. This is clever because $i is increasing by one
each loop, so is the next value from $colorarr each time. The last line in the loop checks whether $i has reached the total
number of colors in $colorarr yet, and if so resets it back to zero, so the next loop uses the first color in the array again.
Finally, we want to display the rainbow-ified text in a text box for the user to copy:
Code:
//output the rainbow code:
echo("Your Rainbow Code:
<form action=\"\" method=\"post\">
<textarea rows=\"6\" cols=\"50\" name=\"rainbowcode\">$rainbowmsg</textarea><br><br>
<input type=\"button\" value=\"Select All\" onclick=\"this.form.rainbowcode.focus(); this.form.rainbowcode.select();\">
</form><br><br>");
JARGON BIT: SLASHES (ESCAPING)
So why the slashes before all the speech marks in the echo statement? It's because the echo statement begins and ends with
speech marks. If we place a random speech mark in the middle of the echo, it'll be mistakenly read as the end of the echo statement,
and the parser will be confused when it's followed by more code. Preceding all these inner speech marks with a slash tells the
parser they should be read as part of the text to display, and not the end of the echo statement. This is referred to as escaping
them.
Finally, we want to remember to end the else, and if statements that we started way up there somewhere, and end the
php section (the input form is in plain html):
Code:
} //end else (message not empty)
} //end if ($_POST)
?>
And if you did it all right, you should have something that looks like this:
Complete Code:
<?php
if ($_POST['submit'] == "Generate Rainbow Text") {
//color arrays
$rainbow = array('#ff0000', '#ff3300', '#ff6600', '#ff9900', '#ffcc00',
'#ffff00', '#ccff00', '#99ff00', '#66ff00', '#33ff00', '#00ff00', '#00ff33',
'#00ff66', '#00ff99', '#00ffcc', '#00ffff', '#00ccff', '#0099ff', '#0066ff',
'#0033ff', '#0000ff', '#3300ff', '#6600ff', '#9900ff', '#cc00ff', '#ff00ff',
'#ff00cc', '#ff0099', '#ff0066', '#ff0033');
$pinkblue = array('#cc0000', '#ff6600', '#ff9900', '#99cc00', '#66cc00',
'#339900', '#339966', '#009999', '#0099cc', '#3399ff', '#3366ff',
'#3333ff', '#3300cc', '#6600cc', '#9900cc', '#cc00cc', '#ff00ff', '#ff00cc',
'#ff0099', '#ff0066', '#ff0033');
//get the message
$message = strip_tags($_POST['message']);
if (empty($message)) {
echo("Oops! You didn't type any text to rainbow-ify!");
}
else {
//decide which color array to use
if ($_POST['colorscheme'] == "rainbow") { $colorarr = $rainbow; }
else { $colorarr = $pinkblue; }
//split message into array of single letters
$messagearr = str_split($message);
$i = 0; //$i will keep count of which color in $colorarr we're up to
$max = count($colorarr); //the total number of colors in $colorarr
//loop through $messagearr
foreach ($messagearr as $letter) {
//if character isn't a space, assign color and add to $rainbowmsg:
if ($letter != " ") {
$rainbowmsg .= "<font color=\"".$colorarr[$i]."\">$letter</font>";
}
//else add a space to $rainbowmsg:
else { $rainbowmsg .= " "; }
//+1 to $i (so that the next loop uses the next color in $colorarr):
$i++;
//if we've reached the end of $colorarr, set $i to zero to start again:
if ($i == $max) { $i = 0; }
} //(end foreach loop)
//output the rainbow code:
echo("Your Rainbow Code:
<form action=\"\" method=\"post\">
<textarea rows=\"6\" cols=\"50\" name=\"rainbowcode\">$rainbowmsg
</textarea><br><br>
<input type=\"button\" value=\"Select All\"
onclick=\"this.form.rainbowcode.focus();
this.form.rainbowcode.select();\">
</form><br><br>");
} //end else (message not empty)
} //end if ($_POST)
?>
<form action="" method="post">
<input type="radio" name="colorscheme" value="rainbow" checked="checked">
Rainbow Colors <br>
<input type="radio" name="colorscheme" value="pinkblue">
Pinks & Blues <br><br>
Enter some text to color: <br>
<textarea rows="6" cols="50" name="message"></textarea> <br><br>
<input type="submit" name="submit" value="Generate Rainbow Text">
</form>
Now save your page as yourfilename.php, and upload it to your web server to try it out!
As a final finishing touch, I ran the colorscheme titles from the input form through the generator so that people using
it will be able to see what their rainbow text will look like.