Flash sending, manipulating and receiving data with sendAndLoad
This is a very importat thing to know when you are about to design Flash forms, or any application that needs to manage data such as an highscore table.
This is a standalone tutorial, but the basics explained will be useful in the Creation of a Flash highscores API course.
The only method we need to know is sendAndLoad
Let's dive into an example: I created an input text area called input_text, a button called send_button and a dynamic text called result_text.
Now, look at the code:
-
var php_process:LoadVars = new LoadVars();
-
send_button.onRelease = function() {
-
var post_variable:LoadVars = new LoadVars();
-
post_variable.string = input_text.text;
-
post_variable.sendAndLoad("http://www.emanueleferonato.com/downloads/sendtest.php",php_process,"POST");
-
};
-
php_process.onLoad = function(success:Boolean) {
-
if (success) {
-
result_text.text = php_process.result;
-
}
-
else {
-
result_text.text = "Error connecting to server.";
-
}
-
};
Line 1: Declaring a variable called php_process as LoadVars
The LoadVars class is useful for transferring variables between a Flash application and a server (who said "like an highscore"?)
Line 2: Beginning of the function to be executed when the send_button button has been pressed
Line 3: Declaring another LoadVars variable called post_variable
Line 4: Setting the string attribute of the post_variable as the content of the input_text dynamic text area
Line 5: Using sendAndLoad to pass post_variable to a script on the server at the address http://www.emanueleferonato.com/downloads/sendtest.php using the POST method and saving the result in the php_process variable
Line 7: Beginning of the function to be executed when the php_process has been loaded (when the server replied). Notice the success boolean flag
Lines 8-10: If success is true (I communicated with the server), then display in the result_test dynamic text area the result variable of php_process. We'll see how to get the result variable when we'll examine the php script
Lines 11-13: Dispaying an error if I wasn't able to communicate with the server
Now, let's see the php script in the
http://www.emanueleferonato.com/downloads/sendtest.php page
Line 3: If the string variable passed with POST method contains something...
Line 4: Convert the string to uppercase, just to do something...
Line 5: writing the content of the result variable as the uppercased value of the initial string
Lines 7-9: if the string variable passed with POST does not contain anything, then writing the content of the result variable as a message saying you did not write anything.
And that's it... play with it and give me feedback
Write something in the upper text area and press "send"
Integrate your Flash game on Facebook
You all should know Facebook. I think it would be interesting to integrate your Flash game into a Facebook application.
There are more options than you can imagine, but at the moment I'll just embed a game, nothing more. Just remember there will be a lot more.
Obviously, first you have to have a Facebook account and be logged in
Then you have to install the Developer application.
From this page http://developers.facebook.com/get_started.php click on "Add Facebook Developer Application" and you will be able to start creating your own applications.
Once you clicked on that link, you will find a "Developer" item in the left nav button. This is your application control panel, where you can manage your applications.
Click on "Set up New Application"

You will be redirected to a quite unclear page with a lot of fields to fill... don't worry, you will be out of this step in less than 2 minutes. Read more
Creation of a sortable list with Ajax tutorial
One interesting Web 2.0 effect is the sortable list, and we are going to make one in less than five minutes thanks to Prototype and script.aculo.us libraries.
Prototype is a JavaScript Framework that aims to ease development of dynamic web applications and can be downloaded here, while script.aculo.us (scriptaculous from now on) provides you with easy-to-use, cross-browser user interface JavaScript libraries to make your web sites and web applications fly and can be downloaded here.
This tutorial is based on zen of shen's pseudocode found at script.aculo.us Ajax Sortable Lists Tutorial.
This tutorial will show real code and a step by step guide through MySql, php and Ajax coding in order to create your sortable list.
Of course your hosting plan must allow you to use php and MySql, so refer to your hosting provider if you don't know if your web space supports those features.
MYSQL
First we need to work on the database to create the table that will store our list. I made the simplest table ever, because it's just an example, but you can add as many columns as you want, in order to fit the table to your needs.
My table only has three fields: the primary key id, the name of the list element color and the core column, the one where you will save the order of the elements, called, in a lack of creativity, color_order. Read more
How to create a WordPress Widget
I think it's time to learn how to create a WordPress Widget. WordPress (WP from now on) is the most used blogging platform, and I want to help everybody to code their favorite widgets.
From the official site (where you can find a lot of widgets): WordPress Widgets (WPW) is like a plugin, but designed to provide a simple way to arrange the various elements of your sidebar content (known as "widgets") without having to change any code.
From a geeky point of view, a widget is a php file. Fullstop. Being a php file, you can make whatever you want. You "just" need to master php and follow some guidelines given by WP guys.
To make the simplest widget ever, you have to know only two WP functions:
The first is register_sidebar_widget($name, $function): adds your widget in the widget admin panel. You simply have to change $name with the name of your widget and $function with the function that your widget will execute.
The second one is add_action($event, $function): $event tells what event your $function should be associated with.
Ok, now let's write some php
-
<?php
-
/*
-
Plugin Name: Triqui
-
Plugin URI: http://www.emanueleferonato.com/
-
Description: Test widget
-
Author: Emanuele Feronato
-
Version: 1
-
Author URI: http://www.emanueleferonato.com/
-
*/
-
-
function triqui_widget() {
-
echo"<h2>Triqui widget</h2>I love <a href = \"http://www.emanueleferonato.com\">Emanuele Feronato</a>";
-
}
-
-
function init_triqui(){
-
register_sidebar_widget("Triqui", "triqui_widget");
-
}
-
-
add_action("plugins_loaded", "init_triqui");
-
-
?>
Lines 2-9: This commented code is needed to show WP users who made the plugin, what does the plugin do, where to download updates, and so on. Look at this picture:

It represents your plugin management page, and you can see how your information is displayed according to what you write in lines 2-9
Line 11: Beginning of the widget itself
Line 12: The widget... just writing the title (between h2 tags) and a link to my blog
Line 15: Function to initialize the widget
Line 16: As explained before, I am registering the sidebar widget calling it Triqui, and want it to execute the triqui_widget function, the one at lines 11-13
Line 19: When the plugins are loaded, I want init_triqui to be executed. Doing this way, once plugins are loaded, I register the sidebar widget that will display the link at my blog.
And that's all.
Now you have to save your file with a php extension (for example triqui.php) and upload it in your wp-content/plugins directory you will find in your WP installation.
Then you will find your Triqui plugin in your plugin management page and once you activate it you will find the Triqui widget in your Sidebar Arrangement page (Presentation -> Widgets in your admin panel)

... and... that's it... now drag your widget into your sidebar and you will display my link in your blog.
This was the very first widget... but soon I will teach you how to develop more complex and interactive widgets, and, why not, widgets that could you earn some money.
Meanwhile, why don't you display my widget for a day or two? If you do it...
How to show latest phpBB3 posts in your WordPress sidebar
As you may notice, at the top of the rightmost column of this blog I am showing the latest posts of the forum.
I did not find any widget fitting my needs so I had to solve the problem by myself. And I am going to tell you how.
At the moment it's not a widget, just a bunch of lines in a PHP Code Widget to do the trick. Should I receive good feedback, I will develop a fully customizable widget.
Anyway, following this tutorial will make you able to show your phpBB3 posts in your WP sidebar in a matter of minutes.
First, in your WP admin area go to Presentation -> Widgets to go into Sidebar Arrangement section.
You should find a screen like this one

... but it may be a bit different according to your theme. Anyway, what you need is a PHP Code Widget, so have to create one (look at the bottom arrow in the image) and drag/drop it in the sidebar.
Then you're ready to enter the code in your widget:
-
<?php
-
$connection = mysql_connect(localhost,"your_login","your password") or die("Service temporairly unavailable");
-
$sql = "select * from phpbb_topics order by topic_last_post_time desc limit 0,10";
-
for($x=1;$x<=10;$x++){
-
echo "<a href = \"http://www.yourforumdomain.com/viewtopic.php?f=$row[forum_id]&t=$row[topic_id]\" target = \"_blank\">$row[topic_title]</a><br>";
-
}
-
?>
Line 1: Opening php tag. Normally it makes no difference between <? and >?php but my widget told me
PHP Code MUST be enclosed in tags!
Line 2: Connecting with the database where you are hosting your phpBB3. Change localhost with the address of your database, your_login with database login and your_password with your database password. In case something should fail (database is down, login or password are incorrect), I return a "Service temporairly unavaiable" and terminate the script.
Line 3: Selecting the database where you installed your phpBB3. Change your_db_name with the name of the database.
If you do not remember one (or more) data explained at lines 2-3, simply give a look to your config.php file in the folder where you installed phpBB3.
It's made in this way:
-
<?php
-
// phpBB 3.0.x auto-generated configuration file
-
// Do not change anything in this file!
-
$dbms = 'mysql';
-
$dbhost = '122.122.122.122';
-
$dbport = '';
-
$dbname = 'emanuele';
-
$dbuser = 'triqui';
-
$dbpasswd = 'banana';
-
-
$table_prefix = 'phpbb_';
-
$acm_type = 'file';
-
$load_extensions = '';
-
-
// @define('DEBUG', true);
-
// @define('DEBUG_EXTRA', true);
-
?>
At line 5 you will find the database address (122.122.122.122), at line 7 db name (emanuele), at line 8 db username (triqui) and at line 9 the password (banana).
Also remember line 11 and write down somewhere the content of table_prefix variable (phpbb_)
I did not used any of those names, so don't even try to hack my forum... but let's go back to the script...
Line 4: This is the string representing the query that will create the table with the last 10 updated topcis. phpbb_topics is the name of the table than contains the topics. If you have another value in your table_prefix variable, then you will need to change phpp_topics with your_table_prefix_topics. So, as an example, if your table prefix is triqui, then your table name will be triqui_topics, while you won't have to change topic_last_post_time that represents the timestamp of the last post in the topic. In this way, you will get a table with the latest 10 topic ordered by last post time. If you want more (or less) than 10 topics, change the 10 in limit 0,10
Line 5: Processing the query, or returning in case of error the same fake message "Service temporairly unavaiable"
Line 6: Loop to be executed 10 times
Line 7: Fetching the row of the table in an array called row
Line 8: Displaying the link: notice that the link starts with http://www.yourforumdomain.com/, you will have to change this address with the path of your phpBB3 forum. For instance, if your forum is in the forum folder in the google domain, you will have to write http://www.google.com/forum/. You don't need to change anything else
That's all. Hope you will find it useful... maybe some day I will make a widget that will be easier to configure.
Did I find a php bug?
Maybe today I found a php bug... try to run this script on your server...
-
<?php
-
-
$a=100.9;
-
$b = 100;
-
$c = $a-$b;
-
echo $c;
-
-
?>
What do you get?
I get a 0.90000000000001 on different servers with different php installations
Am I the only one?
100 ROUNDS, a Flash game with Php/MySql hiscore table and explained source code
I want you all to play 100 ROUNDS, a game created for didactic pourpose.
The game is simple: just enter your name, press "Play" and start moving your mouse clockwise trying to make 100 rounds as fast as you can to enter the highscore table.
I took some ideas from a script found on actionscript.org... but I cannot find it.
For this game I provide the entire source code.
Related Item: Stock Control Software - Wasp Barcode ofers stock control software packages to provide small businesses the efficiency of large companies at prices that are affordable.
Step by step perfect maze generation with php
This is a project I made for teaching purpose.
First of all, let's see what is a perfect maze.
From Maze Works: A perfect maze is defined as a maze which has one and only one path from any point in the maze to any other point. This means that the maze has no inaccessible sections, no circular paths, no open areas.
This is a perfect maze

This is not a perfect maze

With the script I provide, you can generate perfect mazes with a given length and height, and the cute thing is that the script will "think loud" its actions in order to explain how does it work.
It uses backtracking.
Php password generator
Today I had to generate some passwords, and I am not so creative in doing this.
So I coded a function I want to share with you.
-
<?php
-
function create_password($length=8,$use_upper=1,$use_lower=1,$use_number=1,$use_custom=""){
-
$upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
-
$lower = "abcdefghijklmnopqrstuvwxyz";
-
$number = "0123456789";
-
if($use_upper){
-
$seed_length += 26;
-
$seed .= $upper;
-
}
-
if($use_lower){
-
$seed_length += 26;
-
$seed .= $lower;
-
}
-
if($use_number){
-
$seed_length += 10;
-
$seed .= $number;
-
}
-
if($use_custom){
-
$seed .= $use_custom;
-
}
-
for($x=1;$x<=$length;$x++){
-
}
-
return($password);
-
}
-
?>
How does it work?
Let' see the parameters
lenght: is the password length (default = 8)
use_upper: set to 0 if you do not want to use uppercase chars (ABCD...), any other value otherwise. Default = 1
use_lower: set to 0 if you do not want to use lowercase chars (abcd...), any other value otherwise. Default = 1
use_number: set to 0 if you do not want to use number chars (0123...), any other value otherwise. Default = 1
use_custom: a string representing any extra char you want (such as ?*_ ...). Default = empy string
Examples:
Enjoy and give me feedback.
Strip non alphanumeric characters from a string with php
Very simple and useful code, but hard to find in the internet.
You may need it when you code an user authentication script and want to prevent the user to hack it with ascii injection.
I will use regular expressions because it is the quickest way, and we want to be quick (almost) everytime...
-
<?php
-
$string_to_be_stripped = "Hi, I am a string and I need to be stripped...";
-
?>
$new_string content will be: Hi I am a string and I need to be stripped without any coma or stop.
Enjoy.
