Archive

Author Archive

Useful Functions (Tips)

September 1st, 2009 Matthew Lodge No comments

The following code generates an array then adds all the numbers in the array together. Really quite useless.

However the methods used to create this snippet are extremely useful.

Here I break them down.

var randomNumber:int = new int();
var myArray:Array = new Array();
var total:int = new int();
var totalNumbers:int = new int();
totalNumbers = 10;
First of all I declare some variables by doing this the variables can be used in and outside of functions. This is called variable scope
function generateNumbers(){
 for(var i:uint; i<totalNumbers; i++){
Here I start a loop with a variable as the number to count to. This lets you modify the code easily
randomNumber = Math.ceil((Math.random()*(5^i^3)));
Here is the code that generates the random numbers. Math.ceil() rounds the numbers up to an interger. Math.random() generates a random number with is then multiplied by 5 to the power of "i"
myArray.push(randomNumber);
The push() function in an array add the value in the argument as a new value in the array.
}
 for(var il:uint; il<totalNumbers; il++){
 total += myArray[il];
Here the variable "il" counts up until it equals "totalNumbers" whilst it does that it cycles through the array and adds the values to the variable "total" using the operator +=
}
}
generateNumbers();
Calling the function
trace(myArray);
trace(total);
Here I trace to the output panel in flash to check the function is working. It is useful for debugging to put trace() functions in to see where the code is failing So as you can see AS3 has many useful functions to help cut the time spent coding. Use these and you're on your way to becoming an ace programmer.
Categories: Actionscript 3 Tags: , , ,

Mmmm…Cookies

August 18th, 2009 Matthew Lodge No comments

Time for a tutorial on PHP Cookies now. Every time you log into a site chances are you’re kept logged in with the help of cookies. Here I will show you a quick guide to cookies.

Syntax and rules.

setcookie(name, value, expire, path, domain);

Cookies must be set before any information is displayed on the page. So set your cookies before the tag and before you echo() or print() anything.

Ok let’s do this…

$expire=time()+60*60*24*6;
setcookie(”name”, “Matthew Lodge”, $expire);

The first part of the code sets an expiry date for the cookie. Here it is set for a week from when the cookie is set. (60 seconds, 60 minutes, 24 hours and 6 days). This cookie is called name and will have a value of Matthew Lodge.

This code is fine for setting a cookie but it will reset the cookie every time the page is refreshed. so to keep the current cookie we can use an if statement.

if(!isset($_COOKIE["name"])){
$expire=time()+60*60*24*6;
setcookie(”name”, “Matthew Lodge”, $expire);
}

This if statement checks whether the cookie exists and if it doesn’t it sets it.

Showing the value of a cookie is easy as shown below

echo $_COOKIE["name"];
//This will show the cookie that is called name

print_r “$_COOKIE”;
//This will print all the cookies in an array

To delete cookies you must set them in the past.

setcookie(”name”, ” “, time()-3600;
//Sets the cookie an hour in the past and destroys it.

So that’s cookies. Remember that some people do not have cookies enabled so always have a contingency plan if you want your application to be used by all.

Categories: PHP Tags: , ,

PHP & mySQL: An intro

August 14th, 2009 Matthew Lodge No comments

The main benefit of PHP is its compatibility with mySQL. In this tutorial you will learn the basics of PHP and mySQL.

<?php

$connection = mysql_connect(’SEVER’, ‘USER’, ‘PASSWORD’);
$selectdb = mysql_select_db(’DB NAME’);

if(!$connection){
die(’Error connecting to database server’);
}
if(!$selectdb){
die(’Error connecting to selected database’);
}

//This code connects the page to the database ready to make a query. By holding //the connection functions in variables we can create a custom error message.

?>

Next I will teach you about query syntax. A basic query follows this rule:

SELECT name FROM table

The SELECT tells mySQL to return a value from the specified field (in this example the field is called “name”). Alternatively you can use the * wild card. This requests all the information from the table selected in the FROM parameter.
The FROM tells mySQL which table in the database the field can be found.

For example to retrieve a name from a table called users the code would look like this:

$query = mysql_query(”SELECT * FROM users”);
if(!$query){
die(’Error in query’);
}

while($row = mysql_fetch_array($query)){
echo $row['firstname'];
echo “<br />”;
}

First of all we query the database for a set of results. Then we echo out all the results using a WHILE loop. The $row variable contains all the information so we specify what information we want from the query using ['']. This returns the information from that field.

The full code

<?php$connection = mysql_connect(’SEVER’, ‘USER’, ‘PASSWORD’);
$selectdb = mysql_select_db(’DB NAME’);

if(!$connection){
die(’Error connecting to database server’);
}
if(!$selectdb){
die(’Error connecting to selected database’);
}

$query = mysql_query(”SELECT * FROM users”);
if(!$query){
die(’Error in query’);
}

while($row = mysql_fetch_array($query)){
echo $row['firstname'];
echo “<br />”; //Add a line break for formatting
}

mysql_close($connection);

?>

The mysql_close() function will close the connection to the database

So as you can see PHP and mySQL can be a formidable combination. By integrating them into your site you can give users an enhanced experience.

Categories: PHP Tags: , ,

PHP Loops

July 15th, 2009 Matthew Lodge No comments

Loops are pieces of code that repeat as many times as required. I will show a snippet on how each loop is used.

The FOR loop

for($i; $i<25; $i++){
//code
}

The loop is called with the word for(). The $i tells the for() loop that a variable called $i is being declared for this loop. The next argument tells the loop that if the variable is smaller than 25 repeat the code. This last argument increases the $i variable by 1.

The WHILE loop

The WHILE() loop is much simpler than the for loop.

$i = 0;
while ( $i<5 ){
//code
$i++
}

The while() loop repeats itself until the statement in is arguemnt is false. The variable is declared outside the loop so multiple loops can be run the same time from one variable. The code to increase the variable must be included inside the loop or it will run indefinately and crash the browser and possibly the server running the script.

The FOREACH loop

The foreach() loop is used with array variables. It is used to echo them out.

$array = array(1, 2, 3,4);
foreach($array as $a){
//code
print $a //prints every value in the array
}

These loops become very important for automating code when you reach more complicated levels of coding. It’s always good to learn for the future.

Categories: PHP Tags: ,

Basic Variables in PHP

July 13th, 2009 Matthew Lodge No comments

Variables. You’re going to be working with them a lot if you’re going to use PHP so you may as well get used to them.

Declaring variables is easy. Just set the name of the variable and its value.

$variable = “Words”; //An example of a normal variable

There are three types of basic variable:

String
Basicly a section of text

$var = “Text 543″;

Boolean
A variable that is true or false

$var = TRUE;

Number
Ok theres two types of number variables an interger (whole number) and a float ( a decimal)

$var = 32; //an interger
$var = 0.54; //a float
  • Generally you should keep the declaration of variables together just for easy reference.
  • If you declare a variable in a function and want to use it outside the function you’ll have to make it a global variable
global $var = “Text 543″ //declaring a global variable

So as you can see variables are important. Stay tuned to find out how to put this knowledge to use and how to use more complicated variables like arrays.

Categories: PHP Tags: ,