Forum in PHP
Saturday 4th of June 2011
Introduction
The forum that is introduced in the this blog post was made many year ago. It relies on a database handling object such as the one shown in the entry
MySQL Databaseclass.
This reliance on a database handling object should in theory mean that it would be possible to change the database layer without changing in the forum. This however assumes that the layer understands SQL.
Also this forum only implement the logic of the forum system. It is entirely without any interface.
Memory game
Friday 3rd of June 2011
The game
The memory game is the classic game where a number of cards paired is layed out randomly on a table with their backside up. Each player then flip two cards and if the cards are the same he gets one point (the pair of cards).
This implementation of the game in javascript is single player and don't actually count the number of pairs removed. When clicking two of the images they turn and after some time if they are the same they become another image and if the ain't they revert back to the normal backside image.
The game is implemented so it makes use of existing HTML this means that if javascript is disabled the viewer still sees something namely the original hteml.
jQuery UI Tournament
Tuesday 31st of May 2011
Introduction
The widget introduced in this entry is able to take a normal HTML list and make a tournament display looking like the one displayed below. The coloring is dependent on the jQuery UI theme used.
Example:Tournament
- Team B
Team A vs Team B
Score 0 - 2
- Team C
Team C vs Team D
Score 1 - 0
- Team E
Team E vs Team F
Score 3 - 1
- Team G
Team G vs Team H
Score 2 - 0
Pollsystem in PHP and MySQL
Monday 23rd of May 2011
Introduction
The goal of the blog post is to show how a pollsystem handling multiple polls and logging of voters can be implemented in PHP using an MySQL database. The codes are given followed by examples of actually using the system.
The core solution introduced in this entry does not provide any builtin interface, so users would have to provide that. The idea here is that without an interface it is easier to reuse the system in different settings.
The entry does however show a simple user interface in the examples.
Comment System
Tuesday 17th of May 2011
Introduction
The following post introduces the comment system used on this page. As with most comment systems it is essentialy a very simplified one forum multiple thread forum.
All blogposts are one thread and the comments in them are post to that thread. However since the Comment system itself only knows the id of a thread it does not require to know where that id come from.
The system does not come with any predefined user interface. So it is entirely backend. This make it simpler to use the system in different places. But it also adds more requirements in the sense that the UI have to be specified.
The system also require Database classes implementing a specified interface. An example of such a class is given in
MySQL Databaseclass.
MySQL Databaseclass
Tuesday 17th of May 2011
Introduction
The classes introduced in this section is used to hide the mysql API function used in PHP from the code and make it object oriented. If mysqli or pdo i avaible it is better to use either of those proable.
The reasons I once made this class was it encapsulated the API so in theory it would be possible to later change to another API by simply implementing a class for it. It made it easier to iterate over the mysql result cause it was implemented in the retuned object that it could be used for as and iterator. It made it easier to print the query and/or error when the queries failed.
The following shows and example of use. It is assumed the classes are in the
db.php file
Example:Using DB and DBResult.
include ("db.php");
$db = new DB();
$db->setPass("XXXXX");
$db->setHost("XXXXX");
$db->setName("XXXXX");
$db->setUser("XXXXX");
$dbo = $db->query("SELECT * FROM table");
if(!$dbo->isError()){
foreach($dbo as $row){
print_r($row);
}
}
LaTeX hyphenations tool
Thursday 12th of May 2011
Introduction
The tool I introduce in this blog post does, in my experience, make it easier to handel custom hyphenating a LaTeX document.
The normal hyphenation command in LaTeX:
Example:The LaTeX hyphenation command.
hyphenation{split-me-at-strokes}
The command tells the LaTeX compiler that it may only split the word splitmeatstrokes where there is a - in the hyphenation command.
Now you may ask how can it possibly get simpler to write hyphenations and the answer is it can't. The problem my tool makes simpler is controlling and expanding collections of hyphenations.
When writing larger teksts/reports you look through the produced document to see if ur satisfied with the word breaks- If you not satisfied with a word break you switch to the LaTeX file and type in a hyphenation command. This mean you have to switch between to large viewing areas.
My program solve this by being a relatively small window, can be placed besides a PDF reader window.
Another problem is if you have already made a hyphenation command for the word. You will essentially overwrite the existing hyphenation with a new one. My program list the existing hyphenations alphabeticly, so you can see how you have broken words that is the same or similar.
It also simplify writing a hyphenation a bit by removing the latex command syntax. So you only have to write:
Example:The LaTeX hyphenation command.
split-me-at-strokes
The program is written in Python, so a Python interpreter is required.
The program window.
DocWriter
Wednesday 11th of May 2011
Introduction
I have been working on at script that can take a tekst file written in one format and convert it into another format.
The result of the experiment i a generator. That lets the user set the reader and writer of the class. The idea is that it lets the user set readers, that can read differently formatted classes. These readers then reads in the data in the text file and decipher it into a datastucture. A writer is then given this datastructure and writes out a string based that is the result of applying the writer to the datastructure given.
This build makes it possible to make several readers that can read differently formatted text and turn it into the same datastructure.
And it makes it possible to make several writers than can be given the same datastructure but write out the different formatted texts.
The reason for me implementing this is that it saves med from writing the class documenations shown on this page in the somewhat tag intensive BB-code used in the page but instead write it in a neat and simpler format.
AjaxCall
Monday 9th of May 2011
I have created the following AJAX encapsulating class in JavaScript. The idea of the class is to encapsulate AJAX in a single object. That handles both sending the request to the server and delegating the result.
The objekt uses the eventhandling concept to call functions when the state of the request changes.
In the following example it is assumed, that the class code is in the "ajax.js" file. The example loads the url
text.xml and alerts it contents:
Example:Using AjaxCall
<html>
<head>
<script src="ajax.js" type="text/javascript"></script>
<script type="text/javascript">
var aj = new AjaxCall();
aj.url = "test.xml";
aj.onloaded = function(o){
alert(o.getAsText());
}
aj.execute();
</script>
</head>
</html>
AjaxCall   |
|---|
| + onuninitialized : Function |
| + onopen : Function |
| + onsent : Function |
| + onreceiving : Function |
| + onloaded : Function |
| + onloadedOk : Function |
| + asynchronous : boolean |
| + url : string |
| - _request : Object |
| + method : String |
| - _params : Object |
| + piggyback : Mixed |
| + mimetype : String |
| +
getAsXML(): XML |
| +
getAsText(): string |
| +
addParam(string, mixed) |
| +
getParam(string): mixed |
| +
execute() |
| -
_readystatechange() |
Detailed AjaxCall


Fields
public onuninitialized : Function
The function to call if the ajax state changes to uninitialized.
public onopen : Function
The function to call if the ajax state changes to open.
public onsent : Function
The function to call if the ajax state changes to sent.
public onreceiving : Function
The function to call if the ajax state changes to receiving.
public onloaded : Function
The function to call if the ajax state changes to loaded.
public onloadedOk : Function
The function to call if the ajax state changes to loadedNotFound.
public asynchronous : boolean
Tells if the call should be asynchronous (true) or synchronous (false).
public url : string
The url to call.
private _request : Object
The request object. The actual type of this depends on the browser.
public method : String
The method used to communicate with the server. Can be either GET or POST.
private _params : Object
The parameters send along with the request. The parameters are encoded in this temporary object where the name of a parameter becomes a field in the object and the value of the parameter becomes the value of the field.
public piggyback : Mixed
Data that has nothing to with the request but should be availble when the response returns can be assigned to this field.
public mimetype : String
If set to anything the mimetype of the receive response is tried to by the set mimetype.
Methods
public
getAsXML
():XML
Returns theXML of the response this can be traversed using normal DOM methods.
Parameters
- No parameters
- This method takes no parameters.
Return
XML
public
getAsText
():string
Return the text of the response.
Parameters
- No parameters
- This method takes no parameters.
Return
string
public
addParam
(string, mixed)
Add a parameter to the request taking the name and value of the parameter. This method must be called before the execute method to have any effect.
Parameters
- string
- The name of the paramater.
- mixed
- The value of the paramater.
Return
None
public
getParam
(string):mixed
Return the value of a parameter. Takes the name of the parameter whose value to return.
Parameters
- string
- The name of the paramater.
Return
mixed
public
execute
()
Executes the request. Sets the _request variable to a request object. Set the url in this object and if requested tries to override the mimetype with the one set. If the method selected is POST the parameters are encoded.
Parameters
- No parameters
- This method takes no parameters.
Return
None
private
_readystatechange
()
This method is called internally when the state of the request changes. If the state changes to a state that has a set event the function set on this event is called with the current AjaxCall object.
Parameters
- No parameters
- This method takes no parameters.
Return
None
CodeAjaxCall

Code :
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
| //version 1.0
function AjaxCall(){
this.onuninitialized=null;
this.onopen=null;
this.onsent=null;
this.onreceiving=null;
this.onloaded=null;
this.onloadedOk=null;
this.onloadedNotFound=null;
this.asynchronous=true;
this.url="";
this._request=null;
this.method="GET";
this._params={};
this.piggyback=null;
this.mimetype='';
}
AjaxCall.prototype.getAsXML=function(){
return this._request.responseXML;
};
AjaxCall.prototype.getAsText=function(){
return this._request.responseText;
};
AjaxCall.prototype.addParam=function(name,value){
this._params[name]=value;
};
AjaxCall.prototype.getParam=function(name){
return this._params[name];
};
AjaxCall.prototype.execute=function(){
if(window.XMLHttpRequest){ // Mozilla, Safari, ...
this._request = new XMLHttpRequest();
if(this._request.overrideMimeType){
this._request.overrideMimeType(this.mimetype);
}
}else{
if(window.ActiveXObject) { // IE
this._request = new ActiveXObject("Microsoft.XMLHTTP");
}
}
var me=this;
this._request.onreadystatechange = function(){
me._readystatechange();
};
this._request.open(this.method, this.url, this.asynchronous);
if (this.method=="POST") {
this._request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
this._request.setRequestHeader("Content-length",this._params.length);
this._request.setRequestHeader("Connection", "close");
var str="";
var sep="";
for(var prop in this._params){
str+=sep+prop+"="+this._params[prop];
sep="&";
}
this._request.send(str);
}else{
this._request.send(null);
}
};
AjaxCall.prototype._readystatechange=function(){
switch(this._request.readyState){
case 0:
if(this.onuninitialized!=null){
this.onuninitialized(this);
}
break;
case 1:
if(this.onopen!=null){
this.onopen(this);
}
break;
case 2:
if(this.onsent!=null){
this.onsent(this);
}
break;
case 3:
if(this.onreceiving!=null){
this.onreceiving(this);
}
break;
case 4:
if(this.onloaded!=null){
this.onloaded(this);
}
if(this.onloadedOk!=null&&this.request.status==200){
this.onloadedOk(this);
}
if(this.onloadedNotFound!=null&&this.request.status==404){
this.onloadedNotFound(this);
}
break;
}
};
|
CSS gallery
Wednesday 30th of March 2011
The showing
The following gallery is created only using HTML and CSS.
The features of the gallery is as follows:
- Thumbnails
- Full images
- Default image
In my opinion this gallery should never ever be used on any page. The reason for this is that the gallery loads all pictures in at once. For that very reason the gallery can only be viewed in isolation mode.